Clear data in server side ajax table

Clear data in server side ajax table

foxxfoxx Posts: 2Questions: 1Answers: 0

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

  • kthorngrenkthorngren Posts: 20,148Questions: 26Answers: 4,736
    edited April 2020 Answer ✓

    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 use destroy() when you are ready to clear the table the reinitialize with the same options which will use deferLoading to provide an empty table.

    Kevin

  • foxxfoxx Posts: 2Questions: 1Answers: 0
    edited April 2020

    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.

  • colincolin Posts: 15,118Questions: 1Answers: 2,583
    Answer ✓

    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 pass true to destroy(),

    Colin

  • DarckCrystaleDarckCrystale Posts: 7Questions: 2Answers: 0

    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.

  • kthorngrenkthorngren Posts: 20,148Questions: 26Answers: 4,736

    The clear() doesn't work with server side processing because the data displayed is controlled by the server side script not in the client. The clear() API is used with client side processing tables. You don't need to use jQuery empty() in the preXhr event as in the thread you linked. Just use it after the destroy(), like this:
    http://live.datatables.net/kanufalo/1/edit

    Kevin

Sign In or Register to comment.