Dynamically Add DataTable Column Headings Data Rows

Dynamically Add DataTable Column Headings Data Rows

cgswtsu78cgswtsu78 Posts: 3Questions: 0Answers: 0
edited March 2012 in General
Does anyone have an example of dynamically adding column headings to a Datatable and rows of data using the API?
All of the examples here, depend on the and elements being in the and and being in the . I would like to dynamically create the 's in the and . Does the API support this?

Replies

  • jamesoakleyjamesoakley Posts: 5Questions: 0Answers: 0
    You can use jquery to query your server-side script for the table structure and then create the table DOM using jquery before calling the DataTables on it. The DataTables needs the portion for it to work properly.

    As for an exmaple:

    Your server side response will be a json object of the table columns such as:

    [code]
    ["Col 1", "Col 2", "Col 3", "Col 4", "Col 5" ]
    [/code]

    You Javascript code would look like:

    [code]

    $.getJSON('/path_to_your_server_side_script.php'),function(data)
    {
    //Create the table
    tablehtml = '';
    $.each(data, function()
    {
    tablehtml += ''+this+'';
    }
    tablehtml += '';
    $('body').append(tablehtml);

    //Make it a DataTables
    $('#MyTable').dataTable();
    }

    [/code]
This discussion has been closed.