Reputation: 6700
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
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.
I thought up two hacky solutions which I'll use as a last resort:
Not use an avent handler at all and simply call the API after the rows().every()
loop - I still have to use the handler to deal with manual row selection, but that'll probably do.
Iterate through the rows twice - first time to check which row would be the last to be selected, then disable the handler, do a second loop which actually selects the rows, and enable the handler before selecting the last row
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
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