CxJS

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>
NamePhoneCity
Alice Johnson 1555-1000New York
Bob Smith 2555-1001Los Angeles
Carol White 3555-1002Chicago
David Brown 4555-1003Houston
Eva Green 5555-1004Phoenix
Alice Johnson 6555-1005New York
Bob Smith 7555-1006Los Angeles
Carol White 8555-1007Chicago
David Brown 9555-1008Houston
Eva Green 10555-1009Phoenix

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:

  1. Checkbox column - A column with checkboxes bound to each record’s selected property
  2. Select all checkbox - A header checkbox that toggles all visible rows
  3. Indeterminate state - The header checkbox shows indeterminate when some (but not all) rows are selected
  4. 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

PropertyTypeDescription
selectionobjectSelection configuration. Use PropertySelection with multiple: true for checkbox-based selection.
onTrackMappedRecordsstring | functionCallback invoked when visible records change. Receives an array of record instances.

See also: Searching and Filtering, Selections