Reloading the entire table via ajax

Reloading the entire table via ajax

drmalex87drmalex87 Posts: 2Questions: 0Answers: 0
edited April 2014 in General
I'm building an admin tool in WordPress and on the backend I'm loading the default table successfully with DataTables.

I then have these filter options that make an ajax call and return an entirely new html table with the filtered data.

[code]
$.get(ajaxurl, save_scope, function(response) {
table.html(response);
$('.return-table').DataTable();
});
[/code]

DataTable() isn't rendering on the newly loaded table. Am I doing this right? Do I have to go back and re-work my ajax call to return a JSON object or something? I'm not that fluent with js.

Replies

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin
    Are you just changing the data in the table? If so, use the fnReloadAjax plug-in (in DataTables 1.9-, or the built-in `ajax.reload()` method in 1.10+). If you can avoid destroying and rewriting the old table, you would be better off doing that.

    Failing that, please link to a test case showing the issue.

    Allan
  • drmalex87drmalex87 Posts: 2Questions: 0Answers: 0
    Hm, I can't easily share the example. I'm using 1.9 and I downloaded the plug-in repo and loaded the fnReloadAjax.js file, however I'm getting an error:

    [quote]object is not a function[/quote]

    in jquery.dataTables.js on line 4713

    On first load, I'm just loading a table and having dataTables run on it, no problem. Then I have a filter button and I altered my code to return a JSON array which outputs:

    [code][["","Post","","","Last Verified","Piwik Visits"]][/code]

    I'm trying this for starters as they are my headers.

    I'm directly inserting this ajax response in the fnReloadAjax:

    [code]
    $.get(ajaxurl, save_scope, function(response) {
    table.DataTable().fnReloadAjax(response);
    });
    [/code]

    I'm probably not seeing this right...hm
  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin
    fnReloadAjax will make the Ajax call for you. The first parameter you give it is the url it will get the data from (you can not give the first parameter and it will use the existing url).

    Allan
This discussion has been closed.