table icon indicating copy to clipboard operation
table copied to clipboard

row selection not working for nested rows (`getSubRows`)

Open csantos-nydig opened this issue 1 year ago • 15 comments

Describe the bug

row-selection in the group rows are not correctly reacting to their sub-rows row-selection state

Your minimal, reproducible example

https://codesandbox.io/s/row-selection-sub-rows-kfgh2j

Steps to reproduce

  1. Go to https://codesandbox.io/s/row-selection-sub-rows-kfgh2j
  2. Expand the first group row
  3. Select the first sub-row
  4. ✅ Notice the parent row checkbox being "indeterminate" image
  5. Select the other 2 sub-rows in that same group
  6. 🛑 Notice the parent row checkbox is "unchecked", when it should be "checked" image

Probably the same issue:

  1. Go to the same codesandbox: https://codesandbox.io/s/row-selection-sub-rows-kfgh2j
  2. Expand the first group row
  3. Select the checkbox from the column header
  4. ✅ Notice all rows (parent and children) are selected image
  5. Unselect the first sub-row of the expanded group
  6. 🛑 Notice the parent row checkbox is stuck on "checked", when it should be "indeterminate" image

Expected behavior

As a user, I expected the parent row to react to the row-selection status of its children, regardless on how they are selected/unselected:

  • some sub-rows selected: parent row "indeterminate"
  • no sub-rows selected: parent row "unchecked"
  • all sub-rows selected: parent row "checked"

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • Browser: N/A
  • OS: N/A

Happens in codesandbox

react-table version

v8.9.1

TypeScript version

N/A - whatever codesandbox uses

Additional context

No response

Terms & Code of Conduct

  • [X] I agree to follow this project's Code of Conduct
  • [X] 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.

csantos-nydig avatar May 25 '23 18:05 csantos-nydig

Also see: https://github.com/TanStack/table/issues/4720

@tannerlinsley could you please take a look at this 🥹🙏

I really want to use tabstack table for a project but this specific issue is preventing me from being able to use it.

This naive UI example here (cascade checking) has the behavior we are expecting:

https://www.naiveui.com/en-US/os-theme/components/tree#cascade.vue

VincEnterprise avatar May 30 '23 13:05 VincEnterprise

bump

VincEnterprise avatar Jun 07 '23 15:06 VincEnterprise

I use this workaround until the bug is fixed (React):

useEffect(() => {
  const { rows } = table.getRowModel();
  rows.forEach((row) => {
    const allSuRowsSelected = row.subRows.length > 0 && row.subRows.every(({ getIsSelected }) => getIsSelected());
    if (allSuRowsSelected) {
      row.toggleSelected(true);
    }
  });
}, [rowSelection, table]);

artem-zaiko-yara avatar Jun 14 '23 10:06 artem-zaiko-yara

Link to fix: https://github.com/TanStack/table/issues/3965#issuecomment-1912872932

R-Bower avatar Jan 27 '24 00:01 R-Bower

bump, issue is actual

Current workaround is to toggle manually

  useEffect(() => {
    const { rows } = table.getRowModel();

    rows.forEach((row) => {
      if (row.subRows.length > 0) {
        if (row.getIsAllSubRowsSelected() === true) {
          row.toggleSelected(true);
        } else if (row.getIsSomeSelected() === false) {
          row.toggleSelected(false);
        }
      }
    });
  }, [rowSelection, table]);

Profesor08 avatar Jan 30 '24 09:01 Profesor08

Any updates on this issue? I'm planning to update from v7 -> v8 because v7 has a bug with isSomeSelected prop for nested rows, but looks like updating does not make any sense because a v8 still has a problem with checkboxes for nested rows...

#3965

VACincoming avatar Feb 10 '24 02:02 VACincoming

Just noticed the same issue :+1:

pelletier197 avatar Feb 28 '24 18:02 pelletier197

Yep, I've run into this issue too.

Macs89 avatar Mar 20 '24 13:03 Macs89

same issue here

minhdtb-tanaakk avatar Mar 27 '24 07:03 minhdtb-tanaakk

bump, issue is actual

Current workaround is to toggle manually

  useEffect(() => {
    const { rows } = table.getRowModel();

    rows.forEach((row) => {
      if (row.subRows.length > 0) {
        if (row.getIsAllSubRowsSelected() === true) {
          row.toggleSelected(true);
        } else if (row.getIsSomeSelected() === false) {
          row.toggleSelected(false);
        }
      }
    });
  }, [rowSelection, table]);

This is a great fix since the issue is still occurring, thank you! I noticed that the table.getIsSomeRowsSelected() handler isn't counting grouped rows that are being selected, so to fix this I fixed up a function you can use to check if any are selected in the grouped or subrows, hope it helps someone like it did for me.

function getIsAnyRowsSelected(table: Table<any>) {
    const rows = table.getExpandedRowModel().rows;
    const { rowSelection } = table.getState();

    let isAnyRowsSelected = rows.some(row => {
      if (rowSelection[row.id]) return true;

      if (row.subRows.some(subRow => rowSelection[subRow.id])) return true;

      return false;
    });

    return isAnyRowsSelected;
  }

cthomesmatt avatar Apr 03 '24 17:04 cthomesmatt

same issue

vladcos avatar Apr 17 '24 21:04 vladcos

Following this issue.

mttigg avatar May 08 '24 17:05 mttigg

same issue

xuyunan avatar May 30 '24 00:05 xuyunan

same issue

etiennecoyacabelio avatar Jul 18 '24 13:07 etiennecoyacabelio