求教easyui datagrid如何动态生成列
1个回答
展开全部
动态产生列的代码,其实就是按datagrid所需列的json格式,拼凑成字符串还可以实现列合并
//用字符串产生grid所需的列
function AddColspanGridColumn(fieldName, title, width, align, rowStyle, formatterStyle,colspan) {
var columnStr = "";
//Easyui的datagrid列的基本格式
columnStr = "{field: '" + fieldName + "', title: '" + title + "', width: " + width;
//对齐方式
if (align != "" && align != null && align != undefined)
columnStr += ",align:'" + align + "'";
//是否合并列
if (colspan != 0 && colspan != "" && colspan != null && colspan != undefined)
columnStr += ",colspan:" + colspan;
//是否需要行样式
if (rowStyle != "" && rowStyle != null && rowStyle != undefined)
columnStr += ",styler: function (val, row, index) {return '" + rowStyle + "';}";
if (formatterStyle != "" && formatterStyle != null && formatterStyle != undefined)
columnStr += ",formatter: function (val, row, index) {" +
"if (val == '' || val == undefined) val = '0';" +
"return \"<span style='" + formatterStyle + "'>\"+val+\"</span>\";}";
columnStr += "}";
return columnStr;
}
然后把产生的json字符串给datagrid,
var options = $("#" + gridName).datagrid("options");
var columnstr =
AddColspanGridColumn("AVG_PRICE", "户均价", 80, "left",
"background-color:#F6B03E;",
"font-size:14px;font-weight:bold;",
0)
options.columns = eval(columnstr);
设置列内容为超链接,参考函数里的formatter,设置列的内容的
//用字符串产生grid所需的列
function AddColspanGridColumn(fieldName, title, width, align, rowStyle, formatterStyle,colspan) {
var columnStr = "";
//Easyui的datagrid列的基本格式
columnStr = "{field: '" + fieldName + "', title: '" + title + "', width: " + width;
//对齐方式
if (align != "" && align != null && align != undefined)
columnStr += ",align:'" + align + "'";
//是否合并列
if (colspan != 0 && colspan != "" && colspan != null && colspan != undefined)
columnStr += ",colspan:" + colspan;
//是否需要行样式
if (rowStyle != "" && rowStyle != null && rowStyle != undefined)
columnStr += ",styler: function (val, row, index) {return '" + rowStyle + "';}";
if (formatterStyle != "" && formatterStyle != null && formatterStyle != undefined)
columnStr += ",formatter: function (val, row, index) {" +
"if (val == '' || val == undefined) val = '0';" +
"return \"<span style='" + formatterStyle + "'>\"+val+\"</span>\";}";
columnStr += "}";
return columnStr;
}
然后把产生的json字符串给datagrid,
var options = $("#" + gridName).datagrid("options");
var columnstr =
AddColspanGridColumn("AVG_PRICE", "户均价", 80, "left",
"background-color:#F6B03E;",
"font-size:14px;font-weight:bold;",
0)
options.columns = eval(columnstr);
设置列内容为超链接,参考函数里的formatter,设置列的内容的
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询