How to detect one item in filtered list after search/filter
How to detect one item in filtered list after search/filter
I would like to immediately set key focus on a row if it is the only row left after using the search/filter box.
I have tried the following:
$("div.dataTables_filter input").keyup(function (e) {
info = viewTable.page.info();
if (info.recordsDisplay == 1) {
viewTable.cell(viewTable.row({ search: "applied" }), 0).focus();
}
});
In this case, it seems that the result is not yet filtered, so I get the previous count of records. After doing some reading, I tried this next:
viewTable.on('search.dt', function () {
info = viewTable.page.info();
alert("number of rows:" + info.recordsDisplay);
});
I get similar results despite what I understood from the documentation was different:
Note that the search will be fired before the table has been redrawn with the updated filtered data, although the data will internally have been filtered.
So, my question is, how do I get the number of remaining rows immediately after a typing into the search box? How would I access the "internally filtered data" the documentation mentions to get a count of rows?
Thank you
This question has an accepted answers - jump to answer
Answers
Use the
selector
modifier of{search:'removed'}
. For example:Kevin
Hi Kevin,
Thank you for answering so quickly (and on a weekend ).
I may be using it wrong - when I use count = viewTable.rows( {search:'removed'} ).count();, the result is always 0.
Thank you
It works here:
http://live.datatables.net/canosofu/1/edit
If this doesn't help please provide a link to your page or a test case (update mine if you want) replicating the issue so we can debug.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
I am obviously doing something wrong. Question - does it make a difference if I am set up for serverside? I am not sure how to set up a test case for that...
Thank you
Hi Kevin, I found this in the documentation:
Special note on server-side processing: When using DataTables in server-side processing mode (serverSide) the selector-modifier has very little effect on the rows selected since all processing (ordering, search etc) is performed at the server. Therefore, the only rows that exist on the client-side are those shown in the table at any one time, and the selector can only select those rows which are on the current page.
It appears that this technique does not work when using server side. It looks like the row count happens before the search results are returned from the server which is similar to the problems I have been having all along with the other things I've tried. Is there a technique to get the count of rows for the current search once the server actually sends back the results to display?
Thank you
Yes many of the Datatables APIs only work with client side processing. You can use
page.info()
. Looks like you can takerecordsDisplay
and subtract fromrecordsTotal
to get the number of rows filtered out.You will probably need to use the
draw
event instead ofsearch
. Thesearch
event probably runs before the server side response.Here is a server side example:
http://live.datatables.net/zipejuwe/1/edit
Kevin
Many thanks, Kevin - for the server side situation, the draw event allows us to get the required information after the search is complete.