Search (or filter?) by index and redraw
Search (or filter?) by index and redraw
Hi, i know how to search for a value programmatically and redraw the DataTable so that it shows only rows containing the given value.
I do that like so: this.api().search('8000').draw();
But now i have the need to do the same but not use a value this time, but show only the row with the given id.
Each row in the DataTable has an id like so:
<tr id="198">
<td>8000</td><td>turnover</td>
</tr>
I know the index of the row with id 198 via: var rowIndex = this.api().row('#198');
But how to get the DataTable to only show the row with that index?
Via Google i saw people that would place the id in a separate column and use search for that, but i think there must be a more efficient and better way as every row already has an id and i know the index of the row that i want to show.
Maybe it's simple but I googled a long time, tried lots but none of 'em worked.
Any help or pointers are greatly appreciated, many thanks.
Replies
Hi Dave,
There are two options:
Allan
Hi Allan,
Thanks for the (speedy!) reply, ver much appreciated.
No.1: clear.
About no.2: i was hoping that DataTables would store an array or object of sorts which holds the relationship between the id of a row and its index so that one could efficiently filter/search the DataTable by providing that index (or id).
Do i understand correctly that there is no direct way to show only one row by providing an index for that row? (Or more preferred: by the id of that row)
The reason i'm asking is because i have an input field which value is '8000 - turnover' while it has an attribute with an the id for that value.
If the user opens a dropdown (which holds the DataTable) for that input field, that DataTable should only show one row: the 8000 one.
So a search, not by a value (because the value in the input value is different than in any of the DataTable columns) but by the index of that row. Or even more practical: by the id of that row.
If you use that first approach, the hidden column, you can search explicitly by the hidden column with
column().search()
, rather than across all columns withsearch()
. You can use regexs to make it more specific, i.e.^value$
, which would work with both global and column searches.Colin
Thx Colin (and Allan), i'll try that. Hopefully it'll work.
Maybe it's a thought for a next version of DataTables, adding the possibility to search for a row attribute(s) and show only that row / rows? Or maybe add the ability to access an array or object which holds the relation to the row id of a row and its index?
For now i'll try your solution.
You can get this relationship using a Search Plugin. See this example:
http://live.datatables.net/ruvizuku/1/edit
Among other parameters the search plugin provides the row index and the original row data (
rowData
). The original row data contains the row ID as an object and can be accessed usingrowData.DT_RowId
.Use the search plugin to search by row index or row id. In your search input event handler store the input value’s “ID" in a global variable then use the
draw()
API to run the plugin. In the plugin use the global variable compare to therowData.DT_RowId
to display or hide the row.If you want help with this then please build a simple test case that we can use to help apply your search input to the search plugin.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin