Przemek
Przemek

Reputation: 6700

Retrieving specified rows in Datatables

The question

Is there a way to programmatically retrieve only the rows witch match some specific criteria - for example rows having specific indexes?

The API has a method - rows(), which results can be manipulated a little by providing a selector-modifier argument. However, using it I am able to use the current table search at most, not a custom one.

The problem

For better understanding, I'll provide more details about what I am trying to achieve.

I am using the Select extension. I would like to select some rows at once using an external trigger. Now I do it like this:

myTable.rows().every(function () {
    // ...
    // Check if the row should be selected and if so:
    this.select();
    // ...
});

However, this causes firing of the same number of select events on the table as the number of selected rows. I have a handler bound to this event, which performs a request to an external API, based on the selected rows. Since I cannot discern which select was the last one, my handler ends up spamming the external API needlessly because of the number of events.

Solutions

  1. Retrieve only matching rows and use rows().select() on them.

I think that would be the best solution, because the select event would be fired only once. However, I have no idea how to do it - hence this question.

  1. Prevent the handler from calling the external API until the selecting process is finished.

I thought up two hacky solutions which I'll use as a last resort:

I hope the whole question isn't too confusing. To be clear - the thing I would like to find an answer for is the question on the top, in italics.

Upvotes: 1

Views: 55

Answers (1)

MasNotsram
MasNotsram

Reputation: 2273

The selector-modifier is only for when you're selecting all rows. It's not for limiting the rows by a selector. For that you would use the row-selector, which is much more flexible:

https://datatables.net/reference/type/row-selector

There are several examples on that page so I won't go through the options here.

As you suggest is the case, if you use this selector and then call .select() on the returned rowset, the callback will only fire once.

Upvotes: 1

Related Questions