Cancel the table draw when error or '204 No content' response is received

Cancel the table draw when error or '204 No content' response is received

manikantamanikanta Posts: 7Questions: 0Answers: 0
edited September 2011 in DataTables 1.8
Hi,

I m using DataTables in my new project which is backed by Backbone.js, and so obviously the datatable should be server-side driven.
I've customized the fnServerSide() and it is working quite fine. I've also implemented few CRUD operations like ADD, UPDATE, DELETE.

My implementation flow goes like this - for ex, DELETE:
1. Clicking 'Delete', it will make a call to fnDraw() which in turn calls fnServerData() (as the datatable is server-side driven: bServerSide: true). Before calling the fnDraw(), I m pushing the action details (like the action type, DELETE here, and the action related data, like the row id) into some variable (I couldn't find a better solution :( )
2. In fnServerSide(), it gets the action data from the above variable (using the variable like queue) and gets/modifies the data from Backbone collections instead of directly contacting the server. Backbone' fetch() will do the actual server calls. Here itself I m customizing the pagination meta as required by DataTables.
3. Once the Backbone' fetch/create/destroy/save methods return, I m calling the fnServerData' callback function to report the table that process has completed and now it will comes out from processing state (where the table freezes).


For any of these options, if the service returns some error (like 403, 404, 405) or '204 No content' (when the record detailed info is not available) type response, I want to cancel the table draw so that the existing data will be as it was. Otherwise I need to give the previous data to the datatable (which actually is not that difficult as the Backbone' collection has that data; but I want to stop the unnecessary processing again).

fnPreDrawCallback() seems not the correct function to use for this requirement or is it the correct function? Please suggest.

Thanks,
ManiKanta G

Replies

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin
    Hi ManiKanta,

    The Ajax handler that DataTables uses internally, just uses the jQuery 'success' callback - so any 2xx return will result in that being called. So what you need to do I think, is to override the way DataTables makes the Ajax call ( http://datatables.net/ref#fnServerData ) and put in a check for the 204 return status code. If you find that, then simply don't call the draw callback function, otherwise do and give it the data (note I've not actually tried this, but I think it should work!).

    Allan
  • manikantamanikanta Posts: 7Questions: 0Answers: 0
    Thanks Allan.

    [quote]allan said: If you find that, then simply don't call the draw callback function, otherwise do and give it the data[/quote]

    If I didn't call the callback fn, the table is stays in freeze state (I mean, in still processing)

    ManiKanta
  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin
    Oh I see yes - it wasn't exactly designed to do that :-). What you could do is just give it the result of fnGetData() which will feedback the current data from the table! The alternative would be to "clean" up the draw, such as removing the processing box and so on, which is equally possible, it requires knowing what functions to run from the code.
  • manikantamanikanta Posts: 7Questions: 0Answers: 0
    Thanks Allan

    [quote]allan said: The alternative would be to "clean" up the draw, such as removing the processing box and so on, which is equally possible, it requires knowing what functions to run from the code.[/quote]

    Can you elaborate a bit more on this? Do you mean, removing the processing element from DOM using jQuery or JS calls or DataTables has a API method for that? I couldn't find such a method.

    ManiKanta
  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin
    You would need to call _fnProcessingDisplay for certain ( https://github.com/DataTables/DataTables/blob/master/media/js/jquery.dataTables.js#L5376 ) and set bDrawing ( https://github.com/DataTables/DataTables/blob/master/media/js/jquery.dataTables.js#L3209 ) to false. Its possible there are something things to clear up, but I think that is is. If you'd like me to write a function for you to do it, feel free to hit the 'Support' option at the top of the page :-)

    Allan
This discussion has been closed.