mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 13:29:41 +02:00
fix(app): datatable global checkbox doesn't reflect the selected state (#470)
This commit is contained in:
parent
438b1f9815
commit
b57855f20d
10 changed files with 110 additions and 18 deletions
|
@ -1,7 +1,19 @@
|
|||
import { ColumnDef, Row } from '@tanstack/react-table';
|
||||
import { ColumnDef, Row, Table } from '@tanstack/react-table';
|
||||
|
||||
import { Checkbox } from '@@/form-components/Checkbox';
|
||||
|
||||
function allRowsSelected<T>(table: Table<T>) {
|
||||
return table.getCoreRowModel().rows.every((row) => row.getIsSelected());
|
||||
}
|
||||
|
||||
function someRowsSelected<T>(table: Table<T>) {
|
||||
return table.getCoreRowModel().rows.some((row) => row.getIsSelected());
|
||||
}
|
||||
|
||||
function somePageRowsSelected<T>(table: Table<T>) {
|
||||
return table.getRowModel().rows.some((row) => row.getIsSelected());
|
||||
}
|
||||
|
||||
export function createSelectColumn<T>(dataCy: string): ColumnDef<T> {
|
||||
let lastSelectedId = '';
|
||||
|
||||
|
@ -11,15 +23,15 @@ export function createSelectColumn<T>(dataCy: string): ColumnDef<T> {
|
|||
<Checkbox
|
||||
id="select-all"
|
||||
data-cy={`select-all-checkbox-${dataCy}`}
|
||||
checked={table.getIsAllPageRowsSelected()}
|
||||
indeterminate={table.getIsSomeRowsSelected()}
|
||||
checked={allRowsSelected(table)}
|
||||
indeterminate={!allRowsSelected(table) && someRowsSelected(table)}
|
||||
onChange={(e) => {
|
||||
// Select all rows if shift key is held down, otherwise only page rows
|
||||
if (e.nativeEvent instanceof MouseEvent && e.nativeEvent.shiftKey) {
|
||||
table.getToggleAllRowsSelectedHandler()(e);
|
||||
table.toggleAllRowsSelected();
|
||||
return;
|
||||
}
|
||||
table.getToggleAllPageRowsSelectedHandler()(e);
|
||||
table.toggleAllPageRowsSelected(!somePageRowsSelected(table));
|
||||
}}
|
||||
disabled={table.getRowModel().rows.every((row) => !row.getCanSelect())}
|
||||
onClick={(e) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue