首页 > 代码库 > easyui datagrid动态添加列
easyui datagrid动态添加列
1、脚本
<script type="text/ecmascript">
var grid;
$(function () {
grid = $(‘#gridT‘).datagrid({
fit: true,//自动大小
rownumbers: true,//行号
//loadMsg:‘数据装载中......‘,
singleSelect: true,//单行选取
pagination: false,//显示分页
columns: [],
toolbar: [{
text: ‘显示1‘,
iconCls: ‘icon-add‘,
handler: newTab
}]
});
});
function newTab() {
$.post(‘test/test2.ashx‘, { id: 1 },
function (data) {
alert(data);
grid.datagrid({
columns: [data.columns]
}).datagrid("loadData", data);
}, ‘json‘);
}
</script>
<body>
<div id="gridT"></div>
</body>
2、json串格式:{\"total\":1,\"rows\":[{\"num\":\"10010\"}],\"columns\":[{\"field\":\"num\",\"title\":\"编号\",\"align\":\"center\",\"width\":\"100\"}]}
test.ashx 代码如下所示:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.ContentEncoding = Encoding.UTF8;
string strJson = "{\"total\":1,\"rows\":[{\"num\":\"10010\"}],\"columns\":[{\"field\":\"num\",\"title\":\"编号\",\"align\":\"center\",\"width\":\"100\"}]}";
context.Response.Write(strJson);
context.Response.Flush();
context.Response.End();
}
easyui datagrid动态添加列