我无法在数据表中动态声明表头。这是我到目前为止所尝试的内容:
if (result != "[]") {
var resultColumns = [];
$.each(result.Columns, function(i, value){
var obj = { sTitle: value };
resultColumns.push(obj);
});
$('#trendingTable').dataTable({
"aaData": result,
//important -- headers of the json
"aoColumns": [ resultColumns ],
"sPaginationType": "full_numbers",
"aaSorting": [[0, "asc"]],
"bJQueryUI": true,
"bDestroy": true,
});
}
我的JSON结果:
{"Column1":["ABC","XYZ"],"Column2":[0.0,0.0],"Colum3":[0.0,0.0],,"Columns":["Column1","Column2","Colum3"]}
答案 0 :(得分:4)
您有两种方法可以实现这一目标。
<强> 1 即可。将您的JSON结果更改为以下格式:
{"COLUMNS":[{ sTitle: "COLUMN1"},
{ sTitle: "COLUMN2"}, { sTitle: "COLUMN3"}],
"DATA":[[" ABC","DEF","XYZ"],["0.0,"0.0","0.0"],["1","2","3"],["I","II","III"]]}
2. 由于jQuery Datatable是呈现行和列的HTML表格,因此解析您的JSON结果
并将列附加到table thead
。