/// /// DataList = function (id) { this.id = id; this.RepeatColumns = 10; this.Align = "left"; //文字对齐 this.RepeatLayout = "Table"; //Table或Flow this.RepeatDirection = "Horizontal" //Horizontal或Vertical this.DataSource = null; //接受一个数组对象 this.CellTemplater = ""; var My = this; //绑定数据 this.DataBind = function (source) { My.DataSource = source; if (My.RepeatLayout == "Table") { if (My.RepeatDirection == "Horizontal") { CreateTableHorizontal(); } else { CreateTableVertical(); } } } //#region 表(Table)水平视图 function CreateTableHorizontal() { var html = new Sys.StringBuilder(); html.append(""); html.append(""); var index = 0; $(My.DataSource).each(function (i) { var controlHtml = FormatCellTemplater(this); html.append(""); index++; if (My.RepeatColumns > 0 && index >= My.RepeatColumns) { html.append(""); index = 0; } }); if (index > 0) html.append(""); html.append("
" + controlHtml + "
"); $("#" + My.id).html(html.toString()); } //#endregion //#region 表(Table)垂直视图 function CreateTableVertical() { var html = new Sys.StringBuilder(); html.append(""); html.append(""); var index = 0; var RowCount = My.RepeatColumns; var ColCount = Math.ceil(My.DataSource.length / RowCount); for (var i = 0; i < RowCount; i++) { html.append(""); for (var ii = 0; ii < RowCount * ColCount; ii += RowCount) { if (My.DataSource.length <= i + ii) { html.append(""); continue; } var controlHtml = FormatCellTemplater(My.DataSource[i + ii]); html.append(""); } html.append(""); } html.append("
 " + controlHtml + "
"); $("#" + My.id).html(html.toString()); } //#endregion //#region 流水平视图 function CreateFlowHorizontal() { } //#endregion //#region 格式化模板 function FormatCellTemplater(model) { var controlHtml = My.CellTemplate; StringArray = controlHtml.match(new RegExp("\{Bind [^}]*\}", "ig")); if (StringArray != null) { for (var i = 0; i < StringArray.length; i++) { var ss = StringArray[i].substr(6, StringArray[i].length - 7).split(","); //分隔字符串 var value = model[ss[0]]; //数据源的值 if (value == null) value = ""; if (ss.length == 1) { //不需要格式化 controlHtml = controlHtml.replace("{Bind " + ss[0] + "}", value) } else { if (value == "") { controlHtml = controlHtml.replace("{Bind " + ss[0] + "," + ss[1] + "}", ""); } else { var FormatValue = ""; if (ss[1] != "encode") { if (ss[1].length == 2 && ss[1].substr(0, 1) == "d") { FormatValue = parseFloat(value.localeFormat("n" + ss[1].substr(1, 1))).localeFormat("d"); } else { FormatValue = value.localeFormat(ss[1]); } } else { FormatValue = escape(value); } controlHtml = controlHtml.replace("{Bind " + ss[0] + "," + ss[1] + "}", FormatValue) } } } } controlHtml = controlHtml.replace(new RegExp("checked='false'", "ig"), ""); return controlHtml; } //#endregion }