Auto expandable rows and subrows react table using hooks
Asked Answered
M

2

3

Here I have code of react table using hooks, and i have struggled on task where I need to set all rows and subrows expanded by default when i open the page, I have tried several solutions, but all of them didn't work.

Link to the code : https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/expanding

Misfire answered 5/2, 2020 at 11:10 Comment(1)
It looks like that react-table has an option initialState.expanded to pass to useTable. github.com/tannerlinsley/react-table/blob/…Adallard
A
6

Just as I said, the option initialState.expanded allows you to expand the rows you want.

useTable(
    {
      columns: userColumns,
      data,
      initialState: {
        expanded: { "0": true, "2": true }
      }
    },
    useExpanded // Use the useExpanded plugin hook
  )

This will expand the first and the third row.

Adallard answered 5/2, 2020 at 17:50 Comment(3)
Thank you, sir, for your respond, really appreciate, and how can I make SUBROWS expandable in this case, do not you know? Cause decision above makes expandable only rows, but now subrows. For example, at the sandbox I have subrows for each row, and I need to expand all of them until the last child. Thanks!Misfire
and I cannot do like that : initialState: { expanded: { 0: true, 0.1: true, 0.1.0: true, }, }, },Misfire
You should do something like expanded: { 0: true, 0.1: true, 0.1.0: true, }, maybe you need to do a loop that go through each child to build the object expanded with all your rows/subrows. You don't have many choices, when you choose to use a library you are limited to its limits.Adallard
A
0

I'm not sure this is still relevant 4+ years later, but in the latest MRT V2 you can simply say:

initialState: {
  expanded: true
},

and just like that all your rows / subrows will start expanded. No need for clumsy loops over your data and such imperative hacks.

In the meantime, the codesandbox changed to https://codesandbox.io/p/devbox/github/TanStack/table/tree/main/examples/react/expanding

Enjoy!

Acuminate answered 2/7 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.