You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.4 KiB
JavaScript

/// <reference path="../Scripts/MicrosoftAjax.js" />
/// <reference path="../Scripts/jquery.min.js" />
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("<table border=0 cellpadding=0 cellspacing=0>");
html.append("<tr>");
var index = 0;
$(My.DataSource).each(function (i) {
var controlHtml = FormatCellTemplater(this);
html.append("<td Align='" + My.Align + "' index='" + i + "'>" + controlHtml + "</td>");
index++;
if (My.RepeatColumns > 0 && index >= My.RepeatColumns) {
html.append("</tr><tr>");
index = 0;
}
});
if (index > 0) html.append("</tr>");
html.append("</table>");
$("#" + My.id).html(html.toString());
}
//#endregion
//#region 表(Table)垂直视图
function CreateTableVertical() {
var html = new Sys.StringBuilder();
html.append("<table border=0 cellpadding=0 cellspacing=0>");
html.append("<tr>");
var index = 0;
var RowCount = My.RepeatColumns;
var ColCount = Math.ceil(My.DataSource.length / RowCount);
for (var i = 0; i < RowCount; i++) {
html.append("<tr>");
for (var ii = 0; ii < RowCount * ColCount; ii += RowCount) {
if (My.DataSource.length <= i + ii) {
html.append("<td>&nbsp;</td>");
continue;
}
var controlHtml = FormatCellTemplater(My.DataSource[i + ii]);
html.append("<td Align='" + My.Align + "' index='" + (i + ii) + "'>" + controlHtml + "</td>");
}
html.append("</tr>");
}
html.append("</table>");
$("#" + 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
}