Look up row index from ID
How can I look up the actual rendered row index (after sorting, filtering etc) for a row?
I can't see any other solution than calling (a potentially slow) findIndex on table.getRowModel(), since the index field on the row unfortunately corresponds to the index in the original passed-in data array.
Is there no API for this?
5 Replies
ratty-blush•3mo ago
It sounds like you’re looking to pass the data ID to track the row ID.
You need this: https://tanstack.com/table/latest/docs/guide/row-selection#useful-row-ids
Row Selection Guide | TanStack Table Docs
Examples Want to skip to the implementation? Check out these examples: API Row Selection Guide The row selection feature keeps track of which rows are selected and allows you to toggle the selection o...
ratty-blushOP•3mo ago
I'm using that already for giving each row a stable uid. But I also need to work with plain indices at times, e.g. to implement shift-click to select a range.
As far as I can tell, this is something I have to do externally to TanStack by maintaining my own
rowId2SortedIndexMap
type thing.ratty-blush•3mo ago
Oh ok. Well row selection should be based off of that same stable uid. So if you need to implement row range selection then using the existing ID's should work. Your range selection logic should essentially just update the rowSelection array.
ratty-blushOP•3mo ago
Sure, but I can't calculate what the new rowSelection should be without the indices.
ratty-blush•3mo ago
I have no code in front of me but…
Grab the first selected id and identify the row element, tag the last selected id.
Iterate thru each row and toggle selection to true until you hit the row with the matching id you tagged above.
Something like that?