Default expand subRows #454
Replies: 3 comments 4 replies
-
This is the example what I found:
In the expanded property i would like to set something like this: expanded: { 0: true, 1: true, subRows: { 0: true } } |
Beta Was this translation helpful? Give feedback.
-
This question inspired me to whip up a quick example. You just need to map through your data and find all of the row ids that you want to be expanded by default. Full example here: https://www.material-react-table.com/docs/guides/expanding-sub-rows#expand-root-rows-only-by-default const initialExpandedRootRows = useMemo<MRT_ExpandedState>(
() =>
data
.map((originalRow) => originalRow.id) //get all the root row ids, use recursion for additional levels
.reduce((a, v) => ({ ...a, [v]: true }), {}), //convert to an object with all the ids as keys and `true` as values
[],
);
return (
<MaterialReactTable
columns={columns}
data={data}
enableExpanding
getRowId={(originalRow) => originalRow.id}
initialState={{ expanded: initialExpandedRootRows }} //only expand the root rows by default
/>
); |
Beta Was this translation helpful? Give feedback.
-
Finally I figured out what was my mistake. I forget to add these property: Thank you for your help! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I found two types of default expanding: all rows or rows on the first level. How can I default expand rows which is in the subRows. I would like to default expand the first and the second level (first subRows) rows and expand=false on the other subRows levels.
Beta Was this translation helpful? Give feedback.
All reactions