How do I pass Column names

How do I pass Column names

anilkumar6714anilkumar6714 Posts: 2Questions: 1Answers: 0
edited September 2015 in Free community support

$(document).ready(function() {

$('#example').DataTable( {

"ajax": '../ajax/data/arrays.txt'

} );

} );

In the above code how do I pass Column names, because My requirement is to pass Columns dynamically, Column names and data is not same, Means based on column names the table will laod

This question has an accepted answers - jump to answer

Answers

  • ThomDThomD Posts: 334Questions: 11Answers: 43
    Answer ✓
  • anilkumar6714anilkumar6714 Posts: 2Questions: 1Answers: 0

    Thanks for the reply ThomD,
    that reference helped me to get Dynamic column names, but as i said my data is not in the format of Columns (i.e, data is in Objects format and columns are in Array format).

    I have also tried the below reference

    http://datatables.net/forums/discussion/6260/dynamic-column-names

    Combining both references code it worked me fine :)

     $.ajax({
            "url": '/ajaxcallurl/',
            "success": function (result) {
                var tableHeaders;
                $.each(result.columns, function (i, val) {
                    tableHeaders += "<th>" + val + "</th>";
                });
    
                 $("#tableDiv").empty();
                 $("#tableDiv").append('<table id="displayTable" class="display"     cellspacing="0" width="100%"><thead><tr>' + tableHeaders + '</tr></thead></table>');
    
                 var columns = [];
                jQuery.each(result.columns, function (i, value) {
                    var obj = { data: value };
                    columns.push(obj);
                });
    
                var table = $('#displayTable').DataTable({
                    data: result.data,
                    columns: columns
    
                });
    
            },
            "dataType": "json"
        });
    

    Thanks again for helping me out :)

This discussion has been closed.