Clear data in server side ajax table
Clear data in server side ajax table
I have a datatable that loading is deferred on when initialised. Data is populated via ajax call as soon as the user searches.
I also have an action button that sets an id in another field.
After the action button has run, I would like to clear all the current results from the table, to give the user feedback they are 'done' with that table, but leave the table so if the user wanted they could search again and choose another action.
I understand I cannot use .clear() etc as the data is ajax, is there a simple way I can accomplish this, or obtain similar functionality ? Many thanks in advance.
This question has accepted answers - jump to:
Answers
Using ajax isn't the problem. It sounds like you may have server side processing (SSP) enabled. With SSP the server script is in control of the rows displayed in the table. The
deferLoading
option behaves like you mentioned with SSP. If this is the case then one option would be to usedestroy()
when you are ready to clear the table the reinitialize with the same options which will usedeferLoading
to provide an empty table.Kevin
Hmmm, that almost works perfectly, however not quite, I've stepped through it here :
1) When I destroy() the table, it leaves the html version of the table behind with the currently visible rows.
2) If I then recreate the dataTable over the top this still leaves those rows in place until you search again when they get cleared.
I suppose can empty/delete all the rows after destroying with some javascript, I'm just trying to keep this simple, clean and robust.
Many thanks for your assistance.
You can use
clear()
to remove all rows in the table. Or, if you want to remove the entire table from the DOM, you can passtrue
todestroy()
,Colin
As mentioned in the
clear()
documentation, it shouldn't be used when server side processing is activated.Someone suggested another solution here: Clear Table during ServerSide reload/redraw
I'm not really satisfied with it, but it works as intended.
The
clear()
doesn't work with server side processing because the data displayed is controlled by the server side script not in the client. Theclear()
API is used with client side processing tables. You don't need to use jQuery empty() in thepreXhr
event as in the thread you linked. Just use it after thedestroy()
, like this:http://live.datatables.net/kanufalo/1/edit
Kevin