Do i really need td elements if sAjaxSource is used?

Do i really need td elements if sAjaxSource is used?

TheLexusTheLexus Posts: 4Questions: 0Answers: 0
edited March 2012 in General
Hello there,

i want to use sAjaxSource. The json data is loaded from the server but my table is always empty, i only see the "Showing 1 to 2 of 2 entries" Message, the search box and the show entries combobox. Whats wrong here?

Do I really need to add tbody tr td by myself or should this be done by datatables.

Sorry for that question, but it seems a little bit strange to me that data is fetched from server but i need to add everything by myself... (if this is the case).

Replies

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    > Do I really need to add tbody tr td by myself or should this be done by datatables.

    DataTables will do it for you if you don't give the table a tbody.

    Can you use the debugger please ( http://debug.datatables.net ) so I can see what is in the table? Also a link would be useful.

    Allan
  • TheLexusTheLexus Posts: 4Questions: 0Answers: 0
    edited March 2012
    Hi Allan,

    i first want to describe what i want todo.

    I create a json file on a server with this format:

    [code]
    {
    "columns":["","Col1","Col2","Col3","Col4","Col5","Col6"],
    "data":[
    ["1","Data1","Data2","Data3","Data4","Data5","Data6"]
    ]
    }
    [/code]

    I use the following html/js to show the datatable:

    [code]


    jQuery(document).ready(function() {
    jQuery('#result').dataTable({
    'fnServerData': function( sSource, aoData, fnCallback )
    {
    jQuery.getJSON( sSource, aoData, function (json)
    {
    var _table = jQuery('#result');
    var _data = processJsonReport(_table, json);
    fnCallback(_data);
    })
    },
    'bProcessing':true,
    'sAjaxDataProp':'data',
    'sAjaxSource':/path/to/json/file'
    });
    });

    [/code]

    The processJsonReport function does add the thead:

    [code]
    function processJsonReport(table,json)
    {
    table.empty();

    var _tablecontent = "";
    // append theads to the table
    if (json.columns)
    {
    _tablecontent = "";
    for(var _i=0; _i
  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    Okay - at the moment DataTables does not support dynamic columns. That is something I'm going to address with 1.10, but that might be a little way off... What you need to do at the moment if you want to define your columns in the json response is to initialise DataTables inside your json response handler function.

    > PS: The debugger seems not to want to play with me :(

    It might not like the idea of the DataTable breaking halfway through the initialisation - which is what is happening since there are no columns initially.

    Allan
  • TheLexusTheLexus Posts: 4Questions: 0Answers: 0
    edited March 2012
    Thanks for your answer, i think i get it:

    [code]
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">






    function processJsonReport(table,json)
    {
    table.empty();

    var _tablecontent = "";
    // append theads to the table
    if (json.columns)
    {
    _tablecontent = "";
    for(var _i=0; _i
  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    Yup - bob on :-)

    Allan
This discussion has been closed.