Skip to content

Table getIsSomeRowsSelected() does not consistently return correct value when column filter is applied #6079

@kahopfer

Description

@kahopfer

TanStack Table version

v8.21.3

Framework/Library version

React v18.3.1

Describe the bug and the steps to reproduce it

When using TanStack Table with row selection and filtering, the table getIsSomeRowsSelected() function does not consistently return true when some visible rows are selected.

Steps to reproduce:

  1. Create a table with row selection and filtering
  2. Select several rows.
  3. Apply a filter so only a subset of the selected rows are visible.
  4. Click the header checkbox to select all of the filtered rows.
  5. Begin unselecting the filtered rows from the top and observe that the header checkbox becomes unselected instead of indeterminate, indicating that getIsSomeRowsSelected() is returning false when it should be returning true.

This appears to be caused by the way the table.getIsSomeRowsSelected() is implemented. This is the current function implementation:

table.getIsSomeRowsSelected = () => {
  const totalSelected = Object.keys(
    table.getState().rowSelection ?? {}
  ).length
  return (
    totalSelected > 0 &&
    totalSelected < table.getFilteredRowModel().flatRows.length
  )
}

The issue with this approach is that it checks if the total number of selected rows (across the whole table, not just the filtered rows) is greater than 0 and less than the number of filtered rows. If you select rows that are filtered out, they will still count towards totalSelected. So if you have 3 visible rows and 2 selected (and also 2 more selected that are filtered out), totalSelected would be 4, which is greater than the number of visible rows (3), thus causing this function to return false, even though some filtered rows are selected.

Your Minimal, Reproducible Example - (Sandbox Highly Recommended)

https://tanstack.com/table/latest/docs/framework/react/examples/row-selection

Screenshots or Videos (Optional)

getIsSomeRowsSelected.bug.mov

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

Yes, I am also opening a PR that solves the problem along side this issue

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions