Skip to content
Source
Sign inSign up
TanStack/table

Column visibility APIs do not work with column groups #5497

Closedashacs(GitHub profile, opens in a new tab) opened this issue 5 comments

TanStack Table version

v8.15.2

Framework/Library version

React v8.15.2

Describe the bug and the steps to reproduce it

When using column helpers to create a column group, consumers are able to provide enableHiding: true as a column def option. However:

  1. column.getToggleVisibilityHandler() or column.toggleVisibility always updates the table's columnVisibility state to have a false entry for that column group's id
  2. column.getIsVisible() always returns true for that column group

Ideally, the getToggleVisibilityHandler, toggleVisibility, getIsVisible, and related HeaderGroup APIs could all be updated to support visibility toggling on column groups (which in turn would show or hide the children columns of that column group). Worst case, if toggling visibility of groups isn't something that can be supported, the enableHiding column def option could be removed from the column helpers API so it's not misleading to consumers.

https://codesandbox.io/p/devbox/dry-river-9t7pg9

Screenshots or Videos (Optional)

This video uses the linked Code Sandbox example which is almost identical to the example in the documentation, but instead of rendering the column leaves as visibility checkbox options it renders the column groups as options. When interacting with the checkbox options which should toggle the column visibility, the visibility state is always set to false and the checkbox is always checked.

https://github.com/TanStack/table/assets/10733340/f5a8fd29-0d71-494a-8190-a8588c1e6ccf

Do you intend to try to help solve this bug with your own PR?

None

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
  1. This still appears to be the case for 8.17.3

  2. Same case for me on vue v3.5 and @tanstack/vue-table v8.21.2

  3. vlausargs(GitHub profile, opens in a new tab)commented

    is there any workaround for this?

    edit: i guess i can manipulate columns array manually & combine it with derived & state, but i need to also maintain the column.getCanHide manually

  4. My workaround for this, if I understood correctly was this. I have a table with groups and buttons outside the table to toggle the groups visibility.

    I found that the toggleVisibility in react-table is like a reducer which spreads the existing state and then appends the new state for the column acted on. I just create an object with all the columns columns with the toggled value from the parents visibility. Then do the same.

    {table .getAllColumns() .filter((column) => column.getCanHide()) .map((column) => { return ( <Button key={column.id} onClick={() => { const cols = column.getLeafColumns().reduce((acc, c) => { acc[c.id] = !column.getIsVisible(); return acc; }, {} as Record<string, boolean>); table.setColumnVisibility((old) => ({ ...old, ...cols, })); }} > {column.id} ); })}

    https://github.com/TanStack/table/blob/66fbe3a74b129e700e4667d1d5e1b29dd0a7b723/packages/table-core/src/features/ColumnVisibility.ts#L172

  5. KevinVandy closed this

Sign in to join this conversation.