Filter parent sub rows and display all children #6128
Unanswered
BrendanC23 asked this question in Q&A
Replies: 1 comment
-
| Yes, you can achieve this with a custom The key is to write a filter function that checks whether the current row OR any of its ancestors match: import { useReactTable, getCoreRowModel, getFilteredRowModel, getExpandedRowModel, type FilterFn, } from '@tanstack/react-table' const includeChildrenFilter: FilterFn<any> = (row, columnId, filterValue) => { const cellValue = String(row.getValue(columnId) ?? '').toLowerCase() const search = String(filterValue).toLowerCase() // This row directly matches if (cellValue.includes(search)) { return true } // Check if any ancestor matched — if so, include this child let parent = row.getParentRow() while (parent) { const parentValue = String(parent.getValue(columnId) ?? '').toLowerCase() if (parentValue.includes(search)) { return true } parent = parent.getParentRow() } return false } const table = useReactTable({ data, columns, state: { globalFilter, expanded: true, // auto-expand all rows so children are visible }, getSubRows: (row) => row.subRows, globalFilterFn: includeChildrenFilter, getCoreRowModel: getCoreRowModel(), getFilteredRowModel: getFilteredRowModel(), getExpandedRowModel: getExpandedRowModel(), })How it works:
You can also apply this per-column instead of globally by setting |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using TanStack table's sub-rows feature to display nested rows below the main row. If a filter displays the parent row, I would like to have the child rows also be shown.
When I use the expanding CodeSandbox example, if I type "Woodrow" in the filter input, Woodrow is shown but the children are hidden. Instead, I would like all of the children to be shown.
Is there a way to accomplish this?
Beta Was this translation helpful? Give feedback.
All reactions