Deselect all rows across all pages?

Deselect all rows across all pages?

kaelon141kaelon141 Posts: 1Questions: 1Answers: 0

Hi there!

I'm currently using datatables.net together with the datatables-select plugin in serverside mode, to display data to the user. The user can select multiple trips across pages.

When an action is performed, I´d like to deselect all rows across all pages.

I couldn´t find any API documentation for doing this. Anybody able to help me out with this?

Answers

  • kthorngrenkthorngren Posts: 22,388Questions: 26Answers: 5,142

    AFAIK there is nothing built inot Datatables for this. See this thread for some examples of how to workaround deselect all with server side processing.

    @allan can comment on whether adding this capability to the select code is soemthign he is willing to do. Also I don't see docs for select.cumulative() which @allan can add if needed.

    Kevin

  • allanallan Posts: 65,489Questions: 1Answers: 10,877 Site admin

    When an action is performed, I´d like to deselect all rows across all pages.

    rows().deselect() should do that. Just call it without a rows selector:

    table.rows().deselect();
    

    If that doesn't help, could you explain a little more about what you are doing and looking for please? For example, when you say "Action" do you mean a DataTables operation such as search / sort / page, or do you mean an external action such as deleting rows on the DB or something?

    Allan

  • kthorngrenkthorngren Posts: 22,388Questions: 26Answers: 5,142

    @allan in this example using the below will only deselect the rows on the current page:

    $('#example').DataTable().rows().deselect();
    

    Kevin

  • allanallan Posts: 65,489Questions: 1Answers: 10,877 Site admin

    Many thanks! I see the problem now. The issue is that when in server-side processing mode, rows() will only select the rows on the current page. So the iterator in rows().deselect() will only deselect the rows currently visible.

    That's a problem for Select, since it stores rows from other pages (specifically the row id), so they can be reselected.

    What we need is a way for Select to deselect without using the rows() selector.

    I've come up with this little plugin API method to do that:

    DataTable.Api.register('select.clearAll()', function () {
      this.rows().deselect();
    
      return this.iterator('table', function (ctx) {
        if (ctx._select_set) {
          ctx._select_set.length = 0;
        }
      });
    }); 
    

    Call as:

    table.select.clearAll();
    

    and all rows that Select has selected, regardless of page, will be deselected.

    The second line is so that it will run the handlers on the current page to remove classes and things like that.

    Could you try that and let me know how you get on with it? Assuming it works well for you, I'll incorporate it into Select, as, in retrospect, it seems like a fairly obvious thing to have when working with server-side processing!

    Allan

  • kthorngrenkthorngren Posts: 22,388Questions: 26Answers: 5,142

    A similar plugin is demonstrated in this thread which seems to work. Need to remove the call todraw() though.

    Kevin

  • allanallan Posts: 65,489Questions: 1Answers: 10,877 Site admin

    Lol doh. I should have read the thread. It has made up my mind that I need to include that in the library though!

    Allan

Sign In or Register to comment.