How to deselect rows after ajax.reload call?

How to deselect rows after ajax.reload call?

jstuardojstuardo Posts: 104Questions: 41Answers: 0

Hello,

default behaviour of ajax.reload is to keep selected rows. However, I need the contrary, to deselect rows after ajax.reload completes.

I tried by calling table.rows({ page: 'current' }).deselect(); in draw.dt event, but it did not select anything.

Any help, please?

Jaime

Answers

  • kthorngrenkthorngren Posts: 21,170Questions: 26Answers: 4,922
    edited April 2020

    This thread has the same question.

    Kevin

  • jstuardojstuardo Posts: 104Questions: 41Answers: 0

    I'm afraid but it did not work.

    I have placed exacly the same code:

                                table.ajax.reload(function () {
                                    table.rows().deselect();
                                });
    

    But it did not deselect anything. In other place of the code, I am also using:

                               table.search(searchKeywords).draw();
    

    To load the grid with search data. After that, I am calling

                               table.rows().deselect(); 
    

    but it did not work either. I also tried to use the drawCallback but none of my attempts work.

    Please give a hint.

    Thanks
    Jaime

  • kthorngrenkthorngren Posts: 21,170Questions: 26Answers: 4,922

    Interesting, it doesn't work exactly as @allan suggested. It seems the table.rows().deselect(); statement is executed before the rows are actually reselected. See this example:
    http://live.datatables.net/calibuxe/1/edit

    The first button shows the default behavior. The second uses the code Allan provided. The third invokes a listener for the select event then uses table.rows().deselect();. Notice the order of the console statements is different than the second button.

    You can simply unselect the rows before calling ajax.reload() like the forth example.

    To load the grid with search data. After that, I am calling
    table.rows().deselect();
    but it did not work either.

    The last button shows that this works. I guess it depends on when/where you are using table.rows().deselect();. If you still have difficulties please post or update my test case so we can see the issue.

    Kevin

  • jstuardojstuardo Posts: 104Questions: 41Answers: 0

    @kthorngren Yes, as a workaround, I am calling table.rows().deselect(); before calling to .draw(); or .reload() methods. That way it works.

    However, I think DataTable should have a boolean parameter that one can specify if one want the selection to be preserved or not.

    I think this was implemented in DataTable in order to keep selection across multiple pages. That is cool, but it should consider the case when that feature is not wanted.

    By the way, in your example, when clicking on the second button, even when Selected rows after deselect = 0, the grid shows selected rows.

    Regards
    Jaime

  • kthorngrenkthorngren Posts: 21,170Questions: 26Answers: 4,922

    By the way, in your example, when clicking on the second button, even when Selected rows after deselect = 0, the grid shows selected rows.

    Its probably a race condition between the rows being reselected from ajax.reload() and deselecting them using table.rows().deselect();.

    However, I think DataTable should have a boolean parameter that one can specify if one want the selection to be preserved or not.

    Allan can answer for himself but I suspect the complications of adding a parameter to ajax.reload() would far out weigh just simply calling the deselect() API if you don't want it saved.

    Kevin

This discussion has been closed.