Multiple Selection
import { Grid, Checkbox } from 'cx/widgets'; Copied Grid supports multiple row selection using checkboxes. A “select all” checkbox in the header can toggle selection for all visible rows, with support for the indeterminate state when only some rows are selected.
<div controller={PageController}>
<TextField
value={m.searchText}
icon="search"
placeholder="Search..."
showClear
style="width: 300px; margin-bottom: 16px"
/>
<Grid
records={m.records}
style="width: 100%"
filterParams={m.searchText}
onCreateFilter={(searchText: string) => {
if (!searchText) return () => true
const predicate = getSearchQueryPredicate(searchText)
return (record: Employee) =>
predicate(record.fullName) ||
predicate(record.phone) ||
predicate(record.city)
}}
onTrackMappedRecords={(records, instance) =>
instance.getControllerByType(PageController).updateSelection(records)
}
selection={{ type: PropertySelection, multiple: true }}
columns={[
{
header: {
style: "padding: 2px",
children: (
<Checkbox
value={m.selectAll}
indeterminate
unfocusable
class="ml-4"
/>
),
},
field: "selected",
style: "width: 1px",
pad: false,
align: "center",
children: (
<Checkbox value={m.$record.selected} unfocusable class="ml-4" />
),
},
{ header: "Name", field: "fullName", sortable: true },
{ header: "Phone", field: "phone" },
{ header: "City", field: "city", sortable: true },
]}
/>
</div> | Name | Phone | City | |
|---|---|---|---|
| Alice Johnson 1 | 555-1000 | New York | |
| Bob Smith 2 | 555-1001 | Los Angeles | |
| Carol White 3 | 555-1002 | Chicago | |
| David Brown 4 | 555-1003 | Houston | |
| Eva Green 5 | 555-1004 | Phoenix | |
| Alice Johnson 6 | 555-1005 | New York | |
| Bob Smith 7 | 555-1006 | Los Angeles | |
| Carol White 8 | 555-1007 | Chicago | |
| David Brown 9 | 555-1008 | Houston | |
| Eva Green 10 | 555-1009 | Phoenix |
Click checkboxes to select individual rows. Use the header checkbox to select or deselect all visible rows. Try filtering to see how “select all” works with filtered data.
How It Works
The example combines several features:
- Checkbox column - A column with checkboxes bound to each record’s
selectedproperty - Select all checkbox - A header checkbox that toggles all visible rows
- Indeterminate state - The header checkbox shows indeterminate when some (but not all) rows are selected
- Filtered selection - “Select all” only affects currently visible (filtered) rows
The onTrackMappedRecords callback is called whenever the visible records change (due to filtering or sorting). It receives the current visible records, allowing the controller to track which records are visible and update the “select all” checkbox state accordingly.
onTrackMappedRecords={(records, instance) =>
instance.getControllerByType(PageController).updateSelection(records)
}
Configuration
| Property | Type | Description |
|---|---|---|
selection | object | Selection configuration. Use PropertySelection with multiple: true for checkbox-based selection. |
onTrackMappedRecords | string | function | Callback invoked when visible records change. Receives an array of record instances. |
See also: Searching and Filtering, Selections