Can you initialize the datatable with JSON data when serverside = "true"?

Can you initialize the datatable with JSON data when serverside = "true"?

jameshojamesho Posts: 5Questions: 0Answers: 0
edited November 2011 in General
I noticed in one example (http://www.datatables.net/release-datatables/examples/server_side/defer_loading.html) that:
"When using DataTables with server-side processing the default behaviour is to have DataTables automatically go the server and load the data, removing anything which might already be on the page. However, this behaviour might not always be desirable when the first page of the table has already been preloaded in the HTML."

Is it possible to do this without preloading the data in HTML? More specifically, what I want to be able to do is to pass in JSON directly to the DataTable via fnAddData(). Unfortunately fnAddData() hits the server when server side processing is turned on.

The reason behind this is to not have to reproduce client-side code with HTML and JSON. It would be nice to use JSON for everything, even on initial Page Loads!

Hope that made sense... any thoughts?

James

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    Can't say I understand the rationale (I have a cold so my head is pretty foggy)

    but I wonder if you could use ajax/json to retrieve data, put it into aaData, then initialize your datatable with that, with iDeferLoading and bServerSide

    something like:
    [code]
    var mydata; // hold json response

    $.ajax({
    async: true,
    url: url,
    data: data,
    success: function(jsondata) { mydata = jsondata; }
    dataType: 'json'
    });

    $('#yourtable').dataTable({
    bServerSide: true,
    sAjaxSource: "your_db_url.php",
    iDeferLoading: 10,
    aaData: mydata
    });
    [/code]
  • jameshojamesho Posts: 5Questions: 0Answers: 0
    Thanks, that does solve this particular issue!
This discussion has been closed.