通过后端的动态表头渲染表格
function initTable(isSearch) {
//获取表头数据
$.ajax({
url: "test/fieldList",
method: "get",
success: function (res) {
if (!isSearch) {
//只在初始进入页面时获取表头
for(var i = 1,len = Object.keys(res.data[0]).length;i<=len;i++){
var field = 'key'+i; //key1,key2,key3...
var fieldObj = {
field: field,
title: res.data[0][field]
}
if(i>1){
fieldObj.templet = '<div>{{d["'+field+'"]}}%</div>';
}
cols.push(fieldObj);
}
}
res.data.shift(); //删除表头项,渲染表格
table.render({
elem: "#tableid",
cols: [[
{
title: "编号",
type: "numbers",
fixed: "left",
}]],
data: res.data,
limit: res.data.length,
});
},
error: function (resp) {
layer.msg(resp);
},
});
}