get access to selected row data from outside table component #2155
Replies: 15 comments 29 replies
-
Hi, Right now I am doing it using useEffect and listening to [selectedFlatRows], which is triggering lot of issues. Sometimes it ends up in an infinite loop & checkbox on onchange row.getToggleSelectedRows() triggers/renders twice -> ends up having no selected rows. Let me know if you find any solution. |
Beta Was this translation helpful? Give feedback.
-
There are 2 days to do it in my opinion:
|
Beta Was this translation helpful? Give feedback.
-
I had an issue when trying to do this using a callback function (method num 2 in @GuptaSiddhant answer) and it was causing a re-rendering loop. Found out that it was because my table data was not 'memoized' |
Beta Was this translation helpful? Give feedback.
-
does anyone have a current example of how to make this work? Sandbox references don't seem to be working. TY |
Beta Was this translation helpful? Give feedback.
-
Is there any solution? Coz my data is dynamic data
and I can't leave dependency’s array empty. But Also I have to manipulate with selected data in a parent component. But get " Maximum update depth exceeded" when trying to set data to the state. "handleSelectRows" comes from the parent component. And code below in the react-table component.
|
Beta Was this translation helpful? Give feedback.
-
For particular row or table we can download CSV but whereas as to desc and
then download CSV can't possible..i think so
Kalpataru Sahoo
…On Tue 26 Apr, 2022, 10:08 AM Hamidreza Ghanbari, ***@***.***> wrote:
its my problem too...
—
Reply to this email directly, view it on GitHub
<#2155 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AWNFMHRR2PUJO6HOQVXLSVDVG5XN5ANCNFSM4MHOQIJQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
how can i achieve this with v8? |
Beta Was this translation helpful? Give feedback.
-
After two broken dev servers and hours in search of a way to do that, this seems to work… |
Beta Was this translation helpful? Give feedback.
-
works fine @8.9.1 Parent
Table component
|
Beta Was this translation helpful? Give feedback.
-
This is a way to get selected row datas for manual pagination (server-side pagination)
This way, when user selected a number of rows on page 1, the selected row data will be kept when user selects at another page. |
Beta Was this translation helpful? Give feedback.
-
UPDATE: Accessing the const selectedRows = table.getSelectedRowModel().rows.map(({ original }) => original); I tried to create a custom hook to handle rows selection like this. Sincerely don't know if it's a good practice at the moment... import { Row, Table } from '@tanstack/react-table';
import React, { useCallback, useState } from 'react';
export function useTableRowsSelection<T>() {
const [selectedRows, setSelectedRows] = useState<T[]>([]);
const onRowSelectionChange = useCallback(
(row: Row<T>) => (event: React.ChangeEvent<HTMLInputElement>) => {
row.getToggleSelectedHandler()(event);
if (!row.getIsSelected()) {
setSelectedRows((prev) => [...prev, row.original]);
} else {
setSelectedRows((prev) => prev.filter((item) => item !== row.original));
}
},
[],
);
const onAllRowsSelectionChange = useCallback(
(table: Table<T>) => (event: React.ChangeEvent<HTMLInputElement>) => {
table.getToggleAllRowsSelectedHandler()(event);
if (table.getIsAllRowsSelected()) {
setSelectedRows([]);
} else {
setSelectedRows(table.getPreSelectedRowModel().rows.map((row) => row.original));
}
},
[],
);
return {
selectedRows,
onRowSelectionChange,
onAllRowsSelectionChange,
};
} Then you can use it outside the table component: const { selectedRows, onRowSelectionChange, onAllRowsSelectionChange } = useTableRowsSelection<Data>(); And here my columns definitions. Attached const columns: ColumnDef<Data>[] = useMemo(
() => [
{
id: 'my-data',
header: ({ table }) => (
<Checkbox
{...{
checked: table.getIsAllRowsSelected(),
indeterminate: table.getIsSomeRowsSelected(),
onChange: onAllRowsSelectionChange(table),
}}
/>
),
cell: ({ row }) => (
<Checkbox
{...{
checked: row.getIsSelected(),
disabled: !row.getCanSelect(),
indeterminate: row.getIsSomeSelected(),
onChange: onRowSelectionChange(row),
}}
/>
),
size: 1,
},
...
],
[],
); |
Beta Was this translation helpful? Give feedback.
-
If you want you can use
|
Beta Was this translation helpful? Give feedback.
-
i ussing onRowSelectionChange
and useEffect with dependency rowSelectionState
|
Beta Was this translation helpful? Give feedback.
-
Old thread, but in case someone finds this useful, this was my solution: interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[];
data: TData[];
onRowSelectionChange: (rows: unknown) => void;
}
...
const [rowSelection, setRowSelection] = useState<RowSelectionState>({});
const handleRowSelectionChange: OnChangeFn<RowSelectionState> = (state) => {
setRowSelection(state);
};
useEffect(() => {
onRowSelectionChange(rowSelection);
}, [rowSelection, onRowSelectionChange]);
const table = useReactTable({
data,
columns,
onRowSelectionChange: handleRowSelectionChange,
getCoreRowModel: getCoreRowModel(),
state: {
rowSelection,
},
});
...
|
Beta Was this translation helpful? Give feedback.
-
After hours of attempting and wanting to avoid Here is what I came up and I hope it helps you guys out there ...,
const [rowSelection, setRowSelection] = React.useState<RowSelectionState>({});
...,
const handleOnRowSelectionChange = React.useCallback(
(valueFn: Updater<RowSelectionState>) => {
if (typeof valueFn === "function") {
const updatedRowSelection = valueFn(rowSelection);
setRowSelection(updatedRowSelection);
// Get all selected rows based on the updatedRowSelection
const selectedRows = Object.keys(updatedRowSelection).reduce(
(acc, key) => {
if (updatedRowSelection[key]) {
const row = data.find((row) => row.id.toString() === key);
if (row) {
acc.push(row);
}
}
return acc;
},
[] as StepOutput[],
);
// Call the onRowSelectionChange function with the selected rows
onRowSelectionChange?.(selectedRows);
}
},
[data, onRowSelectionChange, rowSelection],
);
...
const table = useReactTable({
...,
onRowSelectionChange: handleOnRowSelectionChange,
....
});
...,
return (
<>
...,
<SomeAwesomeTableName table={table} columns={columns} />
...
</>
) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a common component for the table which is being used in different pages through the app. Now the selection of rows is being saved inside the table component itself. I want to access the selected rows of data from its parent component whenever button pressed
Here is an example
https://codesandbox.io/s/naughty-pond-3e5jp
Beta Was this translation helpful? Give feedback.
All reactions