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

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

最佳答案

您有两种选择可以实现此目的。

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

09-07 21:18