5

I want to populate a Datatable with a dynamic column header and column data. I can populate dynamic column data successfully, but I can't achieve a dynamic column. I am using an array I get from a JSON AJAX request. My code is this:

<body>
    <table id="example" class="display" cellspacing="0" width="100%" border="1"></table>
</body>
var JSONResult = '{"column1":"data1","column2":"data2","column3":"data3","columnN":"dataN"}';
var row_dtable = new Array();
var dtable_api = $('#example').dataTable();

$.each(JSONResult , function(key, value) {
    row_dtable.push(value);
}); 
dtable_api.api().row.add(row_dtable).draw(false);  

Thanks in advance.

2
  • What is attributes? Commented Jun 17, 2016 at 8:50
  • Sorry,I have edited code.. Commented Jun 17, 2016 at 8:52

3 Answers 3

0

Datatables doesn't have the ability to change it's structure once being created. You have to destroy it and then re-create with a new set of columns.

Additional reading: How to dynamically change Datatables multiple column headers using ajax and jquery without refreshing the webpage?

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

Comments

0

Ideally your JSONResult will always have the same columns and only data will be updated. For this the solution will be to create the columns once in the header of your table and then using the datatable API to add the data.

First you should change your table HTML like this, which shouldn't be a problem.

<body>
 <table id="example" class="display" cellspacing="0" width="100%" border="1">
   <thead>
     <tr></tr>
   </thead>
 </table>
</body>

Then you create the headers through jQuery

   var JSONResult = [{ "column1": "data1", "column2": "data2", "column3": "data3", "columnN": "dataN" },
            { "column1": "data1", "column2": "data2", "column3": "data3", "columnN": "dataN" }];

        $.each(JSONResult[0], function (key, value) {
            $('#example thead tr:first-child').append($('<th>', {
                text: key
            }));
        });

And last, add the data to dataTable

        var row_dtable = new Array();
        var dtable_api = $('#example').dataTable();
        $.each(JSONResult, function (i, l) {
            $.each(l, function (key, value) {
                console.log(key + " " + value);
                row_dtable.push(value);

            });
            dtable_api.api().row.add(row_dtable).draw(false);
        });

Comments

0

This code worked for me.

I was getting an issue in the console with your code.

Uncaught TypeError: Cannot read properties of undefined (reading 'aDataSort')

Then I modified the code according to the code below.

I used THEAD with TR

And added few properties, and code worked

Output

enter image description here

Workable Code

<html>
<head>
     <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css">
   
</head>
<body>
     <table id="example" class="display" cellspacing="0" width="100%" border="1">
   <thead>
     <tr></tr>
   </thead>
 </table>
     <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript">
        var JSONResult = '{"column1":"data1","column2":"data2","column3":"data3","columnN":"dataN"}';
        var row_dtable = new Array();
        var dtable_api = $('#example').dataTable({

            bSort: false,
            aoColumns: [ { sWidth: "45%" }, { sWidth: "45%" }, { sWidth: "10%", bSearchable: false, bSortable: false } ],
        "scrollY":        "200px",
        "scrollCollapse": true,
        "info":           true,
        "paging":         true
        });

            $.each(JSON.parse(JSONResult) , function(key, value) {
                row_dtable.push(value);
            });     
            dtable_api.api().row.add(row_dtable).draw(false);  
    </script>
</body>
</html>

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.