You're viewing the legacy site. Visit cxjs.io/docs for the latest documentation. Legacy site. Visit cxjs.io/docs for new docs.

Documentation

Report Edit

Selections

import { PropertySelection, KeySelection, SimpleSelection } from 'cx/ui';

Some widgets allow the user to select one or more objects presented to them. If only one object can be selected at a time, that's called single selection mode. If multiple objects can be selected, it's referred to as multiple selection mode.

The question here is what happens after the user selects something? There are multiple ways a selection can be handled and Cx offers commonly used methods out of the box.

Property Selection

In this mode, selection is handled by setting a designated selection property to be either true or false. Usually, selected property is used.

This mode is easy to understand with a list of checkboxes. Each checkbox determines whether a corresponding record is selected or not.

0204060801002030405060708090
ControllerChart
<div class="widgets" controller={PageController}>
    <Svg style={{ width: "400px", height: "400px" }}>
        <Chart anchors="0 1 1 0" offset="25 -25 -40 50" axes={NumericAxis.XY()}>
            <Rectangle
                anchors="0 1 1 0"
                style={{ fill: "rgba(100, 100, 100, 0.1)" }}
            />
            <Gridlines />
            <ScatterGraph
                data-bind="$page.bubbles"
                selection={{ type: PropertySelection, multiple: true }}
                sizeField="d"
                colorIndex={0}
            />
        </Chart>
    </Svg>
    <div>
        <Repeater records-bind="$page.bubbles">
            <div>
                <Checkbox
                    checked-bind="$record.selected"
                    text-bind="$record.name"
                />
            </div>
        </Repeater>
    </div>
</div>
Copied!Cx Fiddle

The Ctrl key can be used to toggle bubble selection.

Alternatively, the toggle property can be set to true and selection will behave same as if the Ctrl key is pressed all the time.

This mode is usually used for multiple selection, but it can be used for single selection too.

Property selection mode is very fast for checking if a particular object is selected, however, it needs to go through the whole list of objects to determine what is selected.

Key Selection

Key selection is a more common selection mode, where selected value(s) is/are stored in a separate variable.

NameXYD
Bubble 115.1246.306.10
Bubble 268.4846.0515.83
Bubble 342.2090.2938.15
Bubble 474.8680.8036.41
Bubble 52.6538.987.70
Bubble 66.4540.9712.66
Bubble 727.6723.9529.34
Bubble 86.1739.929.37
Bubble 981.4431.1832.85
Bubble 1030.1349.558.91
Bubble 1161.5149.6838.07
Bubble 1261.4682.5921.69
Bubble 1386.5752.1739.83
Bubble 1435.1636.4926.44
Bubble 1565.6840.1825.30
Index
<Grid records-bind="$page.bubbles"
        style={{width: "400px"}}
        columns={[
            { header: 'Name', field: 'name', sortable: true },
            { header: 'X', field: 'x', sortable: true, format: 'n;2', align: "right" },
            { header: 'Y', field: 'y', sortable: true, format: 'n;2', align: "right" },
            { header: 'R', field: 'r', sortable: true, format: 'n;2', align: "right" }
        ]}
        selection={{type: KeySelection, keyField: 'name', bind: '$page.selection'}}
/>
<div>
    <Select value-bind="$page.selection">
        <Repeater records-bind="$page.bubbles">
            <Option value-bind="$record.name" text-bind="$record.name" />
        </Repeater>
    </Select>
</div>
Copied!Cx Fiddle

Key selection works similar to select control where only key (value) of the selected option represents the selection.

Use keyField or keyFields to configure which fields form the record key.

Use bind property to define where selected keys will be stored.

Use multiple property to decide if multiple selection is allowed or not.

Simple Selection

Simple selection is the simplest selection mode. It allows single selection only.

NameXYD
Bubble 115.1246.306.10
Bubble 268.4846.0515.83
Bubble 342.2090.2938.15
Bubble 474.8680.8036.41
Bubble 52.6538.987.70
Bubble 66.4540.9712.66
Bubble 727.6723.9529.34
Bubble 86.1739.929.37
Bubble 981.4431.1832.85
Bubble 1030.1349.558.91
Bubble 1161.5149.6838.07
Bubble 1261.4682.5921.69
Bubble 1386.5752.1739.83
Bubble 1435.1636.4926.44
Bubble 1565.6840.1825.30
Select a bubble
Index
<Grid records-bind="$page.bubbles"
    style={{ width: "400px" }}
    columns={[
        { header: 'Name', field: 'name' },
        { header: 'X', field: 'x', format: 'n;2', align: "right" },
        { header: 'Y', field: 'y', format: 'n;2', align: "right" },
        { header: 'D', field: 'd', format: 'n;2', align: "right" }
    ]}
    selection={{ type: SimpleSelection, bind: '$page.simpleSelection' }}
/>
<div>
    <Text tpl="Select a bubble" if-expr="!{$page.simpleSelection}" />
    <Text
        tpl="Selected bubble: {$page.simpleSelection.name}"
        if-expr="{$page.simpleSelection}"
    />
</div>
Copied!Cx Fiddle

Use bind property to define where selected objects will be stored.