Repository navigation
How to select all sorted rows between currently selected and a given row? #6000
Replies: 1 comment
|
For v8, use the positions in For a flat table: import type { Table } from '@tanstack/table-core';
function selectRange<TData>(
table: Table<TData>,
anchorId: string,
targetId: string,
) {
const rows = table.getRowModel().rows;
const anchorIndex = rows.findIndex(row => row.id === anchorId);
const targetIndex = rows.findIndex(row => row.id === targetId);
if (anchorIndex < 0 || targetIndex < 0) return;
const start = Math.min(anchorIndex, targetIndex);
const end = Math.max(anchorIndex, targetIndex);
table.setRowSelection(previous => {
const next = { ...previous };
for (const row of rows.slice(start, end + 1)) {
if (row.getCanSelect()) next[row.id] = true;
}
return next;
});
}On an ordinary click, store I tested this with The row models guide explains the processing order, and the selection guide covers storing selection by row ID. |
Uh oh!
There was an error while loading. Please reload this page.
I have a sortable and filterable table with many rows. Selecting individual rows with
row.toggleSelected()works fine. I want to implement a standard shift-select functionality, where clicking on a row, then shift clicking another row, selects all rows inbetween.So how would I write the implementation of this function?
Note: I don't want to just select rows by ID between the currently selected row's ID and the newly selected one's, as they are sorted and filtered, so the rows displayed in the table go in some arbitrary sequence of IDs like
[ 12, 4, 15, 0, 5 ... ]).All reactions