2

我无法在数据表中动态声明表头。这是我到目前为止尝试过的:

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"]}
4

1 回答 1

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.

于 2013-08-12T15:23:01.887 回答