求教easyui datagrid如何动态生成列

2024-12-01 11:56:46
推荐回答(1个)
回答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 \"\"+val+\"\";}";
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,设置列的内容的