通過(guò)后端的動(dòng)態(tài)表頭渲染表格
function initTable(isSearch) {
//獲取表頭數(shù)據(jù)
$.ajax({
url: "test/fieldList",
method: "get",
success: function (res) {
if (!isSearch) {
//只在初始進(jìn)入頁(yè)面時(shí)獲取表頭
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(); //刪除表頭項(xiàng)傀广,渲染表格
table.render({
elem: "#tableid",
cols: [[
{
title: "編號(hào)",
type: "numbers",
fixed: "left",
}]],
data: res.data,
limit: res.data.length,
});
},
error: function (resp) {
layer.msg(resp);
},
});
}