Server-side DataTables and manipulating the source data and reloading the data in one call

Server-side DataTables and manipulating the source data and reloading the data in one call

jameshojamesho Posts: 5Questions: 0Answers: 0
edited November 2011 in General
So I've been combing through the API and examples and haven't found a way to do this, but here's a sample use case.

You have a DataTable, whose data is loaded via JSON by a server. Each row of the DataTable has a checkbox which you can click on or off.
Above the table, lies a button, "Delete selected rows" whose purpose is to issue an AJAX call to the server and delete the selected rows.
Sometimes, some of the selected rows will not be "delete-able" and therefore an error message will need to be shown, ideally below the offending row.

Now, it's easy to submit the necessary information to the server about which rows to delete, however, how does one refresh the table? And more importantly, how do you do this without doing two round trips to the server - ie. one to delete the selected rows, and one to refresh (fnDraw) the table (which won't easily handle showing error messages as each roundtrip to the server is a separate transaction).

Any thoughts?
James

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    1) make ajax call to server with list of ID's to delete
    2) server replies with list of ID's not deleted, or ID's of deleted, or both
    3) use the not-deleted list to signal which ones were not deleted
    4) use fnDeleteRow to remove rows where ID's have been deleted.
  • jameshojamesho Posts: 5Questions: 0Answers: 0
    Did you know that when you called essentially any API method on the DataTable, that it essentially calls fnDraw() which will hit the server yet again?

    So, in other words, calling fnDeleteRow will result in a second server call.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    setting param 3 to false will avoid a redraw with fnDeleteRow
  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin
    If you are using server-side processing, then yes anything that causes the table to draw will cause an XHR to be fired off. One thing to note about server-side processing is that fnDeleteRow is not particularly appropriate since it is dealing with client-side data only, and obviously you are using server-side data. fbas's solution will certainly work, but it doesn't take into account paging / filtering I think - since you might end up with a page that has only 9 rows but a paging length of 10 (i.e. you've deleted one row).

    So - if you want to delete rows from the table but only make one XHR (entirely fair enough :-) ), what you would need to do is customise fnServerData or fnServerParams to send the extra information you need to make the server do whatever it needs to do (i.e. a delete flag and the row ideas to delete), modify the server script to do that and then complete the rest of the redraw request as normal.

    Allan
This discussion has been closed.