How do I reload DataTables with new JSON data?

How do I reload DataTables with new JSON data?

SashSash Posts: 5Questions: 0Answers: 0
edited May 2011 in General
Great plugin!

I guess the question is self explanatory.

I've created a reporting module - when a user clicks on a report, it renders the DataTable. So each time a user chooses a different report, DataTable will be rendered with fresh JSON data from the server (fresh aoColumns and aaData).

I'm having troubles achieving the RELOAD. It works first time but I don't know how to reload it with fresh data or essentially re-initialize the table.

Code below is how I load the data. Is there a way I can use the API to reload only the aoColumns and aoData but leave the rest of my settings intact ?

[code]
function getReport(reportId) {
$.post('/Services/Reporting.asmx/GetReport', { reportId: reportId }, function (response) {
var json = $.parseJSON(response.text);
setupTableFooter(json);
//oTable.fnReloadAjax(json);
otable = null;
var settings = {
"bJQueryUI": true,
"bProcessing": true,
"bRetrieve": true,
"sDom": '<"top"T<"clear">Rfrti<"clear">p<"clear">>',
"oTableTools": {
"sSwfPath": "/Scripts/datatables/media/swf/copy_cvs_xls_pdf.swf",
"sRowSelect": "single"
},
"sPaginationType": "full_numbers",
"bScrollCollapse": true,
"bScrollAutoCss": false,
"oColVis": {
"buttonText": " ",
"bRestore": true,
"sAlign": "left"
},
"oLanguage": { "sSearch": "Search all columns:" },
"aaData": json.aaData,
"aoColumns": json.aoColumns
}

oTable = $("#datatable").dataTable(settings);
});
}
[/code]

Replies

  • allanallan Posts: 63,810Questions: 1Answers: 10,516 Site admin
    Given the way you've implemented it, there are two options:

    1. Destroy the table and initialise it again
    2. Call the API method fnClearTable and then fnAddData to add you new data (I'd say this one is preferable).

    Allan
  • shlapshlap Posts: 3Questions: 0Answers: 0
    Thank you allan! I just spent a half hour trying to figure out which functions to call, these worked great.
This discussion has been closed.