2

Im not able to declare the table headers dynamically in the datatables. Here's what i've tried so far:

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,

        });
    }

My JSON result:

{"Column1":["ABC","XYZ"],"Column2":[0.0,0.0],"Colum3":[0.0,0.0],,"Columns":["Column1","Column2","Colum3"]}

1 Answer 1

4

You have two options to achieve this.

1. Change your JSON result to below format:

{"COLUMNS":[{ sTitle: "COLUMN1"}, { sTitle: "COLUMN2"}, { sTitle: "COLUMN3"}], "DATA":[[" ABC","DEF","XYZ"],["0.0,"0.0","0.0"],["1","2","3"],["I","II","III"]]}

2. As jQuery Datatable is HTML table that render rows and columns, parse your JSON result
and append columns to table thead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.