Replies: 5 comments 13 replies
-
@nleroy917 have you found a way to implement multi select? |
Beta Was this translation helpful? Give feedback.
-
Don't use Create instead a new action in the internal React State Reducer and pass the row indexes you want to select or deselct in this format : { [index1]: boolean, [index2]: boolean .... } So Then you can save the last focused row and the last clicked row whi;le holding shift and pass all the indexes in between to that new action. Check also the dispatch method provided |
Beta Was this translation helpful? Give feedback.
-
I use the following for v8, it's a column definition that has inner state for last selected id: import { ColumnDef, Row } from '@tanstack/react-table';
import { Checkbox } from '@@/form-components/Checkbox';
export function createSelectColumn<T>(): ColumnDef<T> {
let lastSelectedId = '';
return {
id: 'select',
header: ({ table }) => (
<Checkbox
id="select-all"
checked={table.getIsAllRowsSelected()}
indeterminate={table.getIsSomeRowsSelected()}
onChange={table.getToggleAllRowsSelectedHandler()}
/>
),
cell: ({ row, table }) => (
<Checkbox
id={`select-row-${row.id}`}
checked={row.getIsSelected()}
indeterminate={row.getIsSomeSelected()}
onChange={row.getToggleSelectedHandler()}
onClick={(e) => {
if (e.shiftKey) {
const { rows, rowsById } = table.getRowModel();
const rowsToToggle = getRowRange(rows, row.id, lastSelectedId);
const isLastSelected = rowsById[lastSelectedId].getIsSelected();
rowsToToggle.forEach((row) => row.toggleSelected(isLastSelected));
}
lastSelectedId = row.id;
}}
/>
),
maxSize: 50,
};
}
function getRowRange<T>(rows: Array<Row<T>>, idA: string, idB: string) {
const range: Array<Row<T>> = [];
let foundStart = false;
let foundEnd = false;
for (let index = 0; index < rows.length; index += 1) {
const row = rows[index];
if (row.id === idA || row.id === idB) {
if (foundStart) {
foundEnd = true;
}
if (!foundStart) {
foundStart = true;
}
}
if (foundStart) {
range.push(row);
}
if (foundEnd) {
break;
}
}
return range;
} I know the getRange is ugly, would love suggestions how to clean it |
Beta Was this translation helpful? Give feedback.
-
@chiptus thanks for sharing that! very useful! Here's my version, with another version of the onClick={(e: MouseEvent<HTMLInputElement>): void => {
if (e.shiftKey) {
const { rows, rowsById } = table.getRowModel();
const rowsToToggle = getRowRange(rows, Number(row.id), Number(lastSelectedID));
const isCellSelected = rowsById[row.id].getIsSelected();
rowsToToggle.forEach((_row) => _row.toggleSelected(!isCellSelected));
} else {
row.toggleSelected();
}
lastSelectedID = row.id;
}} const getRowRange = (rows: Row<T>[], currentID: number, selectedID: number): Row<T>[] => {
const rangeStart = selectedID > currentID ? currentID : selectedID;
const rangeEnd = rangeStart === currentID ? selectedID : currentID;
return rows.slice(rangeStart, rangeEnd + 1);
}; |
Beta Was this translation helpful? Give feedback.
-
@LoicKairon This is not a good approach since it's not working with pagination |
Beta Was this translation helpful? Give feedback.
-
I've been trying to figure this out for a little bit. Is there a way to override the select behavior such that I can select multiple rows by holding shit? That is, select a row, then hold shift and select a row multiple rows down and then all the in-between rows will be populated. I found this on stack-overflow, but there isn't enough context really to figure it out.
Is there a way to implement this? How can I reach into the table and override the
toggleRowSelected
function to search for those "in-between" rows and select them if not selected.Thanks!
Beta Was this translation helpful? Give feedback.
All reactions