Delete row with table.row('.selected').remove().draw( false );

Delete row with table.row('.selected').remove().draw( false );

kaluosikaluosi Posts: 19Questions: 6Answers: 0

Trying to implement a button in a datatable column I am using the method in example:

https://www.datatables.net/examples/api/select_single_row.html

On the button click event I select the row with: $("#btnID).parent().parent().addClass('selected'); It works and the row is selected

Then I make an Ajax call to delete the record in the database. The record is deleted and then in the success section of an ajasx call I use: table.row('.selected').remove().draw(false); to delete the row in the table. That is not working. The row is not deleted and after that the buttons in the datatable are not working any more.

¿Any clue?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,364Questions: 26Answers: 4,777

    The row is not deleted and after that the buttons in the datatable are not working any more.

    If your page stops working then I would start by looking for errors in your browsers console. Do you find any?

    Kevin

  • kaluosikaluosi Posts: 19Questions: 6Answers: 0

    No, there are no errors in the console.

    It is this line: table.row('.selected').remove().draw(false); that cause the problem. Commenting it and everything works as expected (except that the row is not deleted, of course)

  • allanallan Posts: 61,822Questions: 1Answers: 10,127 Site admin

    And if you leave it in the code following it doesn't execute? That sounds a lot like a Javascript exception is happening.

    We'd really need a link to the page showing the issue.

    Allan

  • kaluosikaluosi Posts: 19Questions: 6Answers: 0

    Thank you for your answer kthorngren and allan.

    Yes, the code following that line execute. I put an alert() after that line and it pop up with out problems.

    More information to add is datatables is working server side in this case.

    There is a strange thing with the delete buttons. I have to put the click event inside a $(window).load(function() { ... }, to be sure it is executed after all $(document).ready(function() { } is executed. Maybe the button is created after the event handler is read and then the $(".btnBorrar").click(function (event) { ... } have to be read after the buttons have been created.

    I'am working to put the page on line for you to see it. I am again with you in a moment

  • kaluosikaluosi Posts: 19Questions: 6Answers: 0
    edited June 2017

    You can see the page in: http://www.laurisilva.net/datatables/

    Thank you!!

  • kaluosikaluosi Posts: 19Questions: 6Answers: 0

    Some more information.

    The row is deleted but the datatables is not redraw. If I manually reload the page (F5 or Ctrl-R) the row has disappear and buttons works as expected. It seems it is draw(false) what is not working.

  • allanallan Posts: 61,822Questions: 1Answers: 10,127 Site admin
    Answer ✓

    You are using server-side processing. The client-side methods such as row.add() and row().remove() are effectively meaningless when server-side processing is used, since the data store isn't held at the client-side.

    Use draw() to redraw the table which will fetch the latest data.

    Allan

  • kaluosikaluosi Posts: 19Questions: 6Answers: 0

    At the end I have to deleted the row with plain jquery $(".selected").remove(); but then I have to put pipeline number of pages to 1 (pages: 1). Otherwise the rows deleted remains in the cache and draw() redraws but not erase the rows from the cache.
    During the resolution of this problem I have been able to verify that there are many questions in the forums related to this subject. It seems that datatables do not finish having well resolved their operation with server side processing. I think all that would need a breakthrough in the api for datatables to work properly with server side (90% of use cases).

    Any way, I would like to thanks you for your kindly help and for this superb software development that is datatables

  • allanallan Posts: 61,822Questions: 1Answers: 10,127 Site admin

    but then I have to put pipeline number

    Wait - you are using pipeling as well? That would have been useful to know in the first place :). This is why the forum rules ask for a test case so we can get all the information we need.

    The pipelining demo caches data - that's the whole point of it. So you'd need to clear out the pipeline otherwise it will just used the cached data.

    The pipeline demo includes an API plug-in method called clearPipeline() which you can call to clear out the cache.

    It seems that datatables do not finish having well resolved their operation with server side processing.

    Perhaps that is so, although I don't believe it to be the case. If I'd understood correctly and you are using pipeling, then the issue in this case was that the cache wasn't being cleared.

    Part of the problem with the issues raised in the forum is that they are all different. Again, that's why a test case is so welcome.

    Allan

This discussion has been closed.