From 34de242a1767056bdbc1e61a7b1322a27b4ecb8a Mon Sep 17 00:00:00 2001 From: Albert Nimtz Date: Thu, 26 Nov 2026 23:00:16 +0100 Subject: [PATCH 1/6] feat(table-core): add opt-in include/exclude row selection mode Tables that do not load every row (server-side pagination) could only select the loaded rows with "select all". The new `manualRowSelection` table option allows an `'exclude'` value of the new `rowSelectionMode` state, in which `rowSelection` holds the deselected row ids. - `toggleAllRowsSelected(true)` sets the mode to `'exclude'` and clears `rowSelection`; `toggleAllRowsSelected(false)` sets `'include'` and clears it. `resetRowSelection` resets both slices. - In `'exclude'` mode a row is selected when its id is not in `rowSelection`, so rows that load later show as selected. The row, page, range, sub-row, and selected-row-model APIs follow the mode. - New `table.getSelectedRowCount()`: `rowCount` minus the deselected rows in `'exclude'` mode, the number of ids in `'include'` mode. - New `table.setRowSelectionMode()`, `table.resetRowSelectionMode()`, and `onRowSelectionModeChange`. - When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, a row, range, or page deselect goes back to an empty `'include'` selection. Without `manualRowSelection` the mode is ignored and selection works as before. The `RowSelectionState` shape does not change. Refs https://github.com/TanStack/table/discussions/6658 Co-Authored-By: Claude Fable 5.2 --- .changeset/row-selection-exclude-mode.md | 5 + docs/framework/alpine/guide/row-selection.md | 68 + docs/framework/angular/guide/row-selection.md | 68 + docs/framework/ember/guide/row-selection.md | 68 + docs/framework/lit/guide/row-selection.md | 68 + docs/framework/octane/guide/row-selection.md | 68 + docs/framework/preact/guide/row-selection.md | 68 + docs/framework/react/guide/row-selection.md | 68 + docs/framework/solid/guide/row-selection.md | 68 + docs/framework/svelte/guide/row-selection.md | 68 + docs/framework/vue/guide/row-selection.md | 68 + .../react/kitchen-sink/src/routes/index.tsx | 1 + .../references/row-selection.md | 47 + .../row-selection/rowSelectionFeature.ts | 66 +- .../rowSelectionFeature.types.ts | 85 +- .../rowSelectionFeature.utils.ts | 448 +++++- .../row-selection/rowSelectionMode.test.ts | 1200 +++++++++++++++++ .../rowSelectionFeature.utils.test.ts | 92 ++ 18 files changed, 2579 insertions(+), 45 deletions(-) create mode 100644 .changeset/row-selection-exclude-mode.md create mode 100644 packages/table-core/tests/implementation/features/row-selection/rowSelectionMode.test.ts diff --git a/.changeset/row-selection-exclude-mode.md b/.changeset/row-selection-exclude-mode.md new file mode 100644 index 0000000000..b9d37a6d98 --- /dev/null +++ b/.changeset/row-selection-exclude-mode.md @@ -0,0 +1,5 @@ +--- +'@tanstack/table-core': minor +--- + +Add an opt-in include/exclude row selection mode for tables that do not load every row: with `manualRowSelection: true`, `toggleAllRowsSelected(true)` sets the new `rowSelectionMode` state to `'exclude'` and clears `rowSelection`, which then holds the deselected row ids, and the new `table.getSelectedRowCount()` returns `rowCount` minus those ids. diff --git a/docs/framework/alpine/guide/row-selection.md b/docs/framework/alpine/guide/row-selection.md index f1495ae465..1fbfb50811 100644 --- a/docs/framework/alpine/guide/row-selection.md +++ b/docs/framework/alpine/guide/row-selection.md @@ -220,6 +220,74 @@ const handler = row.getToggleSelectedHandler({ The interaction anchor is preserved across sorting, filtering, grouping, expansion, pagination, and data updates while its row id remains in the display order. If filtering or data replacement removes the anchor, the next Shift interaction falls back to an ordinary row toggle and establishes a new anchor. `resetRowSelection`, either select-all API, and `table.reset()` clear the anchor. Direct calls to `row.toggleSelected()` or `table.setRowSelection()`, and external controlled-state changes, do not establish or move it. +### Select All Rows of a Remote Table + +With server-side pagination, the table only has the rows of the current page. By default, `table.toggleAllRowsSelected()` selects only these loaded rows, and `table.getIsAllRowsSelected()` checks only these loaded rows. + +Set the `manualRowSelection` table option to `true` when "select all" must select all rows on the server. The table then reads the `rowSelection` state together with the `rowSelectionMode` state: + +- `'include'` (default) - `rowSelection` contains the ids of the selected rows. +- `'exclude'` - all rows are selected, and `rowSelection` contains the ids of the deselected rows. + +```ts +const table = createTable({ + //... + manualPagination: true, + manualRowSelection: true, // allow the 'exclude' row selection mode + rowCount: 100_000, // the total number of rows on the server +}) +``` + +"Select all" and "deselect all" change the mode and clear `rowSelection`. They do not write the ids of the loaded rows. The row APIs then add and remove exceptions: + +```ts +table.toggleAllRowsSelected(true) // rowSelectionMode: 'exclude', rowSelection: {} +table.getRow('7').toggleSelected(false) // rowSelection: { 7: true } + +table.getRow('7').getIsSelected() // false +table.getIsAllRowsSelected() // false +table.getIsSomeRowsSelected() // true +table.getSelectedRowCount() // 99_999 + +table.getRow('7').toggleSelected(true) // rowSelection: {} +table.toggleAllRowsSelected(false) // rowSelectionMode: 'include', rowSelection: {} +``` + +In `'exclude'` mode: + +- Rows that load later show as selected, unless their id is in `rowSelection`. +- `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. +- `table.getIsSomeRowsSelected()` returns `true`. +- `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. +- `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. + +The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. + +For a bulk action, send the mode, the ids, and the current filters to your server: + +```ts +const mode = table.atoms.rowSelectionMode.get() +const ids = Object.keys(table.atoms.rowSelection.get()) + +await bulkDelete( + mode === 'exclude' + ? { allRowsExcept: ids, filters } // all rows that match the filters, except these ids + : { onlyRows: ids }, +) +``` + +> [!WARNING] +> In `'exclude'` mode, the keys of `rowSelection` are the **deselected** row ids. Code that reads `Object.keys(rowSelection)` as the selected ids must read `rowSelectionMode` first. Without `manualRowSelection`, the mode is always read as `'include'`. + +Some limits apply, because the table cannot see rows that are not loaded: + +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. +- `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. + ### Render Row Selection UI TanStack Table does not dictate how you should render your row selection UI. You can use checkboxes, radio buttons, or simply hook up click events to the row itself. The table instance provides a few APIs to help you render your row selection UI. diff --git a/docs/framework/angular/guide/row-selection.md b/docs/framework/angular/guide/row-selection.md index a98829d7db..d65253ccda 100644 --- a/docs/framework/angular/guide/row-selection.md +++ b/docs/framework/angular/guide/row-selection.md @@ -219,6 +219,74 @@ const handler = row.getToggleSelectedHandler({ The interaction anchor is preserved across sorting, filtering, grouping, expansion, pagination, and data updates while its row id remains in the display order. If filtering or data replacement removes the anchor, the next Shift interaction falls back to an ordinary row toggle and establishes a new anchor. `resetRowSelection`, either select-all API, and `table.reset()` clear the anchor. Direct calls to `row.toggleSelected()` or `table.setRowSelection()`, and external controlled-state changes, do not establish or move it. +### Select All Rows of a Remote Table + +With server-side pagination, the table only has the rows of the current page. By default, `table.toggleAllRowsSelected()` selects only these loaded rows, and `table.getIsAllRowsSelected()` checks only these loaded rows. + +Set the `manualRowSelection` table option to `true` when "select all" must select all rows on the server. The table then reads the `rowSelection` state together with the `rowSelectionMode` state: + +- `'include'` (default) - `rowSelection` contains the ids of the selected rows. +- `'exclude'` - all rows are selected, and `rowSelection` contains the ids of the deselected rows. + +```ts +readonly table = injectTable(() => ({ + //... + manualPagination: true, + manualRowSelection: true, // allow the 'exclude' row selection mode + rowCount: 100_000, // the total number of rows on the server +})) +``` + +"Select all" and "deselect all" change the mode and clear `rowSelection`. They do not write the ids of the loaded rows. The row APIs then add and remove exceptions: + +```ts +table.toggleAllRowsSelected(true) // rowSelectionMode: 'exclude', rowSelection: {} +table.getRow('7').toggleSelected(false) // rowSelection: { 7: true } + +table.getRow('7').getIsSelected() // false +table.getIsAllRowsSelected() // false +table.getIsSomeRowsSelected() // true +table.getSelectedRowCount() // 99_999 + +table.getRow('7').toggleSelected(true) // rowSelection: {} +table.toggleAllRowsSelected(false) // rowSelectionMode: 'include', rowSelection: {} +``` + +In `'exclude'` mode: + +- Rows that load later show as selected, unless their id is in `rowSelection`. +- `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. +- `table.getIsSomeRowsSelected()` returns `true`. +- `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. +- `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. + +The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. + +For a bulk action, send the mode, the ids, and the current filters to your server: + +```ts +const mode = table.atoms.rowSelectionMode.get() +const ids = Object.keys(table.atoms.rowSelection.get()) + +await bulkDelete( + mode === 'exclude' + ? { allRowsExcept: ids, filters } // all rows that match the filters, except these ids + : { onlyRows: ids }, +) +``` + +> [!WARNING] +> In `'exclude'` mode, the keys of `rowSelection` are the **deselected** row ids. Code that reads `Object.keys(rowSelection)` as the selected ids must read `rowSelectionMode` first. Without `manualRowSelection`, the mode is always read as `'include'`. + +Some limits apply, because the table cannot see rows that are not loaded: + +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. +- `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. + ### Render Row Selection UI TanStack table does not dictate how you should render your row selection UI. You can use checkboxes, radio buttons, or simply hook up click events to the row itself. The table instance provides a few APIs to help you render your row selection UI. diff --git a/docs/framework/ember/guide/row-selection.md b/docs/framework/ember/guide/row-selection.md index f7e08fb6ff..e8c8e841a1 100644 --- a/docs/framework/ember/guide/row-selection.md +++ b/docs/framework/ember/guide/row-selection.md @@ -211,6 +211,74 @@ const handler = row.getToggleSelectedHandler({ The interaction anchor is preserved across sorting, filtering, grouping, expansion, pagination, and data updates while its row id remains in the display order. If filtering or data replacement removes the anchor, the next Shift interaction falls back to an ordinary row toggle and establishes a new anchor. `resetRowSelection`, either select-all API, and `table.reset()` clear the anchor. Direct calls to `row.toggleSelected()` or `table.setRowSelection()`, and external controlled-state changes, do not establish or move it. +### Select All Rows of a Remote Table + +With server-side pagination, the table only has the rows of the current page. By default, `table.toggleAllRowsSelected()` selects only these loaded rows, and `table.getIsAllRowsSelected()` checks only these loaded rows. + +Set the `manualRowSelection` table option to `true` when "select all" must select all rows on the server. The table then reads the `rowSelection` state together with the `rowSelectionMode` state: + +- `'include'` (default) - `rowSelection` contains the ids of the selected rows. +- `'exclude'` - all rows are selected, and `rowSelection` contains the ids of the deselected rows. + +```ts +const table = useTable(() => ({ + //... + manualPagination: true, + manualRowSelection: true, // allow the 'exclude' row selection mode + rowCount: 100_000, // the total number of rows on the server +})) +``` + +"Select all" and "deselect all" change the mode and clear `rowSelection`. They do not write the ids of the loaded rows. The row APIs then add and remove exceptions: + +```ts +table.toggleAllRowsSelected(true) // rowSelectionMode: 'exclude', rowSelection: {} +table.getRow('7').toggleSelected(false) // rowSelection: { 7: true } + +table.getRow('7').getIsSelected() // false +table.getIsAllRowsSelected() // false +table.getIsSomeRowsSelected() // true +table.getSelectedRowCount() // 99_999 + +table.getRow('7').toggleSelected(true) // rowSelection: {} +table.toggleAllRowsSelected(false) // rowSelectionMode: 'include', rowSelection: {} +``` + +In `'exclude'` mode: + +- Rows that load later show as selected, unless their id is in `rowSelection`. +- `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. +- `table.getIsSomeRowsSelected()` returns `true`. +- `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. +- `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. + +The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. + +For a bulk action, send the mode, the ids, and the current filters to your server: + +```ts +const mode = table.atoms.rowSelectionMode.get() +const ids = Object.keys(table.atoms.rowSelection.get()) + +await bulkDelete( + mode === 'exclude' + ? { allRowsExcept: ids, filters } // all rows that match the filters, except these ids + : { onlyRows: ids }, +) +``` + +> [!WARNING] +> In `'exclude'` mode, the keys of `rowSelection` are the **deselected** row ids. Code that reads `Object.keys(rowSelection)` as the selected ids must read `rowSelectionMode` first. Without `manualRowSelection`, the mode is always read as `'include'`. + +Some limits apply, because the table cannot see rows that are not loaded: + +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. +- `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. + ### Render Row Selection UI TanStack table does not dictate how you should render your row selection UI. You can use checkboxes, radio buttons, or simply hook up click events to the row itself. The table instance provides a few APIs to help you render your row selection UI. diff --git a/docs/framework/lit/guide/row-selection.md b/docs/framework/lit/guide/row-selection.md index 8911902c16..aff55a0c04 100644 --- a/docs/framework/lit/guide/row-selection.md +++ b/docs/framework/lit/guide/row-selection.md @@ -226,6 +226,74 @@ const handler = row.getToggleSelectedHandler({ The interaction anchor is preserved across sorting, filtering, grouping, expansion, pagination, and data updates while its row id remains in the display order. If filtering or data replacement removes the anchor, the next Shift interaction falls back to an ordinary row toggle and establishes a new anchor. `resetRowSelection`, either select-all API, and `table.reset()` clear the anchor. Direct calls to `row.toggleSelected()` or `table.setRowSelection()`, and external controlled-state changes, do not establish or move it. +### Select All Rows of a Remote Table + +With server-side pagination, the table only has the rows of the current page. By default, `table.toggleAllRowsSelected()` selects only these loaded rows, and `table.getIsAllRowsSelected()` checks only these loaded rows. + +Set the `manualRowSelection` table option to `true` when "select all" must select all rows on the server. The table then reads the `rowSelection` state together with the `rowSelectionMode` state: + +- `'include'` (default) - `rowSelection` contains the ids of the selected rows. +- `'exclude'` - all rows are selected, and `rowSelection` contains the ids of the deselected rows. + +```ts +const table = this.tableController.table({ + //... + manualPagination: true, + manualRowSelection: true, // allow the 'exclude' row selection mode + rowCount: 100_000, // the total number of rows on the server +}) +``` + +"Select all" and "deselect all" change the mode and clear `rowSelection`. They do not write the ids of the loaded rows. The row APIs then add and remove exceptions: + +```ts +table.toggleAllRowsSelected(true) // rowSelectionMode: 'exclude', rowSelection: {} +table.getRow('7').toggleSelected(false) // rowSelection: { 7: true } + +table.getRow('7').getIsSelected() // false +table.getIsAllRowsSelected() // false +table.getIsSomeRowsSelected() // true +table.getSelectedRowCount() // 99_999 + +table.getRow('7').toggleSelected(true) // rowSelection: {} +table.toggleAllRowsSelected(false) // rowSelectionMode: 'include', rowSelection: {} +``` + +In `'exclude'` mode: + +- Rows that load later show as selected, unless their id is in `rowSelection`. +- `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. +- `table.getIsSomeRowsSelected()` returns `true`. +- `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. +- `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. + +The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. + +For a bulk action, send the mode, the ids, and the current filters to your server: + +```ts +const mode = table.atoms.rowSelectionMode.get() +const ids = Object.keys(table.atoms.rowSelection.get()) + +await bulkDelete( + mode === 'exclude' + ? { allRowsExcept: ids, filters } // all rows that match the filters, except these ids + : { onlyRows: ids }, +) +``` + +> [!WARNING] +> In `'exclude'` mode, the keys of `rowSelection` are the **deselected** row ids. Code that reads `Object.keys(rowSelection)` as the selected ids must read `rowSelectionMode` first. Without `manualRowSelection`, the mode is always read as `'include'`. + +Some limits apply, because the table cannot see rows that are not loaded: + +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. +- `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. + ### Render Row Selection UI TanStack table does not dictate how you should render your row selection UI. You can use checkboxes, radio buttons, or simply hook up click events to the row itself. The table instance provides a few APIs to help you render your row selection UI. diff --git a/docs/framework/octane/guide/row-selection.md b/docs/framework/octane/guide/row-selection.md index d4f8e26581..40a6d4a39d 100644 --- a/docs/framework/octane/guide/row-selection.md +++ b/docs/framework/octane/guide/row-selection.md @@ -209,6 +209,74 @@ const handler = row.getToggleSelectedHandler({ The interaction anchor is preserved across sorting, filtering, grouping, expansion, pagination, and data updates while its row id remains in the display order. If filtering or data replacement removes the anchor, the next Shift interaction falls back to an ordinary row toggle and establishes a new anchor. `resetRowSelection`, either select-all API, and `table.reset()` clear the anchor. Direct calls to `row.toggleSelected()` or `table.setRowSelection()`, and external controlled-state changes, do not establish or move it. +### Select All Rows of a Remote Table + +With server-side pagination, the table only has the rows of the current page. By default, `table.toggleAllRowsSelected()` selects only these loaded rows, and `table.getIsAllRowsSelected()` checks only these loaded rows. + +Set the `manualRowSelection` table option to `true` when "select all" must select all rows on the server. The table then reads the `rowSelection` state together with the `rowSelectionMode` state: + +- `'include'` (default) - `rowSelection` contains the ids of the selected rows. +- `'exclude'` - all rows are selected, and `rowSelection` contains the ids of the deselected rows. + +```ts +const table = useTable({ + //... + manualPagination: true, + manualRowSelection: true, // allow the 'exclude' row selection mode + rowCount: 100_000, // the total number of rows on the server +}) +``` + +"Select all" and "deselect all" change the mode and clear `rowSelection`. They do not write the ids of the loaded rows. The row APIs then add and remove exceptions: + +```ts +table.toggleAllRowsSelected(true) // rowSelectionMode: 'exclude', rowSelection: {} +table.getRow('7').toggleSelected(false) // rowSelection: { 7: true } + +table.getRow('7').getIsSelected() // false +table.getIsAllRowsSelected() // false +table.getIsSomeRowsSelected() // true +table.getSelectedRowCount() // 99_999 + +table.getRow('7').toggleSelected(true) // rowSelection: {} +table.toggleAllRowsSelected(false) // rowSelectionMode: 'include', rowSelection: {} +``` + +In `'exclude'` mode: + +- Rows that load later show as selected, unless their id is in `rowSelection`. +- `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. +- `table.getIsSomeRowsSelected()` returns `true`. +- `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. +- `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. + +The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. + +For a bulk action, send the mode, the ids, and the current filters to your server: + +```ts +const mode = table.atoms.rowSelectionMode.get() +const ids = Object.keys(table.atoms.rowSelection.get()) + +await bulkDelete( + mode === 'exclude' + ? { allRowsExcept: ids, filters } // all rows that match the filters, except these ids + : { onlyRows: ids }, +) +``` + +> [!WARNING] +> In `'exclude'` mode, the keys of `rowSelection` are the **deselected** row ids. Code that reads `Object.keys(rowSelection)` as the selected ids must read `rowSelectionMode` first. Without `manualRowSelection`, the mode is always read as `'include'`. + +Some limits apply, because the table cannot see rows that are not loaded: + +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. +- `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. + ### Render Row Selection UI TanStack table does not dictate how you should render your row selection UI. You can use checkboxes, radio buttons, or simply hook up click events to the row itself. The table instance provides a few APIs to help you render your row selection UI. diff --git a/docs/framework/preact/guide/row-selection.md b/docs/framework/preact/guide/row-selection.md index 176f6503e3..c73921eb82 100644 --- a/docs/framework/preact/guide/row-selection.md +++ b/docs/framework/preact/guide/row-selection.md @@ -209,6 +209,74 @@ const handler = row.getToggleSelectedHandler({ The interaction anchor is preserved across sorting, filtering, grouping, expansion, pagination, and data updates while its row id remains in the display order. If filtering or data replacement removes the anchor, the next Shift interaction falls back to an ordinary row toggle and establishes a new anchor. `resetRowSelection`, either select-all API, and `table.reset()` clear the anchor. Direct calls to `row.toggleSelected()` or `table.setRowSelection()`, and external controlled-state changes, do not establish or move it. +### Select All Rows of a Remote Table + +With server-side pagination, the table only has the rows of the current page. By default, `table.toggleAllRowsSelected()` selects only these loaded rows, and `table.getIsAllRowsSelected()` checks only these loaded rows. + +Set the `manualRowSelection` table option to `true` when "select all" must select all rows on the server. The table then reads the `rowSelection` state together with the `rowSelectionMode` state: + +- `'include'` (default) - `rowSelection` contains the ids of the selected rows. +- `'exclude'` - all rows are selected, and `rowSelection` contains the ids of the deselected rows. + +```ts +const table = useTable({ + //... + manualPagination: true, + manualRowSelection: true, // allow the 'exclude' row selection mode + rowCount: 100_000, // the total number of rows on the server +}) +``` + +"Select all" and "deselect all" change the mode and clear `rowSelection`. They do not write the ids of the loaded rows. The row APIs then add and remove exceptions: + +```ts +table.toggleAllRowsSelected(true) // rowSelectionMode: 'exclude', rowSelection: {} +table.getRow('7').toggleSelected(false) // rowSelection: { 7: true } + +table.getRow('7').getIsSelected() // false +table.getIsAllRowsSelected() // false +table.getIsSomeRowsSelected() // true +table.getSelectedRowCount() // 99_999 + +table.getRow('7').toggleSelected(true) // rowSelection: {} +table.toggleAllRowsSelected(false) // rowSelectionMode: 'include', rowSelection: {} +``` + +In `'exclude'` mode: + +- Rows that load later show as selected, unless their id is in `rowSelection`. +- `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. +- `table.getIsSomeRowsSelected()` returns `true`. +- `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. +- `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. + +The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. + +For a bulk action, send the mode, the ids, and the current filters to your server: + +```ts +const mode = table.atoms.rowSelectionMode.get() +const ids = Object.keys(table.atoms.rowSelection.get()) + +await bulkDelete( + mode === 'exclude' + ? { allRowsExcept: ids, filters } // all rows that match the filters, except these ids + : { onlyRows: ids }, +) +``` + +> [!WARNING] +> In `'exclude'` mode, the keys of `rowSelection` are the **deselected** row ids. Code that reads `Object.keys(rowSelection)` as the selected ids must read `rowSelectionMode` first. Without `manualRowSelection`, the mode is always read as `'include'`. + +Some limits apply, because the table cannot see rows that are not loaded: + +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. +- `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. + ### Render Row Selection UI TanStack table does not dictate how you should render your row selection UI. You can use checkboxes, radio buttons, or simply hook up click events to the row itself. The table instance provides a few APIs to help you render your row selection UI. diff --git a/docs/framework/react/guide/row-selection.md b/docs/framework/react/guide/row-selection.md index 3c3cd7b0b8..e8d857e2ba 100644 --- a/docs/framework/react/guide/row-selection.md +++ b/docs/framework/react/guide/row-selection.md @@ -210,6 +210,74 @@ By default, a parent encountered in a range recursively toggles its selectable d The interaction anchor is preserved across sorting, filtering, grouping, expansion, pagination, and data updates while its row id remains in the display order. If filtering or data replacement removes the anchor, the next Shift interaction falls back to an ordinary row toggle and establishes a new anchor. `resetRowSelection`, either select-all API, and `table.reset()` clear the anchor. Direct calls to `row.toggleSelected()` or `table.setRowSelection()`, and external controlled-state changes, do not establish or move it. +### Select All Rows of a Remote Table + +With server-side pagination, the table only has the rows of the current page. By default, `table.toggleAllRowsSelected()` selects only these loaded rows, and `table.getIsAllRowsSelected()` checks only these loaded rows. + +Set the `manualRowSelection` table option to `true` when "select all" must select all rows on the server. The table then reads the `rowSelection` state together with the `rowSelectionMode` state: + +- `'include'` (default) - `rowSelection` contains the ids of the selected rows. +- `'exclude'` - all rows are selected, and `rowSelection` contains the ids of the deselected rows. + +```ts +const table = useTable({ + //... + manualPagination: true, + manualRowSelection: true, // allow the 'exclude' row selection mode + rowCount: 100_000, // the total number of rows on the server +}) +``` + +"Select all" and "deselect all" change the mode and clear `rowSelection`. They do not write the ids of the loaded rows. The row APIs then add and remove exceptions: + +```ts +table.toggleAllRowsSelected(true) // rowSelectionMode: 'exclude', rowSelection: {} +table.getRow('7').toggleSelected(false) // rowSelection: { 7: true } + +table.getRow('7').getIsSelected() // false +table.getIsAllRowsSelected() // false +table.getIsSomeRowsSelected() // true +table.getSelectedRowCount() // 99_999 + +table.getRow('7').toggleSelected(true) // rowSelection: {} +table.toggleAllRowsSelected(false) // rowSelectionMode: 'include', rowSelection: {} +``` + +In `'exclude'` mode: + +- Rows that load later show as selected, unless their id is in `rowSelection`. +- `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. +- `table.getIsSomeRowsSelected()` returns `true`. +- `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. +- `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. + +The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. + +For a bulk action, send the mode, the ids, and the current filters to your server: + +```ts +const mode = table.atoms.rowSelectionMode.get() +const ids = Object.keys(table.atoms.rowSelection.get()) + +await bulkDelete( + mode === 'exclude' + ? { allRowsExcept: ids, filters } // all rows that match the filters, except these ids + : { onlyRows: ids }, +) +``` + +> [!WARNING] +> In `'exclude'` mode, the keys of `rowSelection` are the **deselected** row ids. Code that reads `Object.keys(rowSelection)` as the selected ids must read `rowSelectionMode` first. Without `manualRowSelection`, the mode is always read as `'include'`. + +Some limits apply, because the table cannot see rows that are not loaded: + +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. +- `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. + ### Render Row Selection UI TanStack table does not dictate how you should render your row selection UI. You can use checkboxes, radio buttons, or simply hook up click events to the row itself. The table instance provides a few APIs to help you render your row selection UI. diff --git a/docs/framework/solid/guide/row-selection.md b/docs/framework/solid/guide/row-selection.md index b7588da736..8010c4e7ab 100644 --- a/docs/framework/solid/guide/row-selection.md +++ b/docs/framework/solid/guide/row-selection.md @@ -215,6 +215,74 @@ const handler = row.getToggleSelectedHandler({ The interaction anchor is preserved across sorting, filtering, grouping, expansion, pagination, and data updates while its row id remains in the display order. If filtering or data replacement removes the anchor, the next Shift interaction falls back to an ordinary row toggle and establishes a new anchor. `resetRowSelection`, either select-all API, and `table.reset()` clear the anchor. Direct calls to `row.toggleSelected()` or `table.setRowSelection()`, and external controlled-state changes, do not establish or move it. +### Select All Rows of a Remote Table + +With server-side pagination, the table only has the rows of the current page. By default, `table.toggleAllRowsSelected()` selects only these loaded rows, and `table.getIsAllRowsSelected()` checks only these loaded rows. + +Set the `manualRowSelection` table option to `true` when "select all" must select all rows on the server. The table then reads the `rowSelection` state together with the `rowSelectionMode` state: + +- `'include'` (default) - `rowSelection` contains the ids of the selected rows. +- `'exclude'` - all rows are selected, and `rowSelection` contains the ids of the deselected rows. + +```ts +const table = createTable({ + //... + manualPagination: true, + manualRowSelection: true, // allow the 'exclude' row selection mode + rowCount: 100_000, // the total number of rows on the server +}) +``` + +"Select all" and "deselect all" change the mode and clear `rowSelection`. They do not write the ids of the loaded rows. The row APIs then add and remove exceptions: + +```ts +table.toggleAllRowsSelected(true) // rowSelectionMode: 'exclude', rowSelection: {} +table.getRow('7').toggleSelected(false) // rowSelection: { 7: true } + +table.getRow('7').getIsSelected() // false +table.getIsAllRowsSelected() // false +table.getIsSomeRowsSelected() // true +table.getSelectedRowCount() // 99_999 + +table.getRow('7').toggleSelected(true) // rowSelection: {} +table.toggleAllRowsSelected(false) // rowSelectionMode: 'include', rowSelection: {} +``` + +In `'exclude'` mode: + +- Rows that load later show as selected, unless their id is in `rowSelection`. +- `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. +- `table.getIsSomeRowsSelected()` returns `true`. +- `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. +- `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. + +The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. + +For a bulk action, send the mode, the ids, and the current filters to your server: + +```ts +const mode = table.atoms.rowSelectionMode.get() +const ids = Object.keys(table.atoms.rowSelection.get()) + +await bulkDelete( + mode === 'exclude' + ? { allRowsExcept: ids, filters } // all rows that match the filters, except these ids + : { onlyRows: ids }, +) +``` + +> [!WARNING] +> In `'exclude'` mode, the keys of `rowSelection` are the **deselected** row ids. Code that reads `Object.keys(rowSelection)` as the selected ids must read `rowSelectionMode` first. Without `manualRowSelection`, the mode is always read as `'include'`. + +Some limits apply, because the table cannot see rows that are not loaded: + +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. +- `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. + ### Render Row Selection UI TanStack table does not dictate how you should render your row selection UI. You can use checkboxes, radio buttons, or simply hook up click events to the row itself. The table instance provides a few APIs to help you render your row selection UI. diff --git a/docs/framework/svelte/guide/row-selection.md b/docs/framework/svelte/guide/row-selection.md index 4a90984063..9efb11c665 100644 --- a/docs/framework/svelte/guide/row-selection.md +++ b/docs/framework/svelte/guide/row-selection.md @@ -224,6 +224,74 @@ const handler = row.getToggleSelectedHandler({ The interaction anchor is preserved across sorting, filtering, grouping, expansion, pagination, and data updates while its row id remains in the display order. If filtering or data replacement removes the anchor, the next Shift interaction falls back to an ordinary row toggle and establishes a new anchor. `resetRowSelection`, either select-all API, and `table.reset()` clear the anchor. Direct calls to `row.toggleSelected()` or `table.setRowSelection()`, and external controlled-state changes, do not establish or move it. +### Select All Rows of a Remote Table + +With server-side pagination, the table only has the rows of the current page. By default, `table.toggleAllRowsSelected()` selects only these loaded rows, and `table.getIsAllRowsSelected()` checks only these loaded rows. + +Set the `manualRowSelection` table option to `true` when "select all" must select all rows on the server. The table then reads the `rowSelection` state together with the `rowSelectionMode` state: + +- `'include'` (default) - `rowSelection` contains the ids of the selected rows. +- `'exclude'` - all rows are selected, and `rowSelection` contains the ids of the deselected rows. + +```ts +const table = createTable({ + //... + manualPagination: true, + manualRowSelection: true, // allow the 'exclude' row selection mode + rowCount: 100_000, // the total number of rows on the server +}) +``` + +"Select all" and "deselect all" change the mode and clear `rowSelection`. They do not write the ids of the loaded rows. The row APIs then add and remove exceptions: + +```ts +table.toggleAllRowsSelected(true) // rowSelectionMode: 'exclude', rowSelection: {} +table.getRow('7').toggleSelected(false) // rowSelection: { 7: true } + +table.getRow('7').getIsSelected() // false +table.getIsAllRowsSelected() // false +table.getIsSomeRowsSelected() // true +table.getSelectedRowCount() // 99_999 + +table.getRow('7').toggleSelected(true) // rowSelection: {} +table.toggleAllRowsSelected(false) // rowSelectionMode: 'include', rowSelection: {} +``` + +In `'exclude'` mode: + +- Rows that load later show as selected, unless their id is in `rowSelection`. +- `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. +- `table.getIsSomeRowsSelected()` returns `true`. +- `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. +- `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. + +The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. + +For a bulk action, send the mode, the ids, and the current filters to your server: + +```ts +const mode = table.atoms.rowSelectionMode.get() +const ids = Object.keys(table.atoms.rowSelection.get()) + +await bulkDelete( + mode === 'exclude' + ? { allRowsExcept: ids, filters } // all rows that match the filters, except these ids + : { onlyRows: ids }, +) +``` + +> [!WARNING] +> In `'exclude'` mode, the keys of `rowSelection` are the **deselected** row ids. Code that reads `Object.keys(rowSelection)` as the selected ids must read `rowSelectionMode` first. Without `manualRowSelection`, the mode is always read as `'include'`. + +Some limits apply, because the table cannot see rows that are not loaded: + +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. +- `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. + ### Render Row Selection UI TanStack table does not dictate how you should render your row selection UI. You can use checkboxes, radio buttons, or simply hook up click events to the row itself. The table instance provides a few APIs to help you render your row selection UI. diff --git a/docs/framework/vue/guide/row-selection.md b/docs/framework/vue/guide/row-selection.md index 2ecb04b9b3..9384cc3512 100644 --- a/docs/framework/vue/guide/row-selection.md +++ b/docs/framework/vue/guide/row-selection.md @@ -214,6 +214,74 @@ const handler = row.getToggleSelectedHandler({ The interaction anchor is preserved across sorting, filtering, grouping, expansion, pagination, and data updates while its row id remains in the display order. If filtering or data replacement removes the anchor, the next Shift interaction falls back to an ordinary row toggle and establishes a new anchor. `resetRowSelection`, either select-all API, and `table.reset()` clear the anchor. Direct calls to `row.toggleSelected()` or `table.setRowSelection()`, and external controlled-state changes, do not establish or move it. +### Select All Rows of a Remote Table + +With server-side pagination, the table only has the rows of the current page. By default, `table.toggleAllRowsSelected()` selects only these loaded rows, and `table.getIsAllRowsSelected()` checks only these loaded rows. + +Set the `manualRowSelection` table option to `true` when "select all" must select all rows on the server. The table then reads the `rowSelection` state together with the `rowSelectionMode` state: + +- `'include'` (default) - `rowSelection` contains the ids of the selected rows. +- `'exclude'` - all rows are selected, and `rowSelection` contains the ids of the deselected rows. + +```ts +const table = useTable({ + //... + manualPagination: true, + manualRowSelection: true, // allow the 'exclude' row selection mode + rowCount: 100_000, // the total number of rows on the server +}) +``` + +"Select all" and "deselect all" change the mode and clear `rowSelection`. They do not write the ids of the loaded rows. The row APIs then add and remove exceptions: + +```ts +table.toggleAllRowsSelected(true) // rowSelectionMode: 'exclude', rowSelection: {} +table.getRow('7').toggleSelected(false) // rowSelection: { 7: true } + +table.getRow('7').getIsSelected() // false +table.getIsAllRowsSelected() // false +table.getIsSomeRowsSelected() // true +table.getSelectedRowCount() // 99_999 + +table.getRow('7').toggleSelected(true) // rowSelection: {} +table.toggleAllRowsSelected(false) // rowSelectionMode: 'include', rowSelection: {} +``` + +In `'exclude'` mode: + +- Rows that load later show as selected, unless their id is in `rowSelection`. +- `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. +- `table.getIsSomeRowsSelected()` returns `true`. +- `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. +- `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. + +The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. + +For a bulk action, send the mode, the ids, and the current filters to your server: + +```ts +const mode = table.atoms.rowSelectionMode.get() +const ids = Object.keys(table.atoms.rowSelection.get()) + +await bulkDelete( + mode === 'exclude' + ? { allRowsExcept: ids, filters } // all rows that match the filters, except these ids + : { onlyRows: ids }, +) +``` + +> [!WARNING] +> In `'exclude'` mode, the keys of `rowSelection` are the **deselected** row ids. Code that reads `Object.keys(rowSelection)` as the selected ids must read `rowSelectionMode` first. Without `manualRowSelection`, the mode is always read as `'include'`. + +Some limits apply, because the table cannot see rows that are not loaded: + +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. +- `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. + ### Render Row Selection UI TanStack table does not dictate how you should render your row selection UI. You can use checkboxes, radio buttons, or simply hook up click events to the row itself. The table instance provides a few APIs to help you render your row selection UI. diff --git a/examples/react/kitchen-sink/src/routes/index.tsx b/examples/react/kitchen-sink/src/routes/index.tsx index c14d877ed5..1555a5a930 100644 --- a/examples/react/kitchen-sink/src/routes/index.tsx +++ b/examples/react/kitchen-sink/src/routes/index.tsx @@ -832,6 +832,7 @@ function App() { pagination: state.pagination, rowPinning: state.rowPinning, rowSelection: state.rowSelection, + rowSelectionMode: state.rowSelectionMode, sorting: state.sorting, }), ) diff --git a/packages/table-core/skills/table-features/references/row-selection.md b/packages/table-core/skills/table-features/references/row-selection.md index a5e4779340..4420fdd17e 100644 --- a/packages/table-core/skills/table-features/references/row-selection.md +++ b/packages/table-core/skills/table-features/references/row-selection.md @@ -64,6 +64,32 @@ Selecting a parent cascades its id plus selectable descendant ids into state, bu With `enableSubRowSelection: false` (or a per-row predicate), `table.toggleAllRowsSelected()` skips descendants of blocking parents, and `getIsAllRowsSelected()`/`getIsAllPageRowsSelected()` exclude those descendants from the all-selected computation, so the header checkbox still reads checked. Deselect-all skips rows whose `enableRowSelection` resolves false, preserving their selection; use `toggleAllRowsSelected(false, { deselectAll: true })` or `resetRowSelection(true)` to clear everything including disabled and out-of-model ids. +### Select all rows of a remote table + +```ts +export const remoteOptions = { + getRowId: (row: Person) => row.id, + manualRowSelection: true, + rowCount: 100_000, +} +``` + +```ts +table.toggleAllRowsSelected(true) // rowSelectionMode 'exclude', rowSelection {} +table.getRow('7').toggleSelected(false) // rowSelection { 7: true }, the deselected ids +const selectedCount = table.getSelectedRowCount() // rowCount minus deselected rows +const bulkAction = { + mode: table.atoms.rowSelectionMode.get(), + ids: Object.keys(table.atoms.rowSelection.get()), +} +``` + +`manualRowSelection: true` allows the `'exclude'` value of the `rowSelectionMode` state; without the option the mode is always read as `'include'` and select-all writes the loaded row ids. With it, `toggleAllRowsSelected(true)` sets `'exclude'` and clears `rowSelection`, `toggleAllRowsSelected(false)` sets `'include'` and clears it, and `resetRowSelection()` resets both slices. In `'exclude'` mode a row is selected when its id is not in `rowSelection`, so rows that load later are selected, `getIsAllRowsSelected()` is true only with no ids, and the selected row models and `getSelectedRowIds()` hold only loaded rows. `getIsAllRowsSelected()` is never true in `'include'` mode under this option, because loaded rows are not all rows. + +Set `rowCount` (top-level rows, the same option manual pagination reads) so `getSelectedRowCount()` is right and so a row, range, or page deselect goes back to an empty `'include'` selection when the deselected rows cover every row and no loaded row is still selected. Direct `setRowSelection()` writes and updaters applied later are not normalized. The count skips the ids of loaded sub-rows and group rows but subtracts an id that is not loaded as one row, so with tree data on more than one page take the count from the server. The table does not reset the selection when filters change: "all" means all rows matching the filters at click time, and the deselected ids and `rowCount` belong to those filters, so call `resetRowSelection()` whenever filters or `rowCount` change. + +`enableRowSelection` only gates toggling. In `'exclude'` mode a row whose id is not in `rowSelection` is selected (`getIsSelected()`, selected row models, `getSelectedRowCount()`) even when it cannot be toggled, so the server must skip such rows if they must not be part of a bulk action. The row handler and the page APIs skip such a row, so `getIsSomePageRowsSelected()` can be false while `getIsSomeRowsSelected()` is true, and the table stays in `'exclude'` mode while the row is loaded. Select-all is a no-op when `enableRowSelection` is `false` and stays in `'include'` mode when `rowCount` is `0`. `enableSubRowSelection` only cascades to loaded sub-rows; the server must decide how a parent applies to sub-rows that are not loaded. + ## Common mistakes ### [HIGH] Expecting selection to clean itself @@ -96,6 +122,27 @@ Under manual pagination, unloaded selected IDs have no `Row` object in the curre Source: `docs/framework/react/guide/row-selection.md#note-if-you-are-using-manualpagination` +### [HIGH] Reading exclude-mode ids as the selected ids + +Wrong: + +```ts +const selectedIds = Object.keys(table.atoms.rowSelection.get()) +``` + +Correct: + +```ts +const mode = table.atoms.rowSelectionMode.get() +const ids = Object.keys(table.atoms.rowSelection.get()) +const bulkAction = + mode === 'exclude' ? { allRowsExcept: ids } : { onlyRows: ids } +``` + +With `manualRowSelection: true`, select-all switches `rowSelectionMode` to `'exclude'` and the keys of `rowSelection` become the deselected row ids. Read the mode with the map, or use `row.getIsSelected()` and `table.getSelectedRowCount()`. + +Source: `docs/framework/react/guide/row-selection.md#select-all-rows-of-a-remote-table` + ### [HIGH] Bypassing the range-selection handler Wrong: diff --git a/packages/table-core/src/features/row-selection/rowSelectionFeature.ts b/packages/table-core/src/features/row-selection/rowSelectionFeature.ts index 9053a52a7d..c987664ff0 100644 --- a/packages/table-core/src/features/row-selection/rowSelectionFeature.ts +++ b/packages/table-core/src/features/row-selection/rowSelectionFeature.ts @@ -4,6 +4,7 @@ import { makeStateUpdater, } from '../../utils' import { + getDefaultRowSelectionModeState, getDefaultRowSelectionState, row_getCanMultiSelect, row_getCanSelect, @@ -20,12 +21,15 @@ import { table_getIsSomePageRowsSelected, table_getIsSomeRowsSelected, table_getPreSelectedRowModel, + table_getSelectedRowCount, table_getSelectedRowIds, table_getSelectedRowModel, table_getToggleAllPageRowsSelectedHandler, table_getToggleAllRowsSelectedHandler, table_resetRowSelection, + table_resetRowSelectionMode, table_setRowSelection, + table_setRowSelectionMode, table_toggleAllPageRowsSelected, table_toggleAllRowsSelected, } from './rowSelectionFeature.utils' @@ -48,6 +52,7 @@ export const rowSelectionFeature: TableFeature = { getInitialState: (initialState) => { return { rowSelection: getDefaultRowSelectionState(), + rowSelectionMode: getDefaultRowSelectionModeState(), ...initialState, } }, @@ -55,6 +60,7 @@ export const rowSelectionFeature: TableFeature = { getDefaultTableOptions: (table) => { return { onRowSelectionChange: makeStateUpdater('rowSelection', table), + onRowSelectionModeChange: makeStateUpdater('rowSelectionMode', table), enableRowSelection: true, enableMultiRowSelection: true, enableRowRangeSelection: true, @@ -82,6 +88,8 @@ export const rowSelectionFeature: TableFeature = { memoDeps: (row) => [ row.subRows, row.table.atoms.rowSelection?.get(), + row.table.atoms.rowSelectionMode?.get(), + row.table.options.manualRowSelection, row.table.options.enableRowSelection, ], }, @@ -90,6 +98,8 @@ export const rowSelectionFeature: TableFeature = { memoDeps: (row) => [ row.subRows, row.table.atoms.rowSelection?.get(), + row.table.atoms.rowSelectionMode?.get(), + row.table.options.manualRowSelection, row.table.options.enableRowSelection, ], }, @@ -116,6 +126,12 @@ export const rowSelectionFeature: TableFeature = { table_resetRowSelection: { fn: (defaultState) => table_resetRowSelection(table, defaultState), }, + table_setRowSelectionMode: { + fn: (updater) => table_setRowSelectionMode(table, updater), + }, + table_resetRowSelectionMode: { + fn: (defaultState) => table_resetRowSelectionMode(table, defaultState), + }, table_toggleAllRowsSelected: { fn: (value, opts) => table_toggleAllRowsSelected(table, value, opts), }, @@ -130,6 +146,8 @@ export const rowSelectionFeature: TableFeature = { fn: () => table_getSelectedRowModel(table), memoDeps: () => [ table.atoms.rowSelection?.get(), + table.atoms.rowSelectionMode?.get(), + table.options.manualRowSelection, table.getCoreRowModel(), ], }, @@ -137,6 +155,8 @@ export const rowSelectionFeature: TableFeature = { fn: () => table_getFilteredSelectedRowModel(table), memoDeps: () => [ table.atoms.rowSelection?.get(), + table.atoms.rowSelectionMode?.get(), + table.options.manualRowSelection, table.getFilteredRowModel(), ], }, @@ -144,17 +164,51 @@ export const rowSelectionFeature: TableFeature = { fn: () => table_getGroupedSelectedRowModel(table), memoDeps: () => [ table.atoms.rowSelection?.get(), + table.atoms.rowSelectionMode?.get(), + table.options.manualRowSelection, table.getSortedRowModel(), ], }, table_getSelectedRowIds: { fn: () => table_getSelectedRowIds(table), - memoDeps: () => [table.atoms.rowSelection?.get()], + memoDeps: () => { + const mode = table.atoms.rowSelectionMode?.get() + const manual = table.options.manualRowSelection + return [ + table.atoms.rowSelection?.get(), + mode, + manual, + manual && mode === 'exclude' ? table.getCoreRowModel() : undefined, + ] + }, + }, + table_getSelectedRowCount: { + fn: () => table_getSelectedRowCount(table), + memoDeps: () => { + const mode = table.atoms.rowSelectionMode?.get() + const manual = table.options.manualRowSelection + const rowCount = table.options.rowCount + const rowModels = + !manual || mode !== 'exclude' + ? [] + : rowCount === undefined + ? [table.getFilteredRowModel()] + : [table.getCoreRowModel(), table.getGroupedRowModel()] + return [ + table.atoms.rowSelection?.get(), + mode, + manual, + rowCount, + ...rowModels, + ] + }, }, table_getIsAllRowsSelected: { fn: () => table_getIsAllRowsSelected(table), memoDeps: () => [ table.atoms.rowSelection?.get(), + table.atoms.rowSelectionMode?.get(), + table.options.manualRowSelection, table.getFilteredRowModel(), table.options.enableRowSelection, table.options.enableSubRowSelection, @@ -164,6 +218,8 @@ export const rowSelectionFeature: TableFeature = { fn: () => table_getIsAllPageRowsSelected(table), memoDeps: () => [ table.atoms.rowSelection?.get(), + table.atoms.rowSelectionMode?.get(), + table.options.manualRowSelection, table.getPaginatedRowModel(), table.options.enableRowSelection, table.options.enableSubRowSelection, @@ -171,12 +227,18 @@ export const rowSelectionFeature: TableFeature = { }, table_getIsSomeRowsSelected: { fn: () => table_getIsSomeRowsSelected(table), - memoDeps: () => [table.atoms.rowSelection?.get()], + memoDeps: () => [ + table.atoms.rowSelection?.get(), + table.atoms.rowSelectionMode?.get(), + table.options.manualRowSelection, + ], }, table_getIsSomePageRowsSelected: { fn: () => table_getIsSomePageRowsSelected(table), memoDeps: () => [ table.atoms.rowSelection?.get(), + table.atoms.rowSelectionMode?.get(), + table.options.manualRowSelection, table.getPaginatedRowModel(), table.options.enableRowSelection, ], diff --git a/packages/table-core/src/features/row-selection/rowSelectionFeature.types.ts b/packages/table-core/src/features/row-selection/rowSelectionFeature.types.ts index ad359737e9..0c8d42fcbb 100644 --- a/packages/table-core/src/features/row-selection/rowSelectionFeature.types.ts +++ b/packages/table-core/src/features/row-selection/rowSelectionFeature.types.ts @@ -5,6 +5,15 @@ import type { Row } from '../../types/Row' export type RowSelectionState = Record +/** + * How the ids in `rowSelection` are read. + * + * - `'include'`: the ids are the selected rows. This is the default. + * - `'exclude'`: every row is selected except the ids in `rowSelection`. Only + * used when the `manualRowSelection` table option is `true`. + */ +export type RowSelectionMode = 'include' | 'exclude' + /** * Controls how toggling a row affects its descendants and ancestors. */ @@ -29,6 +38,7 @@ export interface ToggleSelectedOptions { export interface TableState_RowSelection { rowSelection: RowSelectionState + rowSelectionMode: RowSelectionMode } export interface TableOptions_RowSelection< @@ -68,12 +78,41 @@ export interface TableOptions_RowSelection< * `event.nativeEvent` are treated as range-selection events. */ isRowRangeSelectionEvent?: (event: unknown) => boolean + /** + * Allows the `'exclude'` row selection mode, for tables that do not load + * every row (for example with server-side pagination). Defaults to `false`. + * + * When `true`, `toggleAllRowsSelected(true)` sets `rowSelectionMode` to + * `'exclude'` and clears `rowSelection` instead of writing the loaded row + * ids, and `toggleAllRowsSelected(false)` sets the mode back to `'include'` + * and clears `rowSelection`. In `'exclude'` mode the ids in `rowSelection` + * are the deselected rows, so rows that load later are selected. A row + * whose id is not in `rowSelection` is selected, also when + * `enableRowSelection` does not let the user toggle it. + * + * When `false`, `rowSelectionMode` is ignored and always read as + * `'include'`. + */ + manualRowSelection?: boolean /** * Called with an updater when row selection state changes. Pair this with * `state.rowSelection` when using external state; external atoms can own the * slice without this callback. */ onRowSelectionChange?: OnChangeFn + /** + * Called with an updater when the row selection mode changes. Pair this + * with `state.rowSelectionMode` when using external state; external atoms + * can own the slice without this callback. + */ + onRowSelectionModeChange?: OnChangeFn + /** + * The total number of rows, including rows that are not loaded. In + * `'exclude'` mode, `table.getSelectedRowCount()` subtracts the deselected + * rows from this value. This is the same option that manual pagination + * reads, and it counts top-level rows. + */ + rowCount?: number // enableGroupingRowSelection?: // | boolean // | (( @@ -104,7 +143,8 @@ export interface Row_RowSelection { */ getIsAllSubRowsSelected: () => boolean /** - * Checks whether this row id is selected. + * Checks whether this row id is selected. In `'exclude'` mode, a row is + * selected when its id is not in `rowSelection`. */ getIsSelected: () => boolean /** @@ -146,8 +186,23 @@ export interface Table_RowSelection< getGroupedSelectedRowModel: () => RowModel /** * Returns the ids of all selected rows. + * + * In `'exclude'` mode, only the ids of loaded selected rows are returned, + * because the table does not know the ids of rows that are not loaded. */ getSelectedRowIds: () => Array + /** + * Returns the number of selected rows. + * + * In `'include'` mode this is the number of ids in `rowSelection`. In + * `'exclude'` mode this is `options.rowCount` minus the deselected rows. + * `rowCount` counts top-level rows, so the ids of loaded sub-rows and of + * group rows are not subtracted. An id that is not loaded is subtracted as + * one row, because the table cannot know that it is a sub-row. When + * `rowCount` is not set, the selected top-level rows of the filtered row + * model are counted instead, which is only correct when every row is loaded. + */ + getSelectedRowCount: () => number /** * Checks whether every selectable row on the current page is selected. * Sub-rows whose ancestors block sub-row selection are ignored. @@ -156,6 +211,9 @@ export interface Table_RowSelection< /** * Checks whether every selectable filtered row is selected. * Sub-rows whose ancestors block sub-row selection are ignored. + * + * With `manualRowSelection`, the loaded rows are not all rows, so this is + * only `true` in `'exclude'` mode with an empty `rowSelection`. */ getIsAllRowsSelected: () => boolean /** @@ -163,7 +221,8 @@ export interface Table_RowSelection< */ getIsSomePageRowsSelected: () => boolean /** - * Checks whether at least one row id is selected. + * Checks whether at least one row id is selected. Always `true` in + * `'exclude'` mode. */ getIsSomeRowsSelected: () => boolean /** @@ -185,13 +244,27 @@ export interface Table_RowSelection< /** * Resets `rowSelection` to `initialState.rowSelection`. * - * Pass `true` to ignore initial state and reset to `{}`. + * Pass `true` to ignore initial state and reset to `{}`. With + * `manualRowSelection`, `rowSelectionMode` is reset in the same way. */ resetRowSelection: (defaultState?: boolean) => void + /** + * Resets `rowSelectionMode` to `initialState.rowSelectionMode`. + * + * Pass `true` to ignore initial state and reset to `'include'`. This does + * not change `rowSelection`; use `resetRowSelection` to reset both. + */ + resetRowSelectionMode: (defaultState?: boolean) => void /** * Updates row selection state with a next map or updater function. */ setRowSelection: (updater: Updater) => void + /** + * Updates the row selection mode with a next value or updater function. + * This does not change `rowSelection`, so the ids keep their place but + * change their meaning. + */ + setRowSelectionMode: (updater: Updater) => void /** * Selects/deselects all rows on the current page. */ @@ -204,6 +277,12 @@ export interface Table_RowSelection< * * Deselecting keeps rows that cannot be selected in the selection map unless * `opts.deselectAll` is `true`. + * + * With `manualRowSelection`, no row ids are written. Selecting sets + * `rowSelectionMode` to `'exclude'`, deselecting sets it to `'include'`, + * and both clear `rowSelection`. Selecting does nothing when + * `enableRowSelection` is `false`, and stays in `'include'` mode when + * `rowCount` is `0`. */ toggleAllRowsSelected: ( value?: boolean, diff --git a/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts b/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts index c90758f7b9..2deb42fc29 100644 --- a/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts +++ b/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts @@ -11,6 +11,7 @@ import type { RowModel } from '../../core/row-models/coreRowModelsFeature.types' import type { Table_Internal } from '../../types/Table' import type { Row } from '../../types/Row' import type { + RowSelectionMode, RowSelectionState, ToggleSelectedOptions, } from './rowSelectionFeature.types' @@ -32,6 +33,21 @@ export function getDefaultRowSelectionState(): RowSelectionState { return makeObjectMap() } +/** + * Creates the default row selection mode. + * + * The feature default is `'include'`, meaning the ids in `rowSelection` are the + * selected rows. Reset APIs use this value when `defaultState` is `true`. + * + * @example + * ```ts + * const mode = getDefaultRowSelectionModeState() + * ``` + */ +export function getDefaultRowSelectionModeState(): RowSelectionMode { + return 'include' +} + /** * Routes a row selection updater through the table's selection change handler. * @@ -56,11 +72,54 @@ export function table_setRowSelection< table.options.onRowSelectionChange?.(updater) } +/** + * Routes a row selection mode updater through the table's mode change handler. + * + * The mode only has an effect when `options.manualRowSelection` is `true`. + * Changing it does not change `rowSelection`, so the ids keep their place but + * change their meaning. + * + * @example + * ```ts + * table_setRowSelectionMode(table, 'exclude') + * ``` + */ +export function table_setRowSelectionMode< + TFeatures extends TableFeatures, + TData extends RowData, +>(table: Table_Internal, updater: Updater) { + table.options.onRowSelectionModeChange?.(updater) +} + +/** + * Resets `rowSelectionMode` to the configured initial state or feature default. + * + * With no argument, the reset uses `table.initialState.rowSelectionMode` when + * it exists. Passing `true` ignores initial state and resets to `'include'`. + * `rowSelection` is not changed; `table_resetRowSelection` resets both. + * + * @example + * ```ts + * table_resetRowSelectionMode(table) + * table_resetRowSelectionMode(table, true) + * ``` + */ +export function table_resetRowSelectionMode< + TFeatures extends TableFeatures, + TData extends RowData, +>(table: Table_Internal, defaultState?: boolean) { + table_setRowSelectionMode( + table, + getResetRowSelectionMode(table, defaultState), + ) +} + /** * Resets `rowSelection` to the configured initial state or feature default. * * With no argument, the reset clones `table.initialState.rowSelection` when it - * exists. Passing `true` ignores initial state and resets to `{}`. + * exists. Passing `true` ignores initial state and resets to `{}`. With + * `options.manualRowSelection`, `rowSelectionMode` is reset in the same way. * * @example * ```ts @@ -74,15 +133,22 @@ export function table_resetRowSelection< >(table: Table_Internal, defaultState?: boolean) { // @ts-ignore - _lastSelectedRowId is part of the RowSelection feature table._lastSelectedRowId = null - table_setRowSelection( - table, - defaultState - ? makeObjectMap() - : Object.assign( - makeObjectMap(), - cloneState(table.initialState.rowSelection ?? {}), - ), - ) + const rowSelection: RowSelectionState = defaultState + ? makeObjectMap() + : Object.assign( + makeObjectMap(), + cloneState(table.initialState.rowSelection ?? {}), + ) + + if (table.options.manualRowSelection) { + setRowSelectionAndMode( + table, + rowSelection, + getResetRowSelectionMode(table, defaultState), + ) + } else { + table_setRowSelection(table, rowSelection) + } } // Table APIs @@ -96,6 +162,12 @@ export function table_resetRowSelection< * existing selection map; rows that cannot be selected keep their selection * unless `opts.deselectAll` is `true`. * + * With `options.manualRowSelection`, no row ids are written. Selecting sets + * `rowSelectionMode` to `'exclude'`, deselecting sets it to `'include'`, and + * both clear `rowSelection`. Selecting does nothing when + * `options.enableRowSelection` is `false`, and stays in `'include'` mode when + * `options.rowCount` is `0`, because no row can be selected. + * * @example * ```ts * table_toggleAllRowsSelected(table) @@ -111,6 +183,28 @@ export function table_toggleAllRowsSelected< ) { // @ts-ignore - _lastSelectedRowId is part of the RowSelection feature table._lastSelectedRowId = null + + if (table.options.manualRowSelection) { + const selectAll = + value ?? + !callMemoOrStaticFn( + table, + 'getIsAllRowsSelected', + table_getIsAllRowsSelected, + ) + + if (selectAll && table.options.enableRowSelection === false) { + return + } + + setRowSelectionAndMode( + table, + makeObjectMap(), + selectAll && table.options.rowCount !== 0 ? 'exclude' : 'include', + ) + return + } + table_setRowSelection(table, (old) => { value = typeof value !== 'undefined' @@ -154,7 +248,9 @@ export function table_toggleAllRowsSelected< * Selects or deselects every selectable row on the current page. * * Omitting `value` toggles based on `table_getIsAllPageRowsSelected(table)`. - * Child rows are included when sub-row selection allows it. + * Child rows are included when sub-row selection allows it. In `'exclude'` + * mode, deselecting adds the page row ids to `rowSelection`, selecting removes + * them, and `opts.deselectAll` switches back to an empty `'include'` selection. * * @example * ```ts @@ -171,6 +267,37 @@ export function table_toggleAllPageRowsSelected< ) { // @ts-ignore - _lastSelectedRowId is part of the RowSelection feature table._lastSelectedRowId = null + + if (isRowSelectionExcluded(table)) { + const resolvedValue = + value ?? + !callMemoOrStaticFn( + table, + 'getIsAllPageRowsSelected', + table_getIsAllPageRowsSelected, + ) + + if (opts?.deselectAll && !resolvedValue) { + setRowSelectionAndMode(table, makeObjectMap(), 'include') + return + } + + updateRowSelection(table, true, !resolvedValue, (rowSelection) => { + table.getRowModel().rows.forEach((row) => { + mutateRowIsSelected( + rowSelection, + row.id, + resolvedValue, + true, + table, + true, + true, + ) + }) + }) + return + } + table_setRowSelection(table, (old) => { const resolvedValue = typeof value !== 'undefined' @@ -331,6 +458,9 @@ export function table_getGroupedSelectedRowModel< /** * Returns the ids of all selected rows. * + * In `'exclude'` mode, only the ids of loaded selected rows are returned, + * because the table does not know the ids of rows that are not loaded. + * * @example * ```ts * const selectedRowIds = table_getSelectedRowIds(table) @@ -340,7 +470,59 @@ export function table_getSelectedRowIds< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal): Array { - return Object.keys(table.atoms.rowSelection?.get() ?? {}) + const rowSelection = table.atoms.rowSelection?.get() ?? {} + + if (isRowSelectionExcluded(table)) { + const selectedRowIds: Array = [] + table.getCoreRowModel().flatRows.forEach((row) => { + if (!isRowSelected(row, rowSelection)) { + selectedRowIds.push(row.id) + } + }) + return selectedRowIds + } + + return Object.keys(rowSelection) +} + +/** + * Returns the number of selected rows. + * + * In `'include'` mode this is the number of ids in `rowSelection`. In + * `'exclude'` mode this is `options.rowCount` minus the deselected rows. + * `rowCount` counts top-level rows, so the ids of loaded sub-rows and of group + * rows are not subtracted. An id that is not loaded is subtracted as one row, + * because the table cannot know that it is a sub-row. When `rowCount` is not + * set, the selected top-level rows of the filtered row model are counted + * instead, which is only correct when every row is loaded. + * + * @example + * ```ts + * const selectedRowCount = table_getSelectedRowCount(table) + * ``` + */ +export function table_getSelectedRowCount< + TFeatures extends TableFeatures, + TData extends RowData, +>(table: Table_Internal): number { + if (!isRowSelectionExcluded(table)) { + return callMemoOrStaticFn( + table, + 'getSelectedRowIds', + table_getSelectedRowIds, + ).length + } + + const rowSelection = table.atoms.rowSelection?.get() ?? {} + const rowCount = table.options.rowCount + + if (rowCount === undefined) { + return table + .getFilteredRowModel() + .rows.filter((row) => !isRowSelected(row, rowSelection)).length + } + + return Math.max(0, rowCount - countExcludedRows(table, rowSelection)) } /** @@ -350,6 +532,9 @@ export function table_getSelectedRowIds< * empty. Sub-rows whose ancestors block descent via `enableSubRowSelection` are * ignored, matching the rows that `table_toggleAllRowsSelected` selects. * + * With `options.manualRowSelection`, the loaded rows are not all rows, so the + * result is only true in `'exclude'` mode with an empty `rowSelection`. + * * @example * ```ts * const allSelected = table_getIsAllRowsSelected(table) @@ -359,9 +544,16 @@ export function table_getIsAllRowsSelected< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { - const preGroupedFlatRows = table.getFilteredRowModel().flatRows const rowSelection = table.atoms.rowSelection?.get() ?? {} + if (table.options.manualRowSelection) { + return ( + isRowSelectionExcluded(table) && Object.keys(rowSelection).length === 0 + ) + } + + const preGroupedFlatRows = table.getFilteredRowModel().flatRows + let isAllRowsSelected = Boolean( preGroupedFlatRows.length && Object.keys(rowSelection).length, ) @@ -401,6 +593,7 @@ export function table_getIsAllPageRowsSelected< >(table: Table_Internal) { const paginationFlatRows = table.getPaginatedRowModel().flatRows const rowSelection = table.atoms.rowSelection?.get() ?? {} + const exclude = isRowSelectionExcluded(table) const subtreeCache = new Map() // Single pass, cheap map lookup first: an unselected row only matters if @@ -409,7 +602,7 @@ export function table_getIsAllPageRowsSelected< let sawSelectableRow = false for (let i = 0; i < paginationFlatRows.length; i++) { const row = paginationFlatRows[i]! - if (!isRowSelected(row, rowSelection)) { + if (isRowSelected(row, rowSelection) === exclude) { if (isRowSelectableInSelectAll(row, subtreeCache)) { return false } @@ -427,7 +620,8 @@ export function table_getIsAllPageRowsSelected< /** * Checks whether at least one row id is selected. * - * The result stays true when every row is selected. + * The result stays true when every row is selected, and is always true in + * `'exclude'` mode. * * @example * ```ts @@ -439,6 +633,7 @@ export function table_getIsSomeRowsSelected< TData extends RowData, >(table: Table_Internal) { return ( + isRowSelectionExcluded(table) || callMemoOrStaticFn(table, 'getSelectedRowIds', table_getSelectedRowIds) .length > 0 ) @@ -522,6 +717,11 @@ export function table_getToggleAllPageRowsSelectedHandler< * only supports single selection. Pass `deselectParents: true` to also remove * ancestor row ids from the selection when this row is deselected. * + * In `'exclude'` mode, deselecting adds the row id to `rowSelection` and + * selecting removes it. When `options.rowCount` is set, the deselected rows + * cover every row, and no loaded row is selected, the state goes back to an + * empty `'include'` selection. + * * @example * ```ts * row_toggleSelected(row) @@ -535,33 +735,47 @@ export function row_toggleSelected< TFeatures extends TableFeatures, TData extends RowData, >(row: Row, value?: boolean, opts?: ToggleSelectedOptions) { + const table = row.table + const exclude = isRowSelectionExcluded(table) const isSelected = row_getIsSelected(row) + const nextValue = typeof value !== 'undefined' ? value : !isSelected - table_setRowSelection(row.table, (old) => { - value = typeof value !== 'undefined' ? value : !isSelected - - const rowSelection = Object.assign(makeObjectMap(), old) + if (exclude && nextValue && !row_getCanMultiSelect(row)) { + // A single-select row must be the only selected row. A list of deselected + // ids cannot say that, so go back to `'include'` mode. + setRowSelectionAndMode( + table, + Object.assign( + makeObjectMap(), + row_getCanSelect(row) ? { [row.id]: true } : undefined, + ), + 'include', + ) + return + } + updateRowSelection(table, exclude, !nextValue, (rowSelection) => { mutateRowIsSelected( rowSelection, row.id, - value, + nextValue, (opts?.selectChildren ?? true) && row_getCanMultiSelect(row), - row.table, + table, + false, + exclude, ) - if (!value && opts?.deselectParents) { - pruneAncestorRowIds(rowSelection, row) + if (!nextValue && opts?.deselectParents) { + pruneAncestorRowIds(rowSelection, row, exclude) } - - return rowSelection }) } /** * Checks whether this row id is selected in `state.rowSelection`. * - * Missing row ids are treated as not selected. + * Missing row ids are treated as not selected. In `'exclude'` mode this is + * inverted: a row is selected when its id is missing. * * @example * ```ts @@ -573,7 +787,7 @@ export function row_getIsSelected< TData extends RowData, >(row: Row) { const rowSelection = row.table.atoms.rowSelection?.get() ?? {} - return isRowSelected(row, rowSelection) + return isRowSelected(row, rowSelection) !== isRowSelectionExcluded(row.table) } /** @@ -775,9 +989,9 @@ function selectRowRange( const start = Math.min(anchorIndex, rowIndex) const end = Math.max(anchorIndex, rowIndex) - table_setRowSelection(table, (old) => { - const rowSelection = Object.assign(makeObjectMap(), old) + const exclude = isRowSelectionExcluded(table) + updateRowSelection(table, exclude, !value, (rowSelection) => { for (let index = start; index <= end; index++) { const rangeRow = rows[index]! if (!row_getCanSelect(rangeRow) || !row_getCanMultiSelect(rangeRow)) { @@ -789,18 +1003,141 @@ function selectRowRange( value, includeChildren, table, + false, + exclude, ) if (!value && opts?.deselectParents) { - pruneAncestorRowIds(rowSelection, rangeRow) + pruneAncestorRowIds(rowSelection, rangeRow, exclude) } } - - return rowSelection }) return true } +/** + * Returns whether the ids in `rowSelection` are the deselected rows. The mode + * state only counts when `options.manualRowSelection` allows it. + */ +function isRowSelectionExcluded< + TFeatures extends TableFeatures, + TData extends RowData, +>(table: Table_Internal): boolean { + return ( + !!table.options.manualRowSelection && + table.atoms.rowSelectionMode?.get() === 'exclude' + ) +} + +function getResetRowSelectionMode< + TFeatures extends TableFeatures, + TData extends RowData, +>( + table: Table_Internal, + defaultState?: boolean, +): RowSelectionMode { + return defaultState + ? getDefaultRowSelectionModeState() + : (table.initialState.rowSelectionMode ?? getDefaultRowSelectionModeState()) +} + +function setRowSelectionAndMode< + TFeatures extends TableFeatures, + TData extends RowData, +>( + table: Table_Internal, + rowSelection: RowSelectionState, + mode: RowSelectionMode, +): void { + table._reactivity.batch(() => { + table_setRowSelection(table, rowSelection) + table_setRowSelectionMode(table, mode) + }) +} + +/** + * Counts the deselected rows in the unit of `options.rowCount`, which is + * top-level rows. The id of a row that is not loaded counts as a top-level + * row, because the table cannot know that it is a sub-row. + */ +function countExcludedRows< + TFeatures extends TableFeatures, + TData extends RowData, +>( + table: Table_Internal, + rowSelection: RowSelectionState, +): number { + const rowsById = table.getCoreRowModel().rowsById + const groupedRowsById = table.getGroupedRowModel().rowsById + let count = 0 + for (const rowId of Object.keys(rowSelection)) { + const row = rowsById[rowId] + if (!row) { + // A group row is only in the grouped row model. It is not a data row. + if (!groupedRowsById[rowId]) { + count++ + } + continue + } + // Grouping sets `parentId` to a group row id. A group row is not in the + // core row model, so that row is still a top-level row. + if (row.parentId === undefined || !rowsById[row.parentId]) { + count++ + } + } + return count +} + +/** + * Applies `mutate` to a copy of the row selection map. + * + * In `'exclude'` mode with a known `options.rowCount`, a deselection that + * leaves no row selected goes back to an empty `'include'` selection. Without + * this, the mode says "all rows" while no row is selected. + * + * The count of deselected rows can be too high: an id can be a sub-row of a + * page that is not loaded, or `rowCount` can be the count of a new filter. So + * the state only goes back when no loaded row is selected. + */ +function updateRowSelection< + TFeatures extends TableFeatures, + TData extends RowData, +>( + table: Table_Internal, + exclude: boolean, + isDeselect: boolean, + mutate: (rowSelection: RowSelectionState) => void, +): void { + const updater = (old: RowSelectionState) => { + const rowSelection = Object.assign(makeObjectMap(), old) + mutate(rowSelection) + return rowSelection + } + const rowCount = table.options.rowCount + + if (!exclude || !isDeselect || rowCount === undefined) { + table_setRowSelection(table, updater) + return + } + + // Do not change the mode inside the updater. A state setter can run an + // updater two times. Use the current state to make the decision. A state + // owner that applies the updater later can make this decision stale. + const current: RowSelectionState = table.atoms.rowSelection?.get() ?? {} + const next = updater(current) + + if ( + countExcludedRows(table, next) >= rowCount && + table.getCoreRowModel().flatRows.every((row) => isRowSelected(row, next)) + ) { + setRowSelectionAndMode(table, makeObjectMap(), 'include') + } else { + table_setRowSelection(table, (old) => + old === current ? next : updater(old), + ) + } +} + function mutateRowIsSelected< TFeatures extends TableFeatures, TData extends RowData, @@ -811,18 +1148,19 @@ function mutateRowIsSelected< includeChildren: boolean, table: Table_Internal, respectCanSelectOnDeselect?: boolean, + exclude?: boolean, ): void { const row = table.getRow(rowId, true) if (value) { - if (!row_getCanMultiSelect(row)) { + if (!exclude && !row_getCanMultiSelect(row)) { Object.keys(rowSelection).forEach((key) => delete rowSelection[key]) } if (row_getCanSelect(row)) { - rowSelection[rowId] = true + writeRowIsSelected(rowSelection, rowId, true, exclude) } } else if (!respectCanSelectOnDeselect || row_getCanSelect(row)) { - delete rowSelection[rowId] + writeRowIsSelected(rowSelection, rowId, false, exclude) } if (includeChildren && row.subRows.length && row_getCanSelectSubRows(row)) { @@ -834,11 +1172,29 @@ function mutateRowIsSelected< includeChildren, table, respectCanSelectOnDeselect, + exclude, ), ) } } +/** + * Writes one row's selected state into the map. In `'exclude'` mode the map + * holds the deselected ids, so the write is inverted. + */ +function writeRowIsSelected( + rowSelection: RowSelectionState, + rowId: string, + value: boolean, + exclude?: boolean, +): void { + if (value !== !!exclude) { + rowSelection[rowId] = true + } else { + delete rowSelection[rowId] + } +} + /** * Returns whether a select-all cascade can reach this row: the row itself is * selectable and no ancestor blocks descent via `enableSubRowSelection`. @@ -891,7 +1247,11 @@ function isRowSelectableInSelectAll< function pruneAncestorRowIds< TFeatures extends TableFeatures, TData extends RowData, ->(rowSelection: RowSelectionState, row: Row): void { +>( + rowSelection: RowSelectionState, + row: Row, + exclude?: boolean, +): void { // Deselecting a descendant always invalidates "all children selected", so // every ancestor id can be deleted unconditionally. Deliberately not gated // by `row_getCanSelect`: like the other targeted deselection paths, pruning @@ -901,7 +1261,7 @@ function pruneAncestorRowIds< const rowsById = row.table.getCoreRowModel().rowsById let parentId = row.parentId while (parentId !== undefined) { - delete rowSelection[parentId] + writeRowIsSelected(rowSelection, parentId, false, exclude) parentId = (rowsById[parentId] ?? row.table.getRow(parentId, true)).parentId } } @@ -914,12 +1274,13 @@ function selectRowsRecursively< rowSelection: RowSelectionState, selectedFlatRows: Array>, selectedRowsById: Record>, + exclude: boolean, ): Array> { const result: Array> = [] for (let i = 0; i < rows.length; i++) { const row = rows[i]! - const isSelected = isRowSelected(row, rowSelection) + const isSelected = isRowSelected(row, rowSelection) !== exclude if (isSelected) { selectedFlatRows.push(row) @@ -934,6 +1295,7 @@ function selectRowsRecursively< rowSelection, selectedFlatRows, selectedRowsById, + exclude, ) if (isSelected) { @@ -955,7 +1317,8 @@ function selectRowsRecursively< * Builds a row model containing rows selected by the current row selection state. * * The result is derived from the supplied row model, so selected ids absent from - * that model are not materialized as rows. + * that model are not materialized as rows. In `'exclude'` mode, the result + * contains the rows of the supplied row model whose ids are not in the state. * * @example * ```ts @@ -979,6 +1342,7 @@ export function selectRowsFn< rowSelection, newSelectedFlatRows, newSelectedRowsById, + isRowSelectionExcluded(table), ), flatRows: newSelectedFlatRows, rowsById: newSelectedRowsById, @@ -988,6 +1352,9 @@ export function selectRowsFn< /** * Returns whether a row id is selected in the current row selection state. * + * This is the plain map lookup. In `'exclude'` mode the map holds the + * deselected ids, so use `row_getIsSelected` to read the selected state. + * * @example * ```ts * const selected = isRowSelected(row) @@ -1017,6 +1384,7 @@ export function isSubRowSelected< if (!row.subRows.length) return false const rowSelection = row.table.atoms.rowSelection?.get() ?? {} + const exclude = isRowSelectionExcluded(row.table) let someSelected = false let allChildrenSelected = true @@ -1032,7 +1400,7 @@ export function isSubRowSelected< if (row_getCanSelect(subRow)) { someSelectable = true - if (isRowSelected(subRow, rowSelection)) { + if (isRowSelected(subRow, rowSelection) !== exclude) { someSelected = true } else { allChildrenSelected = false diff --git a/packages/table-core/tests/implementation/features/row-selection/rowSelectionMode.test.ts b/packages/table-core/tests/implementation/features/row-selection/rowSelectionMode.test.ts new file mode 100644 index 0000000000..8451809d07 --- /dev/null +++ b/packages/table-core/tests/implementation/features/row-selection/rowSelectionMode.test.ts @@ -0,0 +1,1200 @@ +import { describe, expect, it, vi } from 'vitest' +import { + columnFilteringFeature, + columnGroupingFeature, + constructTable, + createFilteredRowModel, + createGroupedRowModel, + createPaginatedRowModel, + createSortedRowModel, + filterFns, + rowPaginationFeature, + rowSelectionFeature, + rowSortingFeature, + sortFns, +} from '../../../../src' +import { testFeatures } from '../../../fixtures/features' +import type { + RowSelectionMode, + RowSelectionState, + TableOptions, + ToggleSelectedOptions, +} from '../../../../src' + +interface TestRow { + id: string + kind?: string + value: number + subRows?: Array +} + +const selectionFeatures = testFeatures({ rowSelectionFeature }) +const pipelineFeatures = testFeatures({ + columnFilteringFeature, + columnGroupingFeature, + filterFns, + filteredRowModel: createFilteredRowModel(), + groupedRowModel: createGroupedRowModel(), + rowPaginationFeature, + paginatedRowModel: createPaginatedRowModel(), + rowSelectionFeature, + rowSortingFeature, + sortFns, + sortedRowModel: createSortedRowModel(), +}) + +function makeRows(start: number, length: number): Array { + return Array.from({ length }, (_, index) => ({ + id: String(start + index), + kind: (start + index) % 2 ? 'odd' : 'even', + value: start + index, + })) +} + +const flatData = makeRows(0, 6) + +const nestedData: Array = [ + { + id: 'a', + value: 0, + subRows: [ + { id: 'a.1', value: 1 }, + { id: 'a.2', value: 2 }, + ], + }, + { id: 'b', value: 3, subRows: [{ id: 'b.1', value: 4 }] }, +] + +const otherNestedData: Array = [ + { id: 'c', value: 5, subRows: [{ id: 'c.1', value: 6 }] }, + { id: 'd', value: 7 }, +] + +function makeSelectionTable( + options: Partial< + Omit< + TableOptions, + 'columns' | 'data' | 'features' + > + > = {}, + data: Array = flatData, +) { + return constructTable({ + features: selectionFeatures, + columns: [{ accessorKey: 'value', id: 'value' }], + data, + getRowId: (row) => row.id, + getSubRows: (row) => row.subRows, + ...options, + }) +} + +function makePipelineTable( + options: Partial< + Omit< + TableOptions, + 'columns' | 'data' | 'features' + > + > = {}, + data: Array = flatData, +) { + return constructTable({ + features: pipelineFeatures, + columns: [ + { accessorKey: 'id', id: 'id', filterFn: 'includesString' }, + { accessorKey: 'kind', id: 'kind', filterFn: 'equalsString' }, + { accessorKey: 'value', id: 'value' }, + ], + data, + getRowId: (row) => row.id, + getSubRows: (row) => row.subRows, + ...options, + }) +} + +function interact( + row: { + getToggleSelectedHandler: ( + opts?: ToggleSelectedOptions, + ) => (event: unknown) => void + }, + checked: boolean, + event: Record = {}, +) { + row.getToggleSelectedHandler()({ target: { checked }, ...event }) +} + +function ids(table: { atoms: { rowSelection: { get(): RowSelectionState } } }) { + return Object.keys(table.atoms.rowSelection.get()).sort() +} + +function mode(table: { + atoms: { rowSelectionMode: { get(): RowSelectionMode } } +}) { + return table.atoms.rowSelectionMode.get() +} + +describe('row selection mode without manualRowSelection', () => { + it('adds an include mode slice without changing the selection shape', () => { + const table = makeSelectionTable() + + expect(table.initialState.rowSelectionMode).toBe('include') + expect(table.store.state.rowSelectionMode).toBe('include') + expect(table.store.state.rowSelection).toEqual({}) + }) + + it('selects and deselects the loaded row ids and never writes the mode', () => { + const onRowSelectionModeChange = vi.fn() + const table = makeSelectionTable({ + onRowSelectionModeChange, + initialState: { rowSelection: { '99': true } }, + }) + + table.toggleAllRowsSelected(true) + expect(ids(table)).toEqual(['0', '1', '2', '3', '4', '5', '99']) + expect(table.getIsAllRowsSelected()).toBe(true) + expect(table.getSelectedRowCount()).toBe(7) + + table.toggleAllRowsSelected(false) + expect(ids(table)).toEqual(['99']) + + table.getRow('1').toggleSelected(true) + table.toggleAllPageRowsSelected(false, { deselectAll: true }) + table.resetRowSelection() + table.resetRowSelection(true) + + expect(mode(table)).toBe('include') + expect(onRowSelectionModeChange).not.toHaveBeenCalled() + }) + + it('ignores an exclude mode that the option does not allow', () => { + const table = makeSelectionTable({ + initialState: { + rowSelection: { '1': true }, + rowSelectionMode: 'exclude', + }, + }) + + expect(table.getRow('1').getIsSelected()).toBe(true) + expect(table.getRow('2').getIsSelected()).toBe(false) + expect(table.getIsAllRowsSelected()).toBe(false) + expect(table.getIsSomeRowsSelected()).toBe(true) + expect(table.getSelectedRowIds()).toEqual(['1']) + expect(table.getSelectedRowCount()).toBe(1) + expect(table.getSelectedRowModel().flatRows.map((row) => row.id)).toEqual([ + '1', + ]) + + table.getRow('2').toggleSelected(true) + expect(ids(table)).toEqual(['1', '2']) + + table.getRow('1').toggleSelected(false) + expect(ids(table)).toEqual(['2']) + + table.toggleAllRowsSelected(true) + expect(ids(table)).toEqual(['0', '1', '2', '3', '4', '5']) + expect(mode(table)).toBe('exclude') + }) + + it('reads all-selected from the loaded rows', () => { + const table = makeSelectionTable({ + initialState: { + rowSelection: Object.fromEntries( + flatData.map((row) => [row.id, true as const]), + ), + }, + }) + + expect(table.getIsAllRowsSelected()).toBe(true) + }) +}) + +describe('toggleAllRowsSelected with manualRowSelection', () => { + it('selects all rows by switching to exclude mode with no ids', () => { + const table = makeSelectionTable({ + manualRowSelection: true, + initialState: { rowSelection: { '1': true, '99': true } }, + }) + + table.toggleAllRowsSelected(true) + + expect(mode(table)).toBe('exclude') + expect(ids(table)).toEqual([]) + expect(table.getIsAllRowsSelected()).toBe(true) + expect(table.getIsSomeRowsSelected()).toBe(true) + }) + + it('deselects all rows by switching to include mode with no ids', () => { + const table = makeSelectionTable({ + manualRowSelection: true, + enableRowSelection: (row) => row.id !== '0', + initialState: { + rowSelection: { '0': true, '99': true }, + rowSelectionMode: 'exclude', + }, + }) + + table.toggleAllRowsSelected(false) + + expect(mode(table)).toBe('include') + expect(ids(table)).toEqual([]) + expect(table.getIsAllRowsSelected()).toBe(false) + expect(table.getIsSomeRowsSelected()).toBe(false) + }) + + it('toggles from the all-selected state when no value is passed', () => { + const table = makeSelectionTable({ manualRowSelection: true }) + + table.toggleAllRowsSelected() + expect(mode(table)).toBe('exclude') + + table.toggleAllRowsSelected() + expect(mode(table)).toBe('include') + + table.toggleAllRowsSelected() + table.getRow('2').toggleSelected(false) + table.toggleAllRowsSelected() + expect(mode(table)).toBe('exclude') + expect(ids(table)).toEqual([]) + }) + + it('follows the checked value of the select-all handler', () => { + const table = makeSelectionTable({ manualRowSelection: true }) + + table.getToggleAllRowsSelectedHandler()({ target: { checked: true } }) + expect(mode(table)).toBe('exclude') + + table.getToggleAllRowsSelectedHandler()({ target: { checked: false } }) + expect(mode(table)).toBe('include') + }) + + it('writes the selection and the mode in one store update', () => { + const table = makeSelectionTable({ + manualRowSelection: true, + initialState: { rowSelection: { '1': true } }, + }) + const snapshots: Array<[RowSelectionMode, Array]> = [] + const subscription = table.store.subscribe(() => { + const state = table.store.state + snapshots.push([state.rowSelectionMode, Object.keys(state.rowSelection)]) + }) + + table.toggleAllRowsSelected(true) + subscription.unsubscribe() + + expect(snapshots).toEqual([['exclude', []]]) + }) + + it('does not report all-selected in include mode, because loaded rows are not all rows', () => { + const table = makeSelectionTable({ + manualRowSelection: true, + initialState: { + rowSelection: Object.fromEntries( + flatData.map((row) => [row.id, true as const]), + ), + }, + }) + + expect(table.getIsAllRowsSelected()).toBe(false) + expect(table.getIsSomeRowsSelected()).toBe(true) + expect(table.getIsAllPageRowsSelected()).toBe(true) + + table.toggleAllRowsSelected() + expect(mode(table)).toBe('exclude') + expect(ids(table)).toEqual([]) + }) + + it('selects nothing when rowCount is 0', () => { + const table = makeSelectionTable( + { manualRowSelection: true, rowCount: 0 }, + [], + ) + + table.toggleAllRowsSelected(true) + + expect(mode(table)).toBe('include') + expect(table.getIsAllRowsSelected()).toBe(false) + expect(table.getIsSomeRowsSelected()).toBe(false) + expect(table.getSelectedRowCount()).toBe(0) + + table.toggleAllRowsSelected() + expect(mode(table)).toBe('include') + }) + + it('selects nothing when row selection is off', () => { + const table = makeSelectionTable({ + manualRowSelection: true, + rowCount: 6, + enableRowSelection: false, + initialState: { rowSelection: { '0': true } }, + }) + + table.toggleAllRowsSelected(true) + expect(mode(table)).toBe('include') + expect(ids(table)).toEqual(['0']) + + table.toggleAllRowsSelected() + expect(mode(table)).toBe('include') + expect(ids(table)).toEqual(['0']) + + table.toggleAllRowsSelected(false) + expect(ids(table)).toEqual([]) + }) +}) + +describe('row selection in exclude mode', () => { + it('matches the example of the proposal', () => { + const table = makePipelineTable( + { + manualPagination: true, + manualRowSelection: true, + rowCount: 100_000, + }, + makeRows(0, 10), + ) + + table.toggleAllRowsSelected(true) + expect(table.store.state.rowSelectionMode).toBe('exclude') + + table.getRow('7').toggleSelected(false) + expect(table.store.state.rowSelection['7']).toBe(true) + + expect(table.getIsAllRowsSelected()).toBe(false) + expect(table.getIsSomeRowsSelected()).toBe(true) + expect(table.getSelectedRowCount()).toBe(99_999) + }) + + it('reads a row as selected when its id is not in the state', () => { + const table = makeSelectionTable({ + manualRowSelection: true, + initialState: { + rowSelection: { '1': true }, + rowSelectionMode: 'exclude', + }, + }) + + expect(table.getRow('0').getIsSelected()).toBe(true) + expect(table.getRow('1').getIsSelected()).toBe(false) + }) + + it('adds the id on deselect and removes it on select', () => { + const table = makeSelectionTable({ manualRowSelection: true }) + table.toggleAllRowsSelected(true) + + table.getRow('2').toggleSelected(false) + table.getRow('4').toggleSelected() + expect(ids(table)).toEqual(['2', '4']) + + table.getRow('2').toggleSelected(true) + table.getRow('4').toggleSelected() + expect(ids(table)).toEqual([]) + expect(mode(table)).toBe('exclude') + expect(table.getIsAllRowsSelected()).toBe(true) + }) + + it('shows rows that load later as selected', () => { + const table = makePipelineTable( + { manualPagination: true, manualRowSelection: true, rowCount: 12 }, + makeRows(0, 6), + ) + table.toggleAllRowsSelected(true) + table.getRow('3').toggleSelected(false) + + table.setOptions((prev) => ({ ...prev, data: makeRows(6, 6) })) + + expect(table.getRowModel().rows.map((row) => row.getIsSelected())).toEqual([ + true, + true, + true, + true, + true, + true, + ]) + expect(table.getIsAllPageRowsSelected()).toBe(true) + expect(table.getIsAllRowsSelected()).toBe(false) + expect(table.getSelectedRowCount()).toBe(11) + expect(ids(table)).toEqual(['3']) + }) + + it('does not select a row that cannot be selected again', () => { + const table = makeSelectionTable({ + manualRowSelection: true, + enableRowSelection: (row) => row.id !== '1', + initialState: { + rowSelection: { '1': true, '2': true }, + rowSelectionMode: 'exclude', + }, + }) + + table.getRow('1').toggleSelected(true) + interact(table.getRow('1'), true) + expect(ids(table)).toEqual(['1', '2']) + + table.getRow('2').toggleSelected(true) + expect(ids(table)).toEqual(['1']) + }) + + it('reads a loaded row that cannot be selected as selected', () => { + const table = makeSelectionTable( + { + manualRowSelection: true, + rowCount: 3, + enableRowSelection: (row) => row.id !== '2', + }, + makeRows(0, 3), + ) + table.toggleAllRowsSelected(true) + + interact(table.getRow('0'), false) + interact(table.getRow('1'), false) + interact(table.getRow('2'), false) + table.toggleAllPageRowsSelected(false) + + expect(mode(table)).toBe('exclude') + expect(ids(table)).toEqual(['0', '1']) + expect(table.getRow('2').getCanSelect()).toBe(false) + expect(table.getRow('2').getIsSelected()).toBe(true) + expect(table.getSelectedRowModel().rows.map((row) => row.id)).toEqual(['2']) + expect(table.getSelectedRowIds()).toEqual(['2']) + expect(table.getSelectedRowCount()).toBe(1) + expect(table.getIsSomeRowsSelected()).toBe(true) + expect(table.getIsSomePageRowsSelected()).toBe(false) + }) + + it('goes back to include mode with one id when a single-select row is selected', () => { + const table = makeSelectionTable({ + manualRowSelection: true, + enableMultiRowSelection: false, + initialState: { + rowSelection: { '1': true, '2': true }, + rowSelectionMode: 'exclude', + }, + }) + + table.getRow('2').toggleSelected(true) + + expect(mode(table)).toBe('include') + expect(ids(table)).toEqual(['2']) + expect( + flatData.filter((row) => table.getRow(row.id).getIsSelected()), + ).toEqual([flatData[2]]) + }) + + it('applies a Shift range to the deselected ids', () => { + const table = makeSelectionTable({ manualRowSelection: true }) + table.toggleAllRowsSelected(true) + + interact(table.getRow('1'), false) + interact(table.getRow('3'), false, { shiftKey: true }) + expect(ids(table)).toEqual(['1', '2', '3']) + + interact(table.getRow('2'), true, { shiftKey: true }) + expect(ids(table)).toEqual(['1']) + expect(mode(table)).toBe('exclude') + }) + + it('supports controlled mode state', () => { + let rowSelection: RowSelectionState = {} + let rowSelectionMode: RowSelectionMode = 'include' + const table = makeSelectionTable({ + manualRowSelection: true, + state: { rowSelection, rowSelectionMode }, + onRowSelectionChange: (updater) => { + rowSelection = + typeof updater === 'function' ? updater(rowSelection) : updater + table.setOptions((prev) => ({ + ...prev, + state: { ...prev.state, rowSelection }, + })) + }, + onRowSelectionModeChange: (updater) => { + rowSelectionMode = + typeof updater === 'function' ? updater(rowSelectionMode) : updater + table.setOptions((prev) => ({ + ...prev, + state: { ...prev.state, rowSelectionMode }, + })) + }, + }) + + table.toggleAllRowsSelected(true) + table.getRow('5').toggleSelected(false) + + expect(rowSelectionMode).toBe('exclude') + expect(rowSelection).toEqual({ '5': true }) + expect(table.getRow('5').getIsSelected()).toBe(false) + expect(table.getRow('0').getIsSelected()).toBe(true) + }) +}) + +describe('getSelectedRowCount', () => { + it('counts the ids in include mode', () => { + const table = makeSelectionTable({ + manualRowSelection: true, + rowCount: 100, + initialState: { rowSelection: { '0': true, '1': true, '99': true } }, + }) + + expect(table.getSelectedRowCount()).toBe(3) + }) + + it('subtracts the deselected ids from rowCount in exclude mode', () => { + const table = makeSelectionTable({ + manualRowSelection: true, + rowCount: 100, + initialState: { + rowSelection: { '0': true, '50': true }, + rowSelectionMode: 'exclude', + }, + }) + + expect(table.getSelectedRowCount()).toBe(98) + + table.setOptions((prev) => ({ ...prev, rowCount: 1 })) + expect(table.getSelectedRowCount()).toBe(0) + }) + + it('counts the selected filtered top-level rows when rowCount is not set', () => { + const table = makePipelineTable({ + manualRowSelection: true, + initialState: { + columnFilters: [{ id: 'kind', value: 'even' }], + rowSelection: { '0': true, '1': true }, + rowSelectionMode: 'exclude', + }, + }) + + expect(table.getSelectedRowCount()).toBe(2) + }) + + it('does not subtract loaded sub-rows, because rowCount counts top-level rows', () => { + const table = makeSelectionTable( + { manualRowSelection: true, rowCount: 20 }, + nestedData, + ) + table.toggleAllRowsSelected(true) + + table.getRow('a').toggleSelected(false) + + expect(ids(table)).toEqual(['a', 'a.1', 'a.2']) + expect(table.getSelectedRowCount()).toBe(19) + }) + + it('counts an id that is not loaded as one row', () => { + const table = makeSelectionTable( + { manualRowSelection: true, rowCount: 20 }, + nestedData, + ) + table.toggleAllRowsSelected(true) + table.getRow('a').toggleSelected(false) + + table.setOptions((prev) => ({ ...prev, data: otherNestedData })) + expect(table.getSelectedRowCount()).toBe(17) + + table.setOptions((prev) => ({ ...prev, data: nestedData })) + expect(table.getSelectedRowCount()).toBe(19) + }) + + it('does not count the id of a group row', () => { + const table = makePipelineTable({ + manualRowSelection: true, + rowCount: 6, + initialState: { grouping: ['kind'] }, + }) + table.toggleAllRowsSelected(true) + + table.getRow('kind:even', true).toggleSelected(false) + + expect(ids(table)).toEqual(['0', '2', '4', 'kind:even']) + expect(table.getSelectedRowCount()).toBe(3) + }) + + it('follows rowCount, the selection, the mode, and the loaded rows', () => { + const table = makeSelectionTable( + { manualRowSelection: true, rowCount: 20 }, + nestedData, + ) + expect(table.getSelectedRowCount()).toBe(0) + + table.setRowSelectionMode('exclude') + expect(table.getSelectedRowCount()).toBe(20) + + table.setRowSelection({ 'a.1': true, b: true }) + expect(table.getSelectedRowCount()).toBe(19) + + table.setOptions((prev) => ({ ...prev, rowCount: 10 })) + expect(table.getSelectedRowCount()).toBe(9) + + table.setOptions((prev) => ({ ...prev, data: otherNestedData })) + expect(table.getSelectedRowCount()).toBe(8) + + table.setOptions((prev) => ({ ...prev, rowCount: undefined })) + expect(table.getSelectedRowCount()).toBe(2) + + table.setOptions((prev) => ({ ...prev, data: nestedData })) + expect(table.getSelectedRowCount()).toBe(1) + + table.setOptions((prev) => ({ ...prev, manualRowSelection: false })) + expect(table.getSelectedRowCount()).toBe(2) + }) +}) + +describe('selected row models in exclude mode', () => { + it('contain every loaded row when no id is deselected', () => { + const table = makePipelineTable({ manualRowSelection: true }) + table.toggleAllRowsSelected(true) + + expect(table.getSelectedRowModel().rows).toHaveLength(6) + expect(table.getFilteredSelectedRowModel().rows).toHaveLength(6) + expect(table.getGroupedSelectedRowModel().rows).toHaveLength(6) + expect(table.getSelectedRowIds()).toEqual(['0', '1', '2', '3', '4', '5']) + }) + + it('contain the loaded rows that are not deselected', () => { + const table = makePipelineTable({ + manualRowSelection: true, + initialState: { + columnFilters: [{ id: 'kind', value: 'even' }], + rowSelection: { '0': true, '1': true, '99': true }, + rowSelectionMode: 'exclude', + }, + }) + + expect(table.getSelectedRowModel().flatRows.map((row) => row.id)).toEqual([ + '2', + '3', + '4', + '5', + ]) + expect(Object.keys(table.getSelectedRowModel().rowsById)).toEqual([ + '2', + '3', + '4', + '5', + ]) + expect( + table.getFilteredSelectedRowModel().flatRows.map((row) => row.id), + ).toEqual(['2', '4']) + expect( + table.getGroupedSelectedRowModel().flatRows.map((row) => row.id), + ).toEqual(['2', '4']) + expect(table.getSelectedRowIds()).toEqual(['2', '3', '4', '5']) + }) + + it('keep selected sub-rows of a deselected parent', () => { + const table = makeSelectionTable( + { + manualRowSelection: true, + initialState: { + rowSelection: { a: true, 'a.1': true }, + rowSelectionMode: 'exclude', + }, + }, + nestedData, + ) + const selected = table.getSelectedRowModel() + + expect(selected.rows.map((row) => row.id)).toEqual(['b']) + expect(selected.flatRows.map((row) => row.id)).toEqual(['a.2', 'b', 'b.1']) + }) +}) + +describe('page selection in exclude mode', () => { + function makePagedTable() { + const table = makePipelineTable({ + manualRowSelection: true, + initialState: { pagination: { pageIndex: 0, pageSize: 2 } }, + }) + table.toggleAllRowsSelected(true) + return table + } + + it('reads the page state from the deselected ids', () => { + const table = makePagedTable() + expect(table.getIsAllPageRowsSelected()).toBe(true) + expect(table.getIsSomePageRowsSelected()).toBe(true) + + table.getRow('0').toggleSelected(false) + expect(table.getIsAllPageRowsSelected()).toBe(false) + expect(table.getIsSomePageRowsSelected()).toBe(true) + + table.getRow('1').toggleSelected(false) + expect(table.getIsAllPageRowsSelected()).toBe(false) + expect(table.getIsSomePageRowsSelected()).toBe(false) + }) + + it('deselects and selects the page rows only', () => { + const table = makePagedTable() + + table.toggleAllPageRowsSelected(false) + expect(ids(table)).toEqual(['0', '1']) + expect(mode(table)).toBe('exclude') + expect(table.getRow('2').getIsSelected()).toBe(true) + + table.toggleAllPageRowsSelected() + expect(ids(table)).toEqual([]) + + table.toggleAllPageRowsSelected() + expect(ids(table)).toEqual(['0', '1']) + + table.getToggleAllPageRowsSelectedHandler()({ target: { checked: true } }) + expect(ids(table)).toEqual([]) + }) + + it('keeps page rows that cannot be selected out of the deselected ids', () => { + const table = makePipelineTable({ + manualRowSelection: true, + enableRowSelection: (row) => row.id !== '0', + initialState: { + pagination: { pageIndex: 0, pageSize: 2 }, + rowSelectionMode: 'exclude', + }, + }) + + table.toggleAllPageRowsSelected(false) + + expect(ids(table)).toEqual(['1']) + }) + + it('deselects every row with the deselectAll option', () => { + const table = makePagedTable() + table.getRow('4').toggleSelected(false) + + table.toggleAllPageRowsSelected(false, { deselectAll: true }) + + expect(mode(table)).toBe('include') + expect(ids(table)).toEqual([]) + }) +}) + +describe('sub-row selection in exclude mode', () => { + function makeNestedTable( + options: Parameters[0] = {}, + ) { + const table = makeSelectionTable( + { manualRowSelection: true, ...options }, + nestedData, + ) + table.toggleAllRowsSelected(true) + return table + } + + it('deselects and selects the sub-rows with their parent', () => { + const table = makeNestedTable() + + table.getRow('a').toggleSelected(false) + expect(ids(table)).toEqual(['a', 'a.1', 'a.2']) + expect(table.getRow('a.1').getIsSelected()).toBe(false) + expect(table.getRow('b.1').getIsSelected()).toBe(true) + + table.getRow('a').toggleSelected(true) + expect(ids(table)).toEqual([]) + }) + + it('leaves the sub-rows alone when selectChildren is false or sub-row selection is off', () => { + const table = makeNestedTable() + table.getRow('a').toggleSelected(false, { selectChildren: false }) + expect(ids(table)).toEqual(['a']) + + const blocked = makeNestedTable({ enableSubRowSelection: false }) + blocked.getRow('a').toggleSelected(false) + expect(ids(blocked)).toEqual(['a']) + }) + + it('reports the sub-row state of a parent', () => { + const table = makeNestedTable() + const parent = table.getRow('a') + + expect(parent.getIsAllSubRowsSelected()).toBe(true) + expect(parent.getIsSomeSelected()).toBe(false) + + table.getRow('a.1').toggleSelected(false) + expect(parent.getIsAllSubRowsSelected()).toBe(false) + expect(parent.getIsSomeSelected()).toBe(true) + expect(parent.getIsSelected()).toBe(true) + + table.getRow('a.2').toggleSelected(false) + expect(parent.getIsAllSubRowsSelected()).toBe(false) + expect(parent.getIsSomeSelected()).toBe(false) + }) + + it('deselects the ancestors with the deselectParents option', () => { + const table = makeNestedTable() + + table.getRow('a.1').toggleSelected(false, { deselectParents: true }) + + expect(ids(table)).toEqual(['a', 'a.1']) + expect(table.getRow('a').getIsSelected()).toBe(false) + expect(table.getRow('a.2').getIsSelected()).toBe(true) + }) +}) + +describe('exclude mode when the deselected rows cover every row', () => { + it('goes back to an empty include selection when rowCount is known', () => { + const onRowSelectionModeChange = vi.fn() + const table = makeSelectionTable( + { manualRowSelection: true, rowCount: 3 }, + makeRows(0, 3), + ) + table.toggleAllRowsSelected(true) + + table.getRow('0').toggleSelected(false) + table.getRow('1').toggleSelected(false) + expect(mode(table)).toBe('exclude') + expect(table.getSelectedRowCount()).toBe(1) + + table.setOptions((prev) => ({ ...prev, onRowSelectionModeChange })) + table.getRow('2').toggleSelected(false) + + expect(onRowSelectionModeChange).toHaveBeenCalledWith('include') + }) + + it('leaves no row selected after the last row is deselected', () => { + const table = makeSelectionTable( + { manualRowSelection: true, rowCount: 3 }, + makeRows(0, 3), + ) + table.toggleAllRowsSelected(true) + + table.getRow('0').toggleSelected(false) + table.getRow('1').toggleSelected(false) + table.getRow('2').toggleSelected(false) + + expect(mode(table)).toBe('include') + expect(ids(table)).toEqual([]) + expect(table.getIsSomeRowsSelected()).toBe(false) + expect(table.getSelectedRowCount()).toBe(0) + expect(table.getRow('0').getIsSelected()).toBe(false) + }) + + it('also applies to page and range deselection', () => { + const paged = makePipelineTable( + { + manualPagination: true, + manualRowSelection: true, + rowCount: 3, + }, + makeRows(0, 3), + ) + paged.toggleAllRowsSelected(true) + paged.toggleAllPageRowsSelected(false) + expect(mode(paged)).toBe('include') + expect(ids(paged)).toEqual([]) + + const ranged = makeSelectionTable( + { manualRowSelection: true, rowCount: 3 }, + makeRows(0, 3), + ) + ranged.toggleAllRowsSelected(true) + interact(ranged.getRow('0'), false) + interact(ranged.getRow('2'), false, { shiftKey: true }) + expect(mode(ranged)).toBe('include') + expect(ids(ranged)).toEqual([]) + }) + + it('stays in exclude mode when rowCount is not known', () => { + const table = makeSelectionTable( + { manualRowSelection: true }, + makeRows(0, 2), + ) + table.toggleAllRowsSelected(true) + + table.getRow('0').toggleSelected(false) + table.getRow('1').toggleSelected(false) + + expect(mode(table)).toBe('exclude') + expect(ids(table)).toEqual(['0', '1']) + expect(table.getSelectedRowCount()).toBe(0) + }) + + it('does not count loaded sub-rows against rowCount', () => { + const table = makeSelectionTable( + { manualRowSelection: true, rowCount: 2 }, + nestedData, + ) + table.toggleAllRowsSelected(true) + + table.getRow('a').toggleSelected(false) + expect(mode(table)).toBe('exclude') + expect(ids(table)).toEqual(['a', 'a.1', 'a.2']) + expect(table.getRow('b').getIsSelected()).toBe(true) + + table.getRow('b').toggleSelected(false) + expect(mode(table)).toBe('include') + expect(ids(table)).toEqual([]) + }) + + it('applies across pages when every row was deselected', () => { + const table = makeSelectionTable( + { manualRowSelection: true, rowCount: 4 }, + makeRows(0, 2), + ) + table.toggleAllRowsSelected(true) + table.toggleAllPageRowsSelected(false) + + table.setOptions((prev) => ({ ...prev, data: makeRows(2, 2) })) + table.getRow('2').toggleSelected(false) + expect(mode(table)).toBe('exclude') + + table.getRow('3').toggleSelected(false) + expect(mode(table)).toBe('include') + expect(ids(table)).toEqual([]) + }) + + it('keeps exclude mode while a loaded sub-row is selected', () => { + const blocked = makeSelectionTable( + { manualRowSelection: true, rowCount: 2, enableSubRowSelection: false }, + nestedData, + ) + blocked.toggleAllRowsSelected(true) + interact(blocked.getRow('a'), false) + interact(blocked.getRow('b'), false) + + expect(mode(blocked)).toBe('exclude') + expect(ids(blocked)).toEqual(['a', 'b']) + expect(blocked.getRow('a.1').getIsSelected()).toBe(true) + expect(blocked.getSelectedRowIds()).toEqual(['a.1', 'a.2', 'b.1']) + + const parentsOnly = makeSelectionTable( + { manualRowSelection: true, rowCount: 2 }, + nestedData, + ) + parentsOnly.toggleAllRowsSelected(true) + parentsOnly.getRow('a').toggleSelected(false, { selectChildren: false }) + parentsOnly.getRow('b').toggleSelected(false, { selectChildren: false }) + + expect(mode(parentsOnly)).toBe('exclude') + expect(parentsOnly.getRow('b.1').getIsSelected()).toBe(true) + + const pruned = makeSelectionTable( + { manualRowSelection: true, rowCount: 2 }, + nestedData, + ) + pruned.toggleAllRowsSelected(true) + pruned.getRow('a.1').toggleSelected(false, { deselectParents: true }) + pruned.getRow('b.1').toggleSelected(false, { deselectParents: true }) + + expect(mode(pruned)).toBe('exclude') + expect(pruned.getRow('a.2').getIsSelected()).toBe(true) + + pruned.getRow('a.2').toggleSelected(false) + expect(mode(pruned)).toBe('include') + expect(ids(pruned)).toEqual([]) + }) + + it('keeps exclude mode when ids of an unloaded page reach rowCount', () => { + const table = makeSelectionTable( + { manualRowSelection: true, rowCount: 4 }, + nestedData, + ) + table.toggleAllRowsSelected(true) + table.getRow('a').toggleSelected(false) + + table.setOptions((prev) => ({ ...prev, data: otherNestedData })) + table.getRow('c').toggleSelected(false, { selectChildren: false }) + + expect(mode(table)).toBe('exclude') + expect(ids(table)).toEqual(['a', 'a.1', 'a.2', 'c']) + expect(table.getRow('d').getIsSelected()).toBe(true) + }) + + it('keeps exclude mode while a row of another group is selected', () => { + const table = makePipelineTable({ + manualRowSelection: true, + rowCount: 6, + initialState: { grouping: ['kind'] }, + }) + table.toggleAllRowsSelected(true) + + table.getRow('kind:even', true).toggleSelected(false) + table.getRow('1').toggleSelected(false) + table.getRow('3').toggleSelected(false) + + expect(mode(table)).toBe('exclude') + expect(table.getRow('5').getIsSelected()).toBe(true) + expect(table.getSelectedRowCount()).toBe(1) + + table.getRow('5').toggleSelected(false) + expect(mode(table)).toBe('include') + expect(ids(table)).toEqual([]) + }) + + it('keeps exclude mode when rowCount changed and loaded rows are selected', () => { + const table = makeSelectionTable( + { manualRowSelection: true, rowCount: 100 }, + makeRows(0, 10), + ) + table.toggleAllRowsSelected(true) + for (const row of table.getRowModel().rows.slice(0, 5)) { + row.toggleSelected(false) + } + + table.setOptions((prev) => ({ + ...prev, + data: makeRows(50, 3), + rowCount: 3, + })) + table.getRow('50').toggleSelected(false) + + expect(mode(table)).toBe('exclude') + expect(table.getRow('51').getIsSelected()).toBe(true) + expect(table.getRow('52').getIsSelected()).toBe(true) + }) + + it('does not apply to a selection that setRowSelection writes', () => { + const table = makeSelectionTable( + { manualRowSelection: true, rowCount: 3 }, + makeRows(0, 3), + ) + table.toggleAllRowsSelected(true) + + table.setRowSelection({ '0': true, '1': true, '2': true }) + + expect(mode(table)).toBe('exclude') + expect(table.getSelectedRowCount()).toBe(0) + }) + + it('does not change the mode when a row is selected', () => { + const table = makeSelectionTable({ + manualRowSelection: true, + rowCount: 2, + initialState: { + rowSelection: { '0': true, '1': true, '2': true }, + rowSelectionMode: 'exclude', + }, + }) + + table.getRow('0').toggleSelected(true) + + expect(mode(table)).toBe('exclude') + expect(ids(table)).toEqual(['1', '2']) + }) +}) + +describe('row selection mode state APIs', () => { + it('resets the mode together with the selection', () => { + const table = makeSelectionTable({ + manualRowSelection: true, + initialState: { + rowSelection: { '1': true }, + rowSelectionMode: 'exclude', + }, + }) + table.toggleAllRowsSelected(false) + table.getRow('3').toggleSelected(true) + + table.resetRowSelection() + expect(mode(table)).toBe('exclude') + expect(ids(table)).toEqual(['1']) + + table.resetRowSelection(true) + expect(mode(table)).toBe('include') + expect(ids(table)).toEqual([]) + }) + + it('sets and resets the mode alone', () => { + const table = makeSelectionTable({ + manualRowSelection: true, + initialState: { rowSelection: { '1': true } }, + }) + + table.setRowSelectionMode('exclude') + expect(mode(table)).toBe('exclude') + expect(ids(table)).toEqual(['1']) + expect(table.getRow('1').getIsSelected()).toBe(false) + + table.setRowSelectionMode((old) => + old === 'exclude' ? 'include' : 'exclude', + ) + expect(mode(table)).toBe('include') + + table.setRowSelectionMode('exclude') + table.resetRowSelectionMode() + expect(mode(table)).toBe('include') + expect(ids(table)).toEqual(['1']) + }) + + it('resets the mode with table.reset', () => { + const table = makeSelectionTable({ manualRowSelection: true }) + table.toggleAllRowsSelected(true) + table.getRow('1').toggleSelected(false) + + table.reset() + + expect(mode(table)).toBe('include') + expect(ids(table)).toEqual([]) + }) +}) + +describe('row selection mode memoization', () => { + it('recomputes the getters when only the mode changes', () => { + const table = makePipelineTable( + { manualRowSelection: true }, + nestedData.concat(makeRows(0, 2)), + ) + const parent = table.getRow('a') + + expect(table.getIsAllRowsSelected()).toBe(false) + expect(table.getIsSomeRowsSelected()).toBe(false) + expect(table.getIsAllPageRowsSelected()).toBe(false) + expect(table.getIsSomePageRowsSelected()).toBe(false) + expect(table.getSelectedRowIds()).toEqual([]) + expect(table.getSelectedRowModel().flatRows).toHaveLength(0) + expect(table.getFilteredSelectedRowModel().flatRows).toHaveLength(0) + expect(table.getGroupedSelectedRowModel().flatRows).toHaveLength(0) + expect(parent.getIsAllSubRowsSelected()).toBe(false) + + table.setRowSelectionMode('exclude') + + expect(table.getIsAllRowsSelected()).toBe(true) + expect(table.getIsSomeRowsSelected()).toBe(true) + expect(table.getIsAllPageRowsSelected()).toBe(true) + expect(table.getIsSomePageRowsSelected()).toBe(true) + expect(table.getSelectedRowIds()).toHaveLength(7) + expect(table.getSelectedRowModel().flatRows).toHaveLength(7) + expect(table.getFilteredSelectedRowModel().flatRows).toHaveLength(7) + expect(table.getGroupedSelectedRowModel().flatRows).toHaveLength(7) + expect(parent.getIsAllSubRowsSelected()).toBe(true) + }) + + it('recomputes the getters when the option changes', () => { + const table = makeSelectionTable({ + manualRowSelection: true, + initialState: { rowSelectionMode: 'exclude' }, + }) + + expect(table.getIsAllRowsSelected()).toBe(true) + expect(table.getSelectedRowModel().flatRows).toHaveLength(6) + + table.setOptions((prev) => ({ ...prev, manualRowSelection: false })) + + expect(table.getIsAllRowsSelected()).toBe(false) + expect(table.getIsSomeRowsSelected()).toBe(false) + expect(table.getSelectedRowModel().flatRows).toHaveLength(0) + expect(table.getRow('0').getIsSelected()).toBe(false) + }) + + it('lists the selected ids of new data in exclude mode', () => { + const table = makeSelectionTable( + { manualRowSelection: true }, + makeRows(0, 2), + ) + table.toggleAllRowsSelected(true) + expect(table.getSelectedRowIds()).toEqual(['0', '1']) + + table.setOptions((prev) => ({ ...prev, data: makeRows(2, 2) })) + + expect(table.getSelectedRowIds()).toEqual(['2', '3']) + }) + + it('keeps the selected id list stable across data changes in include mode', () => { + const table = makeSelectionTable({ + initialState: { rowSelection: { '0': true } }, + }) + const selectedRowIds = table.getSelectedRowIds() + + table.setOptions((prev) => ({ ...prev, data: makeRows(2, 2) })) + + expect(table.getSelectedRowIds()).toBe(selectedRowIds) + }) +}) diff --git a/packages/table-core/tests/unit/features/row-selection/rowSelectionFeature.utils.test.ts b/packages/table-core/tests/unit/features/row-selection/rowSelectionFeature.utils.test.ts index 162c08eb66..59d58ae388 100644 --- a/packages/table-core/tests/unit/features/row-selection/rowSelectionFeature.utils.test.ts +++ b/packages/table-core/tests/unit/features/row-selection/rowSelectionFeature.utils.test.ts @@ -6,6 +6,7 @@ import { rowSelectionFeature, } from '../../../../src' import { + getDefaultRowSelectionModeState, getDefaultRowSelectionState, row_getCanMultiSelect, row_getCanSelect, @@ -20,11 +21,14 @@ import { table_getIsSomePageRowsSelected, table_getIsSomeRowsSelected, table_getPreSelectedRowModel, + table_getSelectedRowCount, table_getSelectedRowIds, table_getToggleAllPageRowsSelectedHandler, table_getToggleAllRowsSelectedHandler, table_resetRowSelection, + table_resetRowSelectionMode, table_setRowSelection, + table_setRowSelectionMode, table_toggleAllPageRowsSelected, table_toggleAllRowsSelected, } from '../../../../src/static-functions' @@ -89,6 +93,50 @@ describe('getDefaultRowSelectionState', () => { }) }) +describe('getDefaultRowSelectionModeState', () => { + it('should return the include mode', () => { + expect(getDefaultRowSelectionModeState()).toBe('include') + }) +}) + +describe('table_setRowSelectionMode', () => { + it('should route the updater through onRowSelectionModeChange', () => { + const onRowSelectionModeChange = vi.fn() + const table = makeTable({ onRowSelectionModeChange }) + + table_setRowSelectionMode(table, 'exclude') + + expect(onRowSelectionModeChange).toHaveBeenCalledWith('exclude') + }) +}) + +describe('table_resetRowSelectionMode', () => { + it('should reset to the include mode when defaultState is true', () => { + const onRowSelectionModeChange = vi.fn() + const table = makeTable({ + onRowSelectionModeChange, + initialState: { rowSelectionMode: 'exclude' }, + }) + + table_resetRowSelectionMode(table, true) + + expect(onRowSelectionModeChange).toHaveBeenCalledWith('include') + }) + + it('should reset to the initial mode by default', () => { + const onRowSelectionModeChange = vi.fn() + const table = makeTable({ + onRowSelectionModeChange, + initialState: { rowSelectionMode: 'exclude' }, + state: { rowSelectionMode: 'include' }, + }) + + table_resetRowSelectionMode(table) + + expect(onRowSelectionModeChange).toHaveBeenCalledWith('exclude') + }) +}) + describe('table_setRowSelection', () => { it('should route the updater through onRowSelectionChange', () => { const onRowSelectionChange = vi.fn() @@ -141,6 +189,24 @@ describe('table_resetRowSelection', () => { expect(onRowSelectionChange).toHaveBeenCalledWith({ '0': true, '2': true }) }) + + it('should only reset the mode with manualRowSelection', () => { + const onRowSelectionModeChange = vi.fn() + const table = makeTable({ + onRowSelectionModeChange, + initialState: { rowSelectionMode: 'exclude' }, + }) + + table_resetRowSelection(table) + expect(onRowSelectionModeChange).not.toHaveBeenCalled() + + table.setOptions((prev) => ({ ...prev, manualRowSelection: true })) + table_resetRowSelection(table) + expect(onRowSelectionModeChange).toHaveBeenLastCalledWith('exclude') + + table_resetRowSelection(table, true) + expect(onRowSelectionModeChange).toHaveBeenLastCalledWith('include') + }) }) describe('table_getPreSelectedRowModel', () => { @@ -165,6 +231,32 @@ describe('table_getSelectedRowIds', () => { }) }) +describe('table_getSelectedRowCount', () => { + it('should count the selected row ids', () => { + const table = makeTable({ + initialState: { rowSelection: { '0': true, '3': true } }, + }) + + expect(table_getSelectedRowCount(table)).toBe(2) + expect(table_getSelectedRowCount(makeTable())).toBe(0) + }) + + it('should count the rows that are not deselected in exclude mode', () => { + const table = makeTable({ + manualRowSelection: true, + initialState: { + rowSelection: { '0': true, '3': true }, + rowSelectionMode: 'exclude', + }, + }) + + expect(table_getSelectedRowCount(table)).toBe(3) + + table.setOptions((prev) => ({ ...prev, rowCount: 50 })) + expect(table_getSelectedRowCount(table)).toBe(48) + }) +}) + describe('table_getIsSomeRowsSelected', () => { it('should return false with no selection', () => { expect(table_getIsSomeRowsSelected(makeTable())).toBe(false) From 98dc9843cbe6f1550ec4ea9cc9251d9210f989cc Mon Sep 17 00:00:00 2001 From: Albert Nimtz Date: Fri, 27 Nov 2026 13:09:17 +0100 Subject: [PATCH 2/6] docs: say that the exclude selection count needs the exact rowCount In 'exclude' mode, table.getSelectedRowCount() is rowCount minus the deselected rows. The docs did not say what happens when the total is not known and rowCount is an estimate: the count is then wrong. The row selection guides, the skill reference, and the TSDoc now say that rowCount must be the exact total of the server. They also say what to do when the total is not known: show the number of deselected rows, or keep the total in app state and calculate the count from it. Co-Authored-By: Claude Fable 5.2 --- docs/framework/alpine/guide/row-selection.md | 1 + docs/framework/angular/guide/row-selection.md | 1 + docs/framework/ember/guide/row-selection.md | 1 + docs/framework/lit/guide/row-selection.md | 1 + docs/framework/octane/guide/row-selection.md | 1 + docs/framework/preact/guide/row-selection.md | 1 + docs/framework/react/guide/row-selection.md | 1 + docs/framework/solid/guide/row-selection.md | 1 + docs/framework/svelte/guide/row-selection.md | 1 + docs/framework/vue/guide/row-selection.md | 1 + .../skills/table-features/references/row-selection.md | 2 +- .../src/features/row-selection/rowSelectionFeature.types.ts | 4 +++- .../src/features/row-selection/rowSelectionFeature.utils.ts | 4 +++- 13 files changed, 17 insertions(+), 3 deletions(-) diff --git a/docs/framework/alpine/guide/row-selection.md b/docs/framework/alpine/guide/row-selection.md index 1fbfb50811..26e7d33831 100644 --- a/docs/framework/alpine/guide/row-selection.md +++ b/docs/framework/alpine/guide/row-selection.md @@ -259,6 +259,7 @@ In `'exclude'` mode: - `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. - `table.getIsSomeRowsSelected()` returns `true`. - `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. - When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. diff --git a/docs/framework/angular/guide/row-selection.md b/docs/framework/angular/guide/row-selection.md index d65253ccda..665916a49b 100644 --- a/docs/framework/angular/guide/row-selection.md +++ b/docs/framework/angular/guide/row-selection.md @@ -258,6 +258,7 @@ In `'exclude'` mode: - `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. - `table.getIsSomeRowsSelected()` returns `true`. - `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. - When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. diff --git a/docs/framework/ember/guide/row-selection.md b/docs/framework/ember/guide/row-selection.md index e8c8e841a1..fc3f828c10 100644 --- a/docs/framework/ember/guide/row-selection.md +++ b/docs/framework/ember/guide/row-selection.md @@ -250,6 +250,7 @@ In `'exclude'` mode: - `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. - `table.getIsSomeRowsSelected()` returns `true`. - `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. - When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. diff --git a/docs/framework/lit/guide/row-selection.md b/docs/framework/lit/guide/row-selection.md index aff55a0c04..ae62bcd161 100644 --- a/docs/framework/lit/guide/row-selection.md +++ b/docs/framework/lit/guide/row-selection.md @@ -265,6 +265,7 @@ In `'exclude'` mode: - `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. - `table.getIsSomeRowsSelected()` returns `true`. - `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. - When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. diff --git a/docs/framework/octane/guide/row-selection.md b/docs/framework/octane/guide/row-selection.md index 40a6d4a39d..67d06f48a0 100644 --- a/docs/framework/octane/guide/row-selection.md +++ b/docs/framework/octane/guide/row-selection.md @@ -248,6 +248,7 @@ In `'exclude'` mode: - `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. - `table.getIsSomeRowsSelected()` returns `true`. - `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. - When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. diff --git a/docs/framework/preact/guide/row-selection.md b/docs/framework/preact/guide/row-selection.md index c73921eb82..27f9e9481b 100644 --- a/docs/framework/preact/guide/row-selection.md +++ b/docs/framework/preact/guide/row-selection.md @@ -248,6 +248,7 @@ In `'exclude'` mode: - `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. - `table.getIsSomeRowsSelected()` returns `true`. - `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. - When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. diff --git a/docs/framework/react/guide/row-selection.md b/docs/framework/react/guide/row-selection.md index e8d857e2ba..5e46d31710 100644 --- a/docs/framework/react/guide/row-selection.md +++ b/docs/framework/react/guide/row-selection.md @@ -249,6 +249,7 @@ In `'exclude'` mode: - `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. - `table.getIsSomeRowsSelected()` returns `true`. - `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. - When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. diff --git a/docs/framework/solid/guide/row-selection.md b/docs/framework/solid/guide/row-selection.md index 8010c4e7ab..496cccd605 100644 --- a/docs/framework/solid/guide/row-selection.md +++ b/docs/framework/solid/guide/row-selection.md @@ -254,6 +254,7 @@ In `'exclude'` mode: - `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. - `table.getIsSomeRowsSelected()` returns `true`. - `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. - When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. diff --git a/docs/framework/svelte/guide/row-selection.md b/docs/framework/svelte/guide/row-selection.md index 9efb11c665..e9581c7337 100644 --- a/docs/framework/svelte/guide/row-selection.md +++ b/docs/framework/svelte/guide/row-selection.md @@ -263,6 +263,7 @@ In `'exclude'` mode: - `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. - `table.getIsSomeRowsSelected()` returns `true`. - `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. - When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. diff --git a/docs/framework/vue/guide/row-selection.md b/docs/framework/vue/guide/row-selection.md index 9384cc3512..c82e45237b 100644 --- a/docs/framework/vue/guide/row-selection.md +++ b/docs/framework/vue/guide/row-selection.md @@ -253,6 +253,7 @@ In `'exclude'` mode: - `table.getIsAllRowsSelected()` returns `true` only when `rowSelection` is empty. With `manualRowSelection`, the loaded rows are not all rows, so this API never returns `true` in `'include'` mode. - `table.getIsSomeRowsSelected()` returns `true`. - `table.getSelectedRowCount()` returns `rowCount` minus the number of deselected rows. If you do not set `rowCount`, the table can only count the loaded rows. In `'include'` mode, it returns the number of ids in `rowSelection`. +- For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. - When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. diff --git a/packages/table-core/skills/table-features/references/row-selection.md b/packages/table-core/skills/table-features/references/row-selection.md index 4420fdd17e..f561ab4d85 100644 --- a/packages/table-core/skills/table-features/references/row-selection.md +++ b/packages/table-core/skills/table-features/references/row-selection.md @@ -86,7 +86,7 @@ const bulkAction = { `manualRowSelection: true` allows the `'exclude'` value of the `rowSelectionMode` state; without the option the mode is always read as `'include'` and select-all writes the loaded row ids. With it, `toggleAllRowsSelected(true)` sets `'exclude'` and clears `rowSelection`, `toggleAllRowsSelected(false)` sets `'include'` and clears it, and `resetRowSelection()` resets both slices. In `'exclude'` mode a row is selected when its id is not in `rowSelection`, so rows that load later are selected, `getIsAllRowsSelected()` is true only with no ids, and the selected row models and `getSelectedRowIds()` hold only loaded rows. `getIsAllRowsSelected()` is never true in `'include'` mode under this option, because loaded rows are not all rows. -Set `rowCount` (top-level rows, the same option manual pagination reads) so `getSelectedRowCount()` is right and so a row, range, or page deselect goes back to an empty `'include'` selection when the deselected rows cover every row and no loaded row is still selected. Direct `setRowSelection()` writes and updaters applied later are not normalized. The count skips the ids of loaded sub-rows and group rows but subtracts an id that is not loaded as one row, so with tree data on more than one page take the count from the server. The table does not reset the selection when filters change: "all" means all rows matching the filters at click time, and the deselected ids and `rowCount` belong to those filters, so call `resetRowSelection()` whenever filters or `rowCount` change. +Set `rowCount` (top-level rows, the same option manual pagination reads) so `getSelectedRowCount()` is right and so a row, range, or page deselect goes back to an empty `'include'` selection when the deselected rows cover every row and no loaded row is still selected. Direct `setRowSelection()` writes and updaters applied later are not normalized. `rowCount` must be the exact server total: with an estimated or too-high `rowCount` (total unknown), `getSelectedRowCount()` is wrong in `'exclude'` mode, so do not show it; show the number of deselected ids instead, or count from a total kept in app state. The count skips the ids of loaded sub-rows and group rows but subtracts an id that is not loaded as one row, so with tree data on more than one page take the count from the server. The table does not reset the selection when filters change: "all" means all rows matching the filters at click time, and the deselected ids and `rowCount` belong to those filters, so call `resetRowSelection()` whenever filters or `rowCount` change. `enableRowSelection` only gates toggling. In `'exclude'` mode a row whose id is not in `rowSelection` is selected (`getIsSelected()`, selected row models, `getSelectedRowCount()`) even when it cannot be toggled, so the server must skip such rows if they must not be part of a bulk action. The row handler and the page APIs skip such a row, so `getIsSomePageRowsSelected()` can be false while `getIsSomeRowsSelected()` is true, and the table stays in `'exclude'` mode while the row is loaded. Select-all is a no-op when `enableRowSelection` is `false` and stays in `'include'` mode when `rowCount` is `0`. `enableSubRowSelection` only cascades to loaded sub-rows; the server must decide how a parent applies to sub-rows that are not loaded. diff --git a/packages/table-core/src/features/row-selection/rowSelectionFeature.types.ts b/packages/table-core/src/features/row-selection/rowSelectionFeature.types.ts index 0c8d42fcbb..ac3d6c3737 100644 --- a/packages/table-core/src/features/row-selection/rowSelectionFeature.types.ts +++ b/packages/table-core/src/features/row-selection/rowSelectionFeature.types.ts @@ -195,7 +195,9 @@ export interface Table_RowSelection< * Returns the number of selected rows. * * In `'include'` mode this is the number of ids in `rowSelection`. In - * `'exclude'` mode this is `options.rowCount` minus the deselected rows. + * `'exclude'` mode this is `options.rowCount` minus the deselected rows, so + * `rowCount` must be the exact total of the server. With an estimated + * total, the result is wrong. * `rowCount` counts top-level rows, so the ids of loaded sub-rows and of * group rows are not subtracted. An id that is not loaded is subtracted as * one row, because the table cannot know that it is a sub-row. When diff --git a/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts b/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts index 2deb42fc29..afea5ee983 100644 --- a/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts +++ b/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts @@ -489,7 +489,9 @@ export function table_getSelectedRowIds< * Returns the number of selected rows. * * In `'include'` mode this is the number of ids in `rowSelection`. In - * `'exclude'` mode this is `options.rowCount` minus the deselected rows. + * `'exclude'` mode this is `options.rowCount` minus the deselected rows, so + * `rowCount` must be the exact total of the server. With an estimated total, + * the result is wrong. * `rowCount` counts top-level rows, so the ids of loaded sub-rows and of group * rows are not subtracted. An id that is not loaded is subtracted as one row, * because the table cannot know that it is a sub-row. When `rowCount` is not From 0fa927e7d1d9b75dbc5c88ceb69c528d070d9957 Mon Sep 17 00:00:00 2001 From: Albert Nimtz Date: Sat, 28 Nov 2026 18:41:01 +0100 Subject: [PATCH 3/6] fix(table-core): keep exclude mode while an unloaded id can be a sub-row or group row The count of deselected rows counts an id that is not loaded as one top-level row. With getSubRows, that id can be a sub-row. With a grouped row model, it can be the id of a group row from an earlier grouping. The count was then too high, and a deselect switched back to an empty 'include' selection while unloaded rows were still selected. Co-Authored-By: Claude Opus 5.5 --- .../rowSelectionFeature.utils.ts | 26 +++++++++- .../row-selection/rowSelectionMode.test.ts | 48 ++++++++++++++++++- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts b/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts index afea5ee983..72c45a6691 100644 --- a/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts +++ b/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts @@ -1090,6 +1090,26 @@ function countExcludedRows< return count } +function isFlatTable( + table: Table_Internal, +): boolean { + return !table.options.getSubRows && !table.options.features.groupedRowModel +} + +function isEveryRowIdLoaded< + TFeatures extends TableFeatures, + TData extends RowData, +>( + table: Table_Internal, + rowSelection: RowSelectionState, +): boolean { + const rowsById = table.getCoreRowModel().rowsById + const groupedRowsById = table.getGroupedRowModel().rowsById + return Object.keys(rowSelection).every( + (rowId) => rowsById[rowId] || groupedRowsById[rowId], + ) +} + /** * Applies `mutate` to a copy of the row selection map. * @@ -1099,7 +1119,10 @@ function countExcludedRows< * * The count of deselected rows can be too high: an id can be a sub-row of a * page that is not loaded, or `rowCount` can be the count of a new filter. So - * the state only goes back when no loaded row is selected. + * the state only goes back when no loaded row is selected. With + * `options.getSubRows` or a grouped row model, every deselected id must also + * be loaded, because an id that is not loaded can be a sub-row or the id of a + * group row from an earlier grouping. */ function updateRowSelection< TFeatures extends TableFeatures, @@ -1130,6 +1153,7 @@ function updateRowSelection< if ( countExcludedRows(table, next) >= rowCount && + (isFlatTable(table) || isEveryRowIdLoaded(table, next)) && table.getCoreRowModel().flatRows.every((row) => isRowSelected(row, next)) ) { setRowSelectionAndMode(table, makeObjectMap(), 'include') diff --git a/packages/table-core/tests/implementation/features/row-selection/rowSelectionMode.test.ts b/packages/table-core/tests/implementation/features/row-selection/rowSelectionMode.test.ts index 8451809d07..ed9a4edab7 100644 --- a/packages/table-core/tests/implementation/features/row-selection/rowSelectionMode.test.ts +++ b/packages/table-core/tests/implementation/features/row-selection/rowSelectionMode.test.ts @@ -924,9 +924,9 @@ describe('exclude mode when the deselected rows cover every row', () => { expect(ids(table)).toEqual([]) }) - it('applies across pages when every row was deselected', () => { + it('applies across pages when every row of a flat table was deselected', () => { const table = makeSelectionTable( - { manualRowSelection: true, rowCount: 4 }, + { manualRowSelection: true, rowCount: 4, getSubRows: undefined }, makeRows(0, 2), ) table.toggleAllRowsSelected(true) @@ -998,6 +998,50 @@ describe('exclude mode when the deselected rows cover every row', () => { expect(table.getRow('d').getIsSelected()).toBe(true) }) + it('keeps exclude mode when an id that is not loaded can be a sub-row', () => { + const table = makeSelectionTable( + { manualRowSelection: true, rowCount: 4 }, + nestedData, + ) + table.toggleAllRowsSelected(true) + table.getRow('a').toggleSelected(false) + + table.setOptions((prev) => ({ ...prev, data: [otherNestedData[1]!] })) + table.getRow('d').toggleSelected(false) + + expect(mode(table)).toBe('exclude') + expect(ids(table)).toEqual(['a', 'a.1', 'a.2', 'd']) + + table.setOptions((prev) => ({ ...prev, data: nestedData })) + expect(table.getRow('b').getIsSelected()).toBe(true) + }) + + it('keeps exclude mode when an id that is not loaded can be an old group row', () => { + const table = makePipelineTable( + { + manualPagination: true, + manualRowSelection: true, + rowCount: 4, + getSubRows: undefined, + initialState: { grouping: ['kind'] }, + }, + makeRows(0, 2), + ) + table.toggleAllRowsSelected(true) + table.getRow('kind:even', true).toggleSelected(false) + table.setGrouping([]) + + table.setOptions((prev) => ({ ...prev, data: makeRows(2, 2) })) + table.getRow('2').toggleSelected(false) + table.getRow('3').toggleSelected(false) + + expect(mode(table)).toBe('exclude') + expect(ids(table)).toEqual(['0', '2', '3', 'kind:even']) + + table.setOptions((prev) => ({ ...prev, data: makeRows(0, 2) })) + expect(table.getRow('1').getIsSelected()).toBe(true) + }) + it('keeps exclude mode while a row of another group is selected', () => { const table = makePipelineTable({ manualRowSelection: true, From 012461286fcd56cc46653c215e4b0589998d18c1 Mon Sep 17 00:00:00 2001 From: Albert Nimtz Date: Sat, 28 Nov 2026 18:41:22 +0100 Subject: [PATCH 4/6] test(table-core): drop row selection mode tests that pin implementation details Remove tests that check the internal store batching, a spied mode handler that never applies the change, a memoized result identity, and the default mode constant. Other tests cover the behavior they stand for. Co-Authored-By: Claude Opus 5.5 --- .../row-selection/rowSelectionMode.test.ts | 55 ------------------- .../rowSelectionFeature.utils.test.ts | 7 --- 2 files changed, 62 deletions(-) diff --git a/packages/table-core/tests/implementation/features/row-selection/rowSelectionMode.test.ts b/packages/table-core/tests/implementation/features/row-selection/rowSelectionMode.test.ts index ed9a4edab7..2071a15b32 100644 --- a/packages/table-core/tests/implementation/features/row-selection/rowSelectionMode.test.ts +++ b/packages/table-core/tests/implementation/features/row-selection/rowSelectionMode.test.ts @@ -135,14 +135,6 @@ function mode(table: { } describe('row selection mode without manualRowSelection', () => { - it('adds an include mode slice without changing the selection shape', () => { - const table = makeSelectionTable() - - expect(table.initialState.rowSelectionMode).toBe('include') - expect(table.store.state.rowSelectionMode).toBe('include') - expect(table.store.state.rowSelection).toEqual({}) - }) - it('selects and deselects the loaded row ids and never writes the mode', () => { const onRowSelectionModeChange = vi.fn() const table = makeSelectionTable({ @@ -268,23 +260,6 @@ describe('toggleAllRowsSelected with manualRowSelection', () => { expect(mode(table)).toBe('include') }) - it('writes the selection and the mode in one store update', () => { - const table = makeSelectionTable({ - manualRowSelection: true, - initialState: { rowSelection: { '1': true } }, - }) - const snapshots: Array<[RowSelectionMode, Array]> = [] - const subscription = table.store.subscribe(() => { - const state = table.store.state - snapshots.push([state.rowSelectionMode, Object.keys(state.rowSelection)]) - }) - - table.toggleAllRowsSelected(true) - subscription.unsubscribe() - - expect(snapshots).toEqual([['exclude', []]]) - }) - it('does not report all-selected in include mode, because loaded rows are not all rows', () => { const table = makeSelectionTable({ manualRowSelection: true, @@ -830,25 +805,6 @@ describe('sub-row selection in exclude mode', () => { }) describe('exclude mode when the deselected rows cover every row', () => { - it('goes back to an empty include selection when rowCount is known', () => { - const onRowSelectionModeChange = vi.fn() - const table = makeSelectionTable( - { manualRowSelection: true, rowCount: 3 }, - makeRows(0, 3), - ) - table.toggleAllRowsSelected(true) - - table.getRow('0').toggleSelected(false) - table.getRow('1').toggleSelected(false) - expect(mode(table)).toBe('exclude') - expect(table.getSelectedRowCount()).toBe(1) - - table.setOptions((prev) => ({ ...prev, onRowSelectionModeChange })) - table.getRow('2').toggleSelected(false) - - expect(onRowSelectionModeChange).toHaveBeenCalledWith('include') - }) - it('leaves no row selected after the last row is deselected', () => { const table = makeSelectionTable( { manualRowSelection: true, rowCount: 3 }, @@ -1230,15 +1186,4 @@ describe('row selection mode memoization', () => { expect(table.getSelectedRowIds()).toEqual(['2', '3']) }) - - it('keeps the selected id list stable across data changes in include mode', () => { - const table = makeSelectionTable({ - initialState: { rowSelection: { '0': true } }, - }) - const selectedRowIds = table.getSelectedRowIds() - - table.setOptions((prev) => ({ ...prev, data: makeRows(2, 2) })) - - expect(table.getSelectedRowIds()).toBe(selectedRowIds) - }) }) diff --git a/packages/table-core/tests/unit/features/row-selection/rowSelectionFeature.utils.test.ts b/packages/table-core/tests/unit/features/row-selection/rowSelectionFeature.utils.test.ts index 59d58ae388..811f99b985 100644 --- a/packages/table-core/tests/unit/features/row-selection/rowSelectionFeature.utils.test.ts +++ b/packages/table-core/tests/unit/features/row-selection/rowSelectionFeature.utils.test.ts @@ -6,7 +6,6 @@ import { rowSelectionFeature, } from '../../../../src' import { - getDefaultRowSelectionModeState, getDefaultRowSelectionState, row_getCanMultiSelect, row_getCanSelect, @@ -93,12 +92,6 @@ describe('getDefaultRowSelectionState', () => { }) }) -describe('getDefaultRowSelectionModeState', () => { - it('should return the include mode', () => { - expect(getDefaultRowSelectionModeState()).toBe('include') - }) -}) - describe('table_setRowSelectionMode', () => { it('should route the updater through onRowSelectionModeChange', () => { const onRowSelectionModeChange = vi.fn() From 65f92841602277210294a4fc7ff259b1dac55702 Mon Sep 17 00:00:00 2001 From: Albert Nimtz Date: Sat, 28 Nov 2026 18:41:22 +0100 Subject: [PATCH 5/6] docs: clarify remote row selection limits in the guides and skill - Set a stable getRowId in the remote table example. - Say when the table goes back to 'include' mode with getSubRows or grouping. - Warn about row APIs called before deferred state handlers apply select all. - Reset with resetRowSelection(true) when filters change, and reset the selection when the grouping changes. - Mark getSelectedRowIds() as complete only in 'include' mode, and send the filters with the exclude payload. Co-Authored-By: Claude Opus 5.5 --- docs/framework/alpine/guide/row-selection.md | 8 +++++--- docs/framework/angular/guide/row-selection.md | 8 +++++--- docs/framework/ember/guide/row-selection.md | 8 +++++--- docs/framework/lit/guide/row-selection.md | 8 +++++--- docs/framework/octane/guide/row-selection.md | 8 +++++--- docs/framework/preact/guide/row-selection.md | 8 +++++--- docs/framework/react/guide/row-selection.md | 8 +++++--- docs/framework/solid/guide/row-selection.md | 8 +++++--- docs/framework/svelte/guide/row-selection.md | 8 +++++--- docs/framework/vue/guide/row-selection.md | 8 +++++--- .../skills/table-features/references/row-selection.md | 8 ++++---- 11 files changed, 54 insertions(+), 34 deletions(-) diff --git a/docs/framework/alpine/guide/row-selection.md b/docs/framework/alpine/guide/row-selection.md index 26e7d33831..63464cba48 100644 --- a/docs/framework/alpine/guide/row-selection.md +++ b/docs/framework/alpine/guide/row-selection.md @@ -234,6 +234,7 @@ const table = createTable({ //... manualPagination: true, manualRowSelection: true, // allow the 'exclude' row selection mode + getRowId: (row) => row.id, // a stable server id, not the row index rowCount: 100_000, // the total number of rows on the server }) ``` @@ -262,7 +263,8 @@ In `'exclude'` mode: - For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. -- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. With `getSubRows` or a grouped row model, every deselected id must also be loaded, because an id that is not loaded can be a sub-row or the id of a group row from an earlier grouping. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- If your `onRowSelectionChange` and `onRowSelectionModeChange` apply updates later, do not call a row API in the same event after "select all" or "deselect all". The row API still reads the old mode, so it writes the id for the wrong mode. The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. @@ -284,10 +286,10 @@ await bulkDelete( Some limits apply, because the table cannot see rows that are not loaded: -- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. ### Render Row Selection UI diff --git a/docs/framework/angular/guide/row-selection.md b/docs/framework/angular/guide/row-selection.md index 665916a49b..36f51905ec 100644 --- a/docs/framework/angular/guide/row-selection.md +++ b/docs/framework/angular/guide/row-selection.md @@ -233,6 +233,7 @@ readonly table = injectTable(() => ({ //... manualPagination: true, manualRowSelection: true, // allow the 'exclude' row selection mode + getRowId: (row) => row.id, // a stable server id, not the row index rowCount: 100_000, // the total number of rows on the server })) ``` @@ -261,7 +262,8 @@ In `'exclude'` mode: - For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. -- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. With `getSubRows` or a grouped row model, every deselected id must also be loaded, because an id that is not loaded can be a sub-row or the id of a group row from an earlier grouping. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- If your `onRowSelectionChange` and `onRowSelectionModeChange` apply updates later, do not call a row API in the same event after "select all" or "deselect all". The row API still reads the old mode, so it writes the id for the wrong mode. The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. @@ -283,10 +285,10 @@ await bulkDelete( Some limits apply, because the table cannot see rows that are not loaded: -- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. ### Render Row Selection UI diff --git a/docs/framework/ember/guide/row-selection.md b/docs/framework/ember/guide/row-selection.md index fc3f828c10..225263927e 100644 --- a/docs/framework/ember/guide/row-selection.md +++ b/docs/framework/ember/guide/row-selection.md @@ -225,6 +225,7 @@ const table = useTable(() => ({ //... manualPagination: true, manualRowSelection: true, // allow the 'exclude' row selection mode + getRowId: (row) => row.id, // a stable server id, not the row index rowCount: 100_000, // the total number of rows on the server })) ``` @@ -253,7 +254,8 @@ In `'exclude'` mode: - For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. -- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. With `getSubRows` or a grouped row model, every deselected id must also be loaded, because an id that is not loaded can be a sub-row or the id of a group row from an earlier grouping. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- If your `onRowSelectionChange` and `onRowSelectionModeChange` apply updates later, do not call a row API in the same event after "select all" or "deselect all". The row API still reads the old mode, so it writes the id for the wrong mode. The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. @@ -275,10 +277,10 @@ await bulkDelete( Some limits apply, because the table cannot see rows that are not loaded: -- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. ### Render Row Selection UI diff --git a/docs/framework/lit/guide/row-selection.md b/docs/framework/lit/guide/row-selection.md index ae62bcd161..b0de7eb720 100644 --- a/docs/framework/lit/guide/row-selection.md +++ b/docs/framework/lit/guide/row-selection.md @@ -240,6 +240,7 @@ const table = this.tableController.table({ //... manualPagination: true, manualRowSelection: true, // allow the 'exclude' row selection mode + getRowId: (row) => row.id, // a stable server id, not the row index rowCount: 100_000, // the total number of rows on the server }) ``` @@ -268,7 +269,8 @@ In `'exclude'` mode: - For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. -- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. With `getSubRows` or a grouped row model, every deselected id must also be loaded, because an id that is not loaded can be a sub-row or the id of a group row from an earlier grouping. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- If your `onRowSelectionChange` and `onRowSelectionModeChange` apply updates later, do not call a row API in the same event after "select all" or "deselect all". The row API still reads the old mode, so it writes the id for the wrong mode. The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. @@ -290,10 +292,10 @@ await bulkDelete( Some limits apply, because the table cannot see rows that are not loaded: -- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. ### Render Row Selection UI diff --git a/docs/framework/octane/guide/row-selection.md b/docs/framework/octane/guide/row-selection.md index 67d06f48a0..4d4def3021 100644 --- a/docs/framework/octane/guide/row-selection.md +++ b/docs/framework/octane/guide/row-selection.md @@ -223,6 +223,7 @@ const table = useTable({ //... manualPagination: true, manualRowSelection: true, // allow the 'exclude' row selection mode + getRowId: (row) => row.id, // a stable server id, not the row index rowCount: 100_000, // the total number of rows on the server }) ``` @@ -251,7 +252,8 @@ In `'exclude'` mode: - For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. -- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. With `getSubRows` or a grouped row model, every deselected id must also be loaded, because an id that is not loaded can be a sub-row or the id of a group row from an earlier grouping. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- If your `onRowSelectionChange` and `onRowSelectionModeChange` apply updates later, do not call a row API in the same event after "select all" or "deselect all". The row API still reads the old mode, so it writes the id for the wrong mode. The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. @@ -273,10 +275,10 @@ await bulkDelete( Some limits apply, because the table cannot see rows that are not loaded: -- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. ### Render Row Selection UI diff --git a/docs/framework/preact/guide/row-selection.md b/docs/framework/preact/guide/row-selection.md index 27f9e9481b..71037b8d78 100644 --- a/docs/framework/preact/guide/row-selection.md +++ b/docs/framework/preact/guide/row-selection.md @@ -223,6 +223,7 @@ const table = useTable({ //... manualPagination: true, manualRowSelection: true, // allow the 'exclude' row selection mode + getRowId: (row) => row.id, // a stable server id, not the row index rowCount: 100_000, // the total number of rows on the server }) ``` @@ -251,7 +252,8 @@ In `'exclude'` mode: - For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. -- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. With `getSubRows` or a grouped row model, every deselected id must also be loaded, because an id that is not loaded can be a sub-row or the id of a group row from an earlier grouping. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- If your `onRowSelectionChange` and `onRowSelectionModeChange` apply updates later, do not call a row API in the same event after "select all" or "deselect all". The row API still reads the old mode, so it writes the id for the wrong mode. The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. @@ -273,10 +275,10 @@ await bulkDelete( Some limits apply, because the table cannot see rows that are not loaded: -- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. ### Render Row Selection UI diff --git a/docs/framework/react/guide/row-selection.md b/docs/framework/react/guide/row-selection.md index 5e46d31710..83e0d12c6a 100644 --- a/docs/framework/react/guide/row-selection.md +++ b/docs/framework/react/guide/row-selection.md @@ -224,6 +224,7 @@ const table = useTable({ //... manualPagination: true, manualRowSelection: true, // allow the 'exclude' row selection mode + getRowId: (row) => row.id, // a stable server id, not the row index rowCount: 100_000, // the total number of rows on the server }) ``` @@ -252,7 +253,8 @@ In `'exclude'` mode: - For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. -- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. With `getSubRows` or a grouped row model, every deselected id must also be loaded, because an id that is not loaded can be a sub-row or the id of a group row from an earlier grouping. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- If your `onRowSelectionChange` and `onRowSelectionModeChange` apply updates later, do not call a row API in the same event after "select all" or "deselect all". The row API still reads the old mode, so it writes the id for the wrong mode. The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. @@ -274,10 +276,10 @@ await bulkDelete( Some limits apply, because the table cannot see rows that are not loaded: -- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. ### Render Row Selection UI diff --git a/docs/framework/solid/guide/row-selection.md b/docs/framework/solid/guide/row-selection.md index 496cccd605..a63fc8483c 100644 --- a/docs/framework/solid/guide/row-selection.md +++ b/docs/framework/solid/guide/row-selection.md @@ -229,6 +229,7 @@ const table = createTable({ //... manualPagination: true, manualRowSelection: true, // allow the 'exclude' row selection mode + getRowId: (row) => row.id, // a stable server id, not the row index rowCount: 100_000, // the total number of rows on the server }) ``` @@ -257,7 +258,8 @@ In `'exclude'` mode: - For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. -- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. With `getSubRows` or a grouped row model, every deselected id must also be loaded, because an id that is not loaded can be a sub-row or the id of a group row from an earlier grouping. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- If your `onRowSelectionChange` and `onRowSelectionModeChange` apply updates later, do not call a row API in the same event after "select all" or "deselect all". The row API still reads the old mode, so it writes the id for the wrong mode. The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. @@ -279,10 +281,10 @@ await bulkDelete( Some limits apply, because the table cannot see rows that are not loaded: -- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. ### Render Row Selection UI diff --git a/docs/framework/svelte/guide/row-selection.md b/docs/framework/svelte/guide/row-selection.md index e9581c7337..ad2ca0c7bc 100644 --- a/docs/framework/svelte/guide/row-selection.md +++ b/docs/framework/svelte/guide/row-selection.md @@ -238,6 +238,7 @@ const table = createTable({ //... manualPagination: true, manualRowSelection: true, // allow the 'exclude' row selection mode + getRowId: (row) => row.id, // a stable server id, not the row index rowCount: 100_000, // the total number of rows on the server }) ``` @@ -266,7 +267,8 @@ In `'exclude'` mode: - For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. -- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. With `getSubRows` or a grouped row model, every deselected id must also be loaded, because an id that is not loaded can be a sub-row or the id of a group row from an earlier grouping. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- If your `onRowSelectionChange` and `onRowSelectionModeChange` apply updates later, do not call a row API in the same event after "select all" or "deselect all". The row API still reads the old mode, so it writes the id for the wrong mode. The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. @@ -288,10 +290,10 @@ await bulkDelete( Some limits apply, because the table cannot see rows that are not loaded: -- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. ### Render Row Selection UI diff --git a/docs/framework/vue/guide/row-selection.md b/docs/framework/vue/guide/row-selection.md index c82e45237b..f4bbba09e6 100644 --- a/docs/framework/vue/guide/row-selection.md +++ b/docs/framework/vue/guide/row-selection.md @@ -228,6 +228,7 @@ const table = useTable({ //... manualPagination: true, manualRowSelection: true, // allow the 'exclude' row selection mode + getRowId: (row) => row.id, // a stable server id, not the row index rowCount: 100_000, // the total number of rows on the server }) ``` @@ -256,7 +257,8 @@ In `'exclude'` mode: - For this count, `rowCount` must be the exact total of the server. If you do not know the total and set `rowCount` to an estimate or to a number that is too high, `table.getSelectedRowCount()` is wrong in `'exclude'` mode. Do not show the count then. Show the number of deselected rows, which is the number of ids in `rowSelection`, or keep the total in your own state when the server returns it and calculate the count from it. - `getSelectedRowModel()`, `getFilteredSelectedRowModel()`, `getGroupedSelectedRowModel()`, and `getSelectedRowIds()` return the loaded rows that are selected. The table does not know the rows that are not loaded. - `table.toggleAllPageRowsSelected()`, `table.getIsAllPageRowsSelected()`, and `table.getIsSomePageRowsSelected()` work on the loaded page rows. Deselecting a page adds its row ids to `rowSelection`. -- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- When `rowCount` is set, the deselected rows cover every row, and no loaded row is selected, the table goes back to `'include'` mode with an empty `rowSelection`. With `getSubRows` or a grouped row model, every deselected id must also be loaded, because an id that is not loaded can be a sub-row or the id of a group row from an earlier grouping. The table makes this decision from the current state when a row, range, or page API deselects rows. It does not make it when you write `rowSelection` yourself, or when your `onRowSelectionChange` applies an updater later. +- If your `onRowSelectionChange` and `onRowSelectionModeChange` apply updates later, do not call a row API in the same event after "select all" or "deselect all". The row API still reads the old mode, so it writes the id for the wrong mode. The mode is state, not an option. "Select all", "deselect all", and `table.resetRowSelection()` change it. You can own it like the `rowSelection` slice, with `atoms.rowSelectionMode`, or with `state.rowSelectionMode` and `onRowSelectionModeChange`. The `table.setRowSelectionMode()` and `table.resetRowSelectionMode()` APIs change only the mode and keep the ids. @@ -278,10 +280,10 @@ await bulkDelete( Some limits apply, because the table cannot see rows that are not loaded: -- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection()` when your filters or `rowCount` change. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. +- "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. ### Render Row Selection UI diff --git a/packages/table-core/skills/table-features/references/row-selection.md b/packages/table-core/skills/table-features/references/row-selection.md index f561ab4d85..b9515612f6 100644 --- a/packages/table-core/skills/table-features/references/row-selection.md +++ b/packages/table-core/skills/table-features/references/row-selection.md @@ -86,7 +86,7 @@ const bulkAction = { `manualRowSelection: true` allows the `'exclude'` value of the `rowSelectionMode` state; without the option the mode is always read as `'include'` and select-all writes the loaded row ids. With it, `toggleAllRowsSelected(true)` sets `'exclude'` and clears `rowSelection`, `toggleAllRowsSelected(false)` sets `'include'` and clears it, and `resetRowSelection()` resets both slices. In `'exclude'` mode a row is selected when its id is not in `rowSelection`, so rows that load later are selected, `getIsAllRowsSelected()` is true only with no ids, and the selected row models and `getSelectedRowIds()` hold only loaded rows. `getIsAllRowsSelected()` is never true in `'include'` mode under this option, because loaded rows are not all rows. -Set `rowCount` (top-level rows, the same option manual pagination reads) so `getSelectedRowCount()` is right and so a row, range, or page deselect goes back to an empty `'include'` selection when the deselected rows cover every row and no loaded row is still selected. Direct `setRowSelection()` writes and updaters applied later are not normalized. `rowCount` must be the exact server total: with an estimated or too-high `rowCount` (total unknown), `getSelectedRowCount()` is wrong in `'exclude'` mode, so do not show it; show the number of deselected ids instead, or count from a total kept in app state. The count skips the ids of loaded sub-rows and group rows but subtracts an id that is not loaded as one row, so with tree data on more than one page take the count from the server. The table does not reset the selection when filters change: "all" means all rows matching the filters at click time, and the deselected ids and `rowCount` belong to those filters, so call `resetRowSelection()` whenever filters or `rowCount` change. +Set `rowCount` (top-level rows, the same option manual pagination reads) so `getSelectedRowCount()` is right and so a row, range, or page deselect goes back to an empty `'include'` selection when the deselected rows cover every row and no loaded row is still selected. With `getSubRows` or a grouped row model, every deselected id must also be loaded, because an unloaded id can be a sub-row or an old group row id. Direct `setRowSelection()` writes and updaters applied later are not normalized. If the change handlers apply updates later, do not call a row API in the same event after select-all or deselect-all: it still reads the old mode and writes the id for the wrong mode. `rowCount` must be the exact server total: with an estimated or too-high `rowCount` (total unknown), `getSelectedRowCount()` is wrong in `'exclude'` mode, so do not show it; show the number of deselected ids instead, or count from a total kept in app state. The count skips the ids of loaded sub-rows and group rows but subtracts an id that is not loaded as one row, so with tree data on more than one page take the count from the server. Group row ids stay in `rowSelection` when the grouping changes and are then subtracted as rows, so reset the selection when the grouping changes. The table does not reset the selection when filters change: "all" means all rows matching the filters at click time, and the deselected ids and `rowCount` belong to those filters, so call `resetRowSelection(true)` whenever filters or `rowCount` change (without `true` the reset restores `initialState`, which can select all rows). `enableRowSelection` only gates toggling. In `'exclude'` mode a row whose id is not in `rowSelection` is selected (`getIsSelected()`, selected row models, `getSelectedRowCount()`) even when it cannot be toggled, so the server must skip such rows if they must not be part of a bulk action. The row handler and the page APIs skip such a row, so `getIsSomePageRowsSelected()` can be false while `getIsSomeRowsSelected()` is true, and the table stays in `'exclude'` mode while the row is loaded. Select-all is a no-op when `enableRowSelection` is `false` and stays in `'include'` mode when `rowCount` is `0`. `enableSubRowSelection` only cascades to loaded sub-rows; the server must decide how a parent applies to sub-rows that are not loaded. @@ -116,9 +116,9 @@ Source: `docs/framework/react/guide/row-selection.md#useful-row-ids` Wrong: `const allSelectedRecords = table.getSelectedRowModel().rows` -Correct: `const allSelectedIds = table.getSelectedRowIds()` +Correct in `'include'` mode: `const allSelectedIds = table.getSelectedRowIds()` -Under manual pagination, unloaded selected IDs have no `Row` object in the current model. +Under manual pagination, unloaded selected IDs have no `Row` object in the current model. In `'exclude'` mode, `getSelectedRowIds()` returns only loaded rows; send the mode and the deselected ids instead (see the next entry). Source: `docs/framework/react/guide/row-selection.md#note-if-you-are-using-manualpagination` @@ -136,7 +136,7 @@ Correct: const mode = table.atoms.rowSelectionMode.get() const ids = Object.keys(table.atoms.rowSelection.get()) const bulkAction = - mode === 'exclude' ? { allRowsExcept: ids } : { onlyRows: ids } + mode === 'exclude' ? { allRowsExcept: ids, filters } : { onlyRows: ids } ``` With `manualRowSelection: true`, select-all switches `rowSelectionMode` to `'exclude'` and the keys of `rowSelection` become the deselected row ids. Read the mode with the map, or use `row.getIsSelected()` and `table.getSelectedRowCount()`. From 347730543557368bf16174a1a2a80e67fbbdbce5 Mon Sep 17 00:00:00 2001 From: Albert Nimtz Date: Sat, 28 Nov 2026 19:26:47 +0100 Subject: [PATCH 6/6] fix(table-core): derive group row selection from sub-rows with manualRowSelection With manualRowSelection, group rows no longer write their id into rowSelection. A group row is selected when all of its selectable sub-rows are selected, and toggling it toggles its sub-rows. This keeps group ids out of the include-mode count and out of the state after the grouping changes. Co-Authored-By: Claude Opus 5.5 --- docs/framework/alpine/guide/row-selection.md | 3 +- docs/framework/angular/guide/row-selection.md | 3 +- docs/framework/ember/guide/row-selection.md | 3 +- docs/framework/lit/guide/row-selection.md | 3 +- docs/framework/octane/guide/row-selection.md | 3 +- docs/framework/preact/guide/row-selection.md | 3 +- docs/framework/react/guide/row-selection.md | 3 +- docs/framework/solid/guide/row-selection.md | 3 +- docs/framework/svelte/guide/row-selection.md | 3 +- docs/framework/vue/guide/row-selection.md | 3 +- .../references/row-selection.md | 2 +- .../rowSelectionFeature.types.ts | 3 +- .../rowSelectionFeature.utils.ts | 66 +++++++-- .../row-selection/rowSelectionMode.test.ts | 125 ++++++++++++++++-- 14 files changed, 191 insertions(+), 35 deletions(-) diff --git a/docs/framework/alpine/guide/row-selection.md b/docs/framework/alpine/guide/row-selection.md index 63464cba48..1435cbfae0 100644 --- a/docs/framework/alpine/guide/row-selection.md +++ b/docs/framework/alpine/guide/row-selection.md @@ -289,7 +289,8 @@ Some limits apply, because the table cannot see rows that are not loaded: - "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- With `manualRowSelection`, a group row has no id in `rowSelection`. It is selected when all of its selectable sub-rows are selected, and toggling it toggles its sub-rows. When it cannot change its sub-rows, for example with `selectChildren: false`, `enableSubRowSelection: false`, or `enableMultiRowSelection: false`, toggling it does nothing. ### Render Row Selection UI diff --git a/docs/framework/angular/guide/row-selection.md b/docs/framework/angular/guide/row-selection.md index 36f51905ec..cfa912e845 100644 --- a/docs/framework/angular/guide/row-selection.md +++ b/docs/framework/angular/guide/row-selection.md @@ -288,7 +288,8 @@ Some limits apply, because the table cannot see rows that are not loaded: - "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- With `manualRowSelection`, a group row has no id in `rowSelection`. It is selected when all of its selectable sub-rows are selected, and toggling it toggles its sub-rows. When it cannot change its sub-rows, for example with `selectChildren: false`, `enableSubRowSelection: false`, or `enableMultiRowSelection: false`, toggling it does nothing. ### Render Row Selection UI diff --git a/docs/framework/ember/guide/row-selection.md b/docs/framework/ember/guide/row-selection.md index 225263927e..628b9f6d9f 100644 --- a/docs/framework/ember/guide/row-selection.md +++ b/docs/framework/ember/guide/row-selection.md @@ -280,7 +280,8 @@ Some limits apply, because the table cannot see rows that are not loaded: - "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- With `manualRowSelection`, a group row has no id in `rowSelection`. It is selected when all of its selectable sub-rows are selected, and toggling it toggles its sub-rows. When it cannot change its sub-rows, for example with `selectChildren: false`, `enableSubRowSelection: false`, or `enableMultiRowSelection: false`, toggling it does nothing. ### Render Row Selection UI diff --git a/docs/framework/lit/guide/row-selection.md b/docs/framework/lit/guide/row-selection.md index b0de7eb720..a7c355be45 100644 --- a/docs/framework/lit/guide/row-selection.md +++ b/docs/framework/lit/guide/row-selection.md @@ -295,7 +295,8 @@ Some limits apply, because the table cannot see rows that are not loaded: - "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- With `manualRowSelection`, a group row has no id in `rowSelection`. It is selected when all of its selectable sub-rows are selected, and toggling it toggles its sub-rows. When it cannot change its sub-rows, for example with `selectChildren: false`, `enableSubRowSelection: false`, or `enableMultiRowSelection: false`, toggling it does nothing. ### Render Row Selection UI diff --git a/docs/framework/octane/guide/row-selection.md b/docs/framework/octane/guide/row-selection.md index 4d4def3021..d228e9780e 100644 --- a/docs/framework/octane/guide/row-selection.md +++ b/docs/framework/octane/guide/row-selection.md @@ -278,7 +278,8 @@ Some limits apply, because the table cannot see rows that are not loaded: - "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- With `manualRowSelection`, a group row has no id in `rowSelection`. It is selected when all of its selectable sub-rows are selected, and toggling it toggles its sub-rows. When it cannot change its sub-rows, for example with `selectChildren: false`, `enableSubRowSelection: false`, or `enableMultiRowSelection: false`, toggling it does nothing. ### Render Row Selection UI diff --git a/docs/framework/preact/guide/row-selection.md b/docs/framework/preact/guide/row-selection.md index 71037b8d78..bc7eb57847 100644 --- a/docs/framework/preact/guide/row-selection.md +++ b/docs/framework/preact/guide/row-selection.md @@ -278,7 +278,8 @@ Some limits apply, because the table cannot see rows that are not loaded: - "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- With `manualRowSelection`, a group row has no id in `rowSelection`. It is selected when all of its selectable sub-rows are selected, and toggling it toggles its sub-rows. When it cannot change its sub-rows, for example with `selectChildren: false`, `enableSubRowSelection: false`, or `enableMultiRowSelection: false`, toggling it does nothing. ### Render Row Selection UI diff --git a/docs/framework/react/guide/row-selection.md b/docs/framework/react/guide/row-selection.md index 83e0d12c6a..bbe4ed8f0f 100644 --- a/docs/framework/react/guide/row-selection.md +++ b/docs/framework/react/guide/row-selection.md @@ -279,7 +279,8 @@ Some limits apply, because the table cannot see rows that are not loaded: - "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- With `manualRowSelection`, a group row has no id in `rowSelection`. It is selected when all of its selectable sub-rows are selected, and toggling it toggles its sub-rows. When it cannot change its sub-rows, for example with `selectChildren: false`, `enableSubRowSelection: false`, or `enableMultiRowSelection: false`, toggling it does nothing. ### Render Row Selection UI diff --git a/docs/framework/solid/guide/row-selection.md b/docs/framework/solid/guide/row-selection.md index a63fc8483c..d2e7c814c6 100644 --- a/docs/framework/solid/guide/row-selection.md +++ b/docs/framework/solid/guide/row-selection.md @@ -284,7 +284,8 @@ Some limits apply, because the table cannot see rows that are not loaded: - "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- With `manualRowSelection`, a group row has no id in `rowSelection`. It is selected when all of its selectable sub-rows are selected, and toggling it toggles its sub-rows. When it cannot change its sub-rows, for example with `selectChildren: false`, `enableSubRowSelection: false`, or `enableMultiRowSelection: false`, toggling it does nothing. ### Render Row Selection UI diff --git a/docs/framework/svelte/guide/row-selection.md b/docs/framework/svelte/guide/row-selection.md index ad2ca0c7bc..0d800c700d 100644 --- a/docs/framework/svelte/guide/row-selection.md +++ b/docs/framework/svelte/guide/row-selection.md @@ -293,7 +293,8 @@ Some limits apply, because the table cannot see rows that are not loaded: - "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- With `manualRowSelection`, a group row has no id in `rowSelection`. It is selected when all of its selectable sub-rows are selected, and toggling it toggles its sub-rows. When it cannot change its sub-rows, for example with `selectChildren: false`, `enableSubRowSelection: false`, or `enableMultiRowSelection: false`, toggling it does nothing. ### Render Row Selection UI diff --git a/docs/framework/vue/guide/row-selection.md b/docs/framework/vue/guide/row-selection.md index f4bbba09e6..fa7d12286c 100644 --- a/docs/framework/vue/guide/row-selection.md +++ b/docs/framework/vue/guide/row-selection.md @@ -283,7 +283,8 @@ Some limits apply, because the table cannot see rows that are not loaded: - "All" means all rows that match the filters at the time of the "select all" click. The deselected ids and `rowCount` belong to those filters. The table does not reset the selection when the filters change, so call `table.resetRowSelection(true)` when your filters or `rowCount` change. Without `true`, the reset goes back to `initialState`, which can select all rows. If you do not, `table.getSelectedRowCount()` is wrong for the new filters. - "Select all" keeps the `'include'` mode when `rowCount` is `0`, and it does nothing when `enableRowSelection` is `false`, because no row can be selected. - `enableRowSelection` still decides which loaded rows the user can toggle, but it does not decide which rows are selected. A row that is not in `rowSelection` is selected, also when the user cannot toggle it: `row.getIsSelected()` returns `true`, the row is in the selected row models, and `table.getSelectedRowCount()` counts it. The row handler and the page APIs skip that row, so `table.getIsSomePageRowsSelected()` can return `false` while `table.getIsSomeRowsSelected()` returns `true`, and the table stays in `'exclude'` mode while that row is loaded. The table also cannot run the rule for rows that are not loaded. If such rows must not be part of a bulk action, your server must skip them. -- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows and group rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. The ids of group rows stay in `rowSelection` when the grouping changes, and the table then subtracts each of them as one row. Reset the selection when the grouping changes. +- `enableSubRowSelection` applies only to loaded sub-rows. When you toggle a parent row, the ids of its loaded sub-rows are added or removed with it. The table does not know sub-rows that are not loaded, so your server must decide how a parent applies to them. `rowCount` counts top-level rows, so `table.getSelectedRowCount()` does not subtract deselected sub-rows while they are loaded. The table cannot know that an id is a sub-row after its page is unloaded, and then subtracts it as one row. With tree data on more than one page, get the count from your server. +- With `manualRowSelection`, a group row has no id in `rowSelection`. It is selected when all of its selectable sub-rows are selected, and toggling it toggles its sub-rows. When it cannot change its sub-rows, for example with `selectChildren: false`, `enableSubRowSelection: false`, or `enableMultiRowSelection: false`, toggling it does nothing. ### Render Row Selection UI diff --git a/packages/table-core/skills/table-features/references/row-selection.md b/packages/table-core/skills/table-features/references/row-selection.md index b9515612f6..f19be39767 100644 --- a/packages/table-core/skills/table-features/references/row-selection.md +++ b/packages/table-core/skills/table-features/references/row-selection.md @@ -86,7 +86,7 @@ const bulkAction = { `manualRowSelection: true` allows the `'exclude'` value of the `rowSelectionMode` state; without the option the mode is always read as `'include'` and select-all writes the loaded row ids. With it, `toggleAllRowsSelected(true)` sets `'exclude'` and clears `rowSelection`, `toggleAllRowsSelected(false)` sets `'include'` and clears it, and `resetRowSelection()` resets both slices. In `'exclude'` mode a row is selected when its id is not in `rowSelection`, so rows that load later are selected, `getIsAllRowsSelected()` is true only with no ids, and the selected row models and `getSelectedRowIds()` hold only loaded rows. `getIsAllRowsSelected()` is never true in `'include'` mode under this option, because loaded rows are not all rows. -Set `rowCount` (top-level rows, the same option manual pagination reads) so `getSelectedRowCount()` is right and so a row, range, or page deselect goes back to an empty `'include'` selection when the deselected rows cover every row and no loaded row is still selected. With `getSubRows` or a grouped row model, every deselected id must also be loaded, because an unloaded id can be a sub-row or an old group row id. Direct `setRowSelection()` writes and updaters applied later are not normalized. If the change handlers apply updates later, do not call a row API in the same event after select-all or deselect-all: it still reads the old mode and writes the id for the wrong mode. `rowCount` must be the exact server total: with an estimated or too-high `rowCount` (total unknown), `getSelectedRowCount()` is wrong in `'exclude'` mode, so do not show it; show the number of deselected ids instead, or count from a total kept in app state. The count skips the ids of loaded sub-rows and group rows but subtracts an id that is not loaded as one row, so with tree data on more than one page take the count from the server. Group row ids stay in `rowSelection` when the grouping changes and are then subtracted as rows, so reset the selection when the grouping changes. The table does not reset the selection when filters change: "all" means all rows matching the filters at click time, and the deselected ids and `rowCount` belong to those filters, so call `resetRowSelection(true)` whenever filters or `rowCount` change (without `true` the reset restores `initialState`, which can select all rows). +Set `rowCount` (top-level rows, the same option manual pagination reads) so `getSelectedRowCount()` is right and so a row, range, or page deselect goes back to an empty `'include'` selection when the deselected rows cover every row and no loaded row is still selected. With `getSubRows` or a grouped row model, every deselected id must also be loaded, because an unloaded id can be a sub-row or an old group row id. Direct `setRowSelection()` writes and updaters applied later are not normalized. If the change handlers apply updates later, do not call a row API in the same event after select-all or deselect-all: it still reads the old mode and writes the id for the wrong mode. `rowCount` must be the exact server total: with an estimated or too-high `rowCount` (total unknown), `getSelectedRowCount()` is wrong in `'exclude'` mode, so do not show it; show the number of deselected ids instead, or count from a total kept in app state. The count skips the ids of loaded sub-rows and group rows but subtracts an id that is not loaded as one row, so with tree data on more than one page take the count from the server. Group rows have no id in `rowSelection` under this option: a group row is selected when all of its selectable sub-rows are selected, toggling it toggles its sub-rows, and toggling it does nothing when it cannot change them (`selectChildren: false`, `enableSubRowSelection: false`, or single-row selection). The table does not reset the selection when filters change: "all" means all rows matching the filters at click time, and the deselected ids and `rowCount` belong to those filters, so call `resetRowSelection(true)` whenever filters or `rowCount` change (without `true` the reset restores `initialState`, which can select all rows). `enableRowSelection` only gates toggling. In `'exclude'` mode a row whose id is not in `rowSelection` is selected (`getIsSelected()`, selected row models, `getSelectedRowCount()`) even when it cannot be toggled, so the server must skip such rows if they must not be part of a bulk action. The row handler and the page APIs skip such a row, so `getIsSomePageRowsSelected()` can be false while `getIsSomeRowsSelected()` is true, and the table stays in `'exclude'` mode while the row is loaded. Select-all is a no-op when `enableRowSelection` is `false` and stays in `'include'` mode when `rowCount` is `0`. `enableSubRowSelection` only cascades to loaded sub-rows; the server must decide how a parent applies to sub-rows that are not loaded. diff --git a/packages/table-core/src/features/row-selection/rowSelectionFeature.types.ts b/packages/table-core/src/features/row-selection/rowSelectionFeature.types.ts index ac3d6c3737..55706d470c 100644 --- a/packages/table-core/src/features/row-selection/rowSelectionFeature.types.ts +++ b/packages/table-core/src/features/row-selection/rowSelectionFeature.types.ts @@ -88,7 +88,8 @@ export interface TableOptions_RowSelection< * and clears `rowSelection`. In `'exclude'` mode the ids in `rowSelection` * are the deselected rows, so rows that load later are selected. A row * whose id is not in `rowSelection` is selected, also when - * `enableRowSelection` does not let the user toggle it. + * `enableRowSelection` does not let the user toggle it. A group row has no + * id in `rowSelection`. Its state comes from its sub-rows. * * When `false`, `rowSelectionMode` is ignored and always read as * `'include'`. diff --git a/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts b/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts index 72c45a6691..8c11573a16 100644 --- a/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts +++ b/packages/table-core/src/features/row-selection/rowSelectionFeature.utils.ts @@ -10,6 +10,7 @@ import type { TableFeatures } from '../../types/TableFeatures' import type { RowModel } from '../../core/row-models/coreRowModelsFeature.types' import type { Table_Internal } from '../../types/Table' import type { Row } from '../../types/Row' +import type { Row_ColumnGrouping } from '../column-grouping/columnGroupingFeature.types' import type { RowSelectionMode, RowSelectionState, @@ -604,6 +605,9 @@ export function table_getIsAllPageRowsSelected< let sawSelectableRow = false for (let i = 0; i < paginationFlatRows.length; i++) { const row = paginationFlatRows[i]! + if (isGroupRowWithoutId(row)) { + continue + } if (isRowSelected(row, rowSelection) === exclude) { if (isRowSelectableInSelectAll(row, subtreeCache)) { return false @@ -724,6 +728,9 @@ export function table_getToggleAllPageRowsSelectedHandler< * cover every row, and no loaded row is selected, the state goes back to an * empty `'include'` selection. * + * With `options.manualRowSelection`, a group row has no id in the state, so + * only its sub-rows change. When its sub-rows cannot change, nothing happens. + * * @example * ```ts * row_toggleSelected(row) @@ -741,6 +748,15 @@ export function row_toggleSelected< const exclude = isRowSelectionExcluded(table) const isSelected = row_getIsSelected(row) const nextValue = typeof value !== 'undefined' ? value : !isSelected + const selectChildren = + (opts?.selectChildren ?? true) && row_getCanMultiSelect(row) + + if ( + isGroupRowWithoutId(row) && + !(selectChildren && row.subRows.length && row_getCanSelectSubRows(row)) + ) { + return + } if (exclude && nextValue && !row_getCanMultiSelect(row)) { // A single-select row must be the only selected row. A list of deselected @@ -761,7 +777,7 @@ export function row_toggleSelected< rowSelection, row.id, nextValue, - (opts?.selectChildren ?? true) && row_getCanMultiSelect(row), + selectChildren, table, false, exclude, @@ -779,6 +795,9 @@ export function row_toggleSelected< * Missing row ids are treated as not selected. In `'exclude'` mode this is * inverted: a row is selected when its id is missing. * + * With `options.manualRowSelection`, a group row has no id in the state. It + * is selected when all of its selectable sub-rows are selected. + * * @example * ```ts * const selected = row_getIsSelected(row) @@ -788,6 +807,9 @@ export function row_getIsSelected< TFeatures extends TableFeatures, TData extends RowData, >(row: Row) { + if (isGroupRowWithoutId(row)) { + return isSubRowSelected(row) === 'all' + } const rowSelection = row.table.atoms.rowSelection?.get() ?? {} return isRowSelected(row, rowSelection) !== isRowSelectionExcluded(row.table) } @@ -1090,6 +1112,17 @@ function countExcludedRows< return count } +function isGroupRowWithoutId< + TFeatures extends TableFeatures, + TData extends RowData, +>(row: Row): boolean { + return ( + !!row.table.options.manualRowSelection && + !!(row as Row & Partial) + .groupingColumnId + ) +} + function isFlatTable( table: Table_Internal, ): boolean { @@ -1178,15 +1211,17 @@ function mutateRowIsSelected< ): void { const row = table.getRow(rowId, true) - if (value) { - if (!exclude && !row_getCanMultiSelect(row)) { - Object.keys(rowSelection).forEach((key) => delete rowSelection[key]) - } - if (row_getCanSelect(row)) { - writeRowIsSelected(rowSelection, rowId, true, exclude) + if (!isGroupRowWithoutId(row)) { + if (value) { + if (!exclude && !row_getCanMultiSelect(row)) { + Object.keys(rowSelection).forEach((key) => delete rowSelection[key]) + } + if (row_getCanSelect(row)) { + writeRowIsSelected(rowSelection, rowId, true, exclude) + } + } else if (!respectCanSelectOnDeselect || row_getCanSelect(row)) { + writeRowIsSelected(rowSelection, rowId, false, exclude) } - } else if (!respectCanSelectOnDeselect || row_getCanSelect(row)) { - writeRowIsSelected(rowSelection, rowId, false, exclude) } if (includeChildren && row.subRows.length && row_getCanSelectSubRows(row)) { @@ -1287,8 +1322,11 @@ function pruneAncestorRowIds< const rowsById = row.table.getCoreRowModel().rowsById let parentId = row.parentId while (parentId !== undefined) { - writeRowIsSelected(rowSelection, parentId, false, exclude) - parentId = (rowsById[parentId] ?? row.table.getRow(parentId, true)).parentId + const parent = rowsById[parentId] ?? row.table.getRow(parentId, true) + if (!isGroupRowWithoutId(parent)) { + writeRowIsSelected(rowSelection, parentId, false, exclude) + } + parentId = parent.parentId } } @@ -1306,7 +1344,9 @@ function selectRowsRecursively< for (let i = 0; i < rows.length; i++) { const row = rows[i]! - const isSelected = isRowSelected(row, rowSelection) !== exclude + const isSelected = isGroupRowWithoutId(row) + ? isSubRowSelected(row) === 'all' + : isRowSelected(row, rowSelection) !== exclude if (isSelected) { selectedFlatRows.push(row) @@ -1424,7 +1464,7 @@ export function isSubRowSelected< break } - if (row_getCanSelect(subRow)) { + if (row_getCanSelect(subRow) && !isGroupRowWithoutId(subRow)) { someSelectable = true if (isRowSelected(subRow, rowSelection) !== exclude) { someSelected = true diff --git a/packages/table-core/tests/implementation/features/row-selection/rowSelectionMode.test.ts b/packages/table-core/tests/implementation/features/row-selection/rowSelectionMode.test.ts index 2071a15b32..96c4b1a583 100644 --- a/packages/table-core/tests/implementation/features/row-selection/rowSelectionMode.test.ts +++ b/packages/table-core/tests/implementation/features/row-selection/rowSelectionMode.test.ts @@ -570,7 +570,7 @@ describe('getSelectedRowCount', () => { expect(table.getSelectedRowCount()).toBe(19) }) - it('does not count the id of a group row', () => { + it('keeps the count of a deselected group when the grouping is cleared', () => { const table = makePipelineTable({ manualRowSelection: true, rowCount: 6, @@ -579,8 +579,10 @@ describe('getSelectedRowCount', () => { table.toggleAllRowsSelected(true) table.getRow('kind:even', true).toggleSelected(false) + expect(ids(table)).toEqual(['0', '2', '4']) + expect(table.getSelectedRowCount()).toBe(3) - expect(ids(table)).toEqual(['0', '2', '4', 'kind:even']) + table.setGrouping([]) expect(table.getSelectedRowCount()).toBe(3) }) @@ -972,22 +974,21 @@ describe('exclude mode when the deselected rows cover every row', () => { expect(table.getRow('b').getIsSelected()).toBe(true) }) - it('keeps exclude mode when an id that is not loaded can be an old group row', () => { + it('keeps exclude mode when the state holds the id of an old group row', () => { const table = makePipelineTable( { manualPagination: true, manualRowSelection: true, rowCount: 4, getSubRows: undefined, - initialState: { grouping: ['kind'] }, + initialState: { + rowSelection: { '0': true, 'kind:even': true }, + rowSelectionMode: 'exclude', + }, }, - makeRows(0, 2), + makeRows(2, 2), ) - table.toggleAllRowsSelected(true) - table.getRow('kind:even', true).toggleSelected(false) - table.setGrouping([]) - table.setOptions((prev) => ({ ...prev, data: makeRows(2, 2) })) table.getRow('2').toggleSelected(false) table.getRow('3').toggleSelected(false) @@ -997,7 +998,6 @@ describe('exclude mode when the deselected rows cover every row', () => { table.setOptions((prev) => ({ ...prev, data: makeRows(0, 2) })) expect(table.getRow('1').getIsSelected()).toBe(true) }) - it('keeps exclude mode while a row of another group is selected', () => { const table = makePipelineTable({ manualRowSelection: true, @@ -1071,6 +1071,111 @@ describe('exclude mode when the deselected rows cover every row', () => { }) }) +describe('group rows with manualRowSelection', () => { + function makeGroupedTable( + options: Parameters[0] = {}, + ) { + return makePipelineTable({ + manualRowSelection: true, + rowCount: 6, + initialState: { grouping: ['kind'] }, + ...options, + }) + } + + it('selects the sub-rows of a group without writing the group id', () => { + const table = makeGroupedTable() + const group = table.getRow('kind:even', true) + + group.toggleSelected(true) + + expect(ids(table)).toEqual(['0', '2', '4']) + expect(table.getSelectedRowIds()).toEqual(['0', '2', '4']) + expect(table.getSelectedRowCount()).toBe(3) + expect(group.getIsSelected()).toBe(true) + expect( + table.getGroupedSelectedRowModel().rows.map((row) => row.id), + ).toEqual(['kind:even']) + + table.getRow('2').toggleSelected(false) + + expect(group.getIsSelected()).toBe(false) + expect(group.getIsSomeSelected()).toBe(true) + expect(table.getSelectedRowCount()).toBe(2) + }) + + it('reads a group of an exclude selection from its sub-rows', () => { + const table = makeGroupedTable() + table.toggleAllRowsSelected(true) + const group = table.getRow('kind:odd', true) + + expect(group.getIsSelected()).toBe(true) + + group.toggleSelected() + + expect(ids(table)).toEqual(['1', '3', '5']) + expect(group.getIsSelected()).toBe(false) + expect(table.getRow('kind:even', true).getIsSelected()).toBe(true) + }) + + it('reads the page state from the rows of the groups', () => { + const table = makeGroupedTable() + + table.getRow('kind:even', true).toggleSelected(true) + expect(table.getIsAllPageRowsSelected()).toBe(false) + + table.getRow('kind:odd', true).toggleSelected(true) + expect(table.getIsAllPageRowsSelected()).toBe(true) + }) + + it('reads nested groups from their sub-rows', () => { + const table = makeGroupedTable({ + initialState: { grouping: ['kind', 'value'] }, + }) + const group = table.getRow('kind:even', true) + const nestedGroup = table.getRow('kind:even>value:0', true) + + group.toggleSelected(true) + expect(ids(table)).toEqual(['0', '2', '4']) + expect(group.getIsSelected()).toBe(true) + expect(nestedGroup.getIsSelected()).toBe(true) + + table.getRow('0').toggleSelected(false) + expect(nestedGroup.getIsSelected()).toBe(false) + expect(group.getIsSelected()).toBe(false) + expect(group.getIsSomeSelected()).toBe(true) + }) + + it('does not change the selection when a group cannot reach its sub-rows', () => { + const table = makeGroupedTable({ + initialState: { grouping: ['kind'], rowSelection: { '1': true } }, + }) + const group = table.getRow('kind:even', true) + + group.toggleSelected(true, { selectChildren: false }) + expect(ids(table)).toEqual(['1']) + + table.setOptions((prev) => ({ ...prev, enableMultiRowSelection: false })) + group.toggleSelected(true) + expect(ids(table)).toEqual(['1']) + + table.setRowSelectionMode('exclude') + group.toggleSelected(true) + expect(mode(table)).toBe('exclude') + expect(ids(table)).toEqual(['1']) + }) + + it('does not write the group id when deselectParents removes the ancestors', () => { + const table = makeGroupedTable() + table.toggleAllRowsSelected(true) + + table.getRow('2').toggleSelected(false, { deselectParents: true }) + + expect(ids(table)).toEqual(['2']) + expect(table.getRow('kind:even', true).getIsSelected()).toBe(false) + }) +}) + describe('row selection mode state APIs', () => { it('resets the mode together with the selection', () => { const table = makeSelectionTable({