User Interaction
The TreeList UI component supports single and multiple row selection. Use the selection.mode property to change the current mode.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import TreeList, {
- Selection
- } from 'devextreme-react/tree-list';
- class App extends React.Component {
- render() {
- return (
- <TreeList ... >
- <Selection mode="single" /> {/* "multiple" | "none" */}
- </TreeList>
- );
- }
- }
- export default App;
In the single mode, only one row can be selected at a time. In multiple mode, rows are supplied with check boxes for multiple selection. A check box in the first column's header allows a user to select all rows at once. Clicking this check box selects only those rows that meet the filtering conditions if a filter is applied.
You can disable the latter feature by setting the selection.allowSelectAll property to false.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import TreeList, {
- Selection
- } from 'devextreme-react/tree-list';
- class App extends React.Component {
- render() {
- return (
- <TreeList ... >
- <Selection
- mode="single"
- allowSelectAll={false}
- />
- </TreeList>
- );
- }
- }
- export default App;
Selection is non-recursive by default, that is, only the clicked row is selected. Assign true to the selection.recursive property to make selection recursive. After that, a click on a row also selects nested rows, and a click on the column header's check box selects all rows disregarding applied filters.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import TreeList, {
- Selection
- } from 'devextreme-react/tree-list';
- class App extends React.Component {
- render() {
- return (
- <TreeList ... >
- <Selection
- mode="multiple"
- recursive={true}
- />
- </TreeList>
- );
- }
- }
- export default App;
Initial and Runtime Selection
Use the selectedRowKeys property to select rows initially. With non-recursive selection, one key selects one row; with recursive - a row with its nested rows. Note that you should specify row keys beforehand. You can do it in the key field of the store that underlies the dataSource. Alternatively, you can set the UI component's keyExpr property. With hierarchical data, keys can be generated automatically if the key and keyExpr are not set.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import TreeList from 'devextreme-react/tree-list';
- import DataSource from 'devextreme/data/data_source';
- import 'devextreme/data/array_store';
- // or
- // import 'devextreme/data/odata/store';
- // import 'devextreme/data/custom_store';
- const treeListDataSource = new DataSource({
- store: {
- // ...
- key: 'id'
- }
- });
- class App extends React.Component {
- selectedRowKeys = [1, 5, 18];
- render() {
- return (
- <TreeList ...
- dataSource={treeListDataSource}
- defaultSelectedRowKeys={this.selectedRowKeys}>
- </TreeList>
- );
- }
- }
- export default App;
You can select rows at runtime using the selectRows(keys, preserve) method. Note that the preserve argument, which tells the UI component whether to keep or clear the previous selection, is false by default. Before selecting a row, you can call the isRowSelected(key) method to check if this row is already selected. If you need to select all rows at once, call the selectAll() method.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import TreeList from 'devextreme-react/tree-list';
- class App extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- selectedRowKeys: []
- }
- this.selectFirstRow = this.selectFirstRow.bind(this);
- this.handleOptionChange = this.handleOptionChange.bind(this);
- }
- selectFirstRow(e) {
- const rowKey = e.component.getKeyByRowIndex(0);
- this.setState(prevState => ({
- selectedRowKeys: [...prevState.selectedRowKeys, rowKey]
- }));
- }
- handleOptionChange(e) {
- if(e.fullName === 'selectedRowKeys') {
- this.setState({
- selectedRowKeys: e.value
- });
- }
- }
- render() {
- return (
- <TreeList ...
- selectedRowKeys={this.state.selectedRowKeys}
- onContentReady={this.selectFirstRow}
- onOptionChanged={this.handleOptionChange}>
- </TreeList>
- );
- }
- }
- export default App;
Call the getSelectedRowKeys(mode) or getSelectedRowsData() method to get the selected rows' keys or data.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import TreeList from 'devextreme-react/tree-list';
- class App extends React.Component {
- constructor(props) {
- super(props);
- this.treeListRef = React.createRef();
- this.getSelectedRowKeys = () => {
- return this.treeList.getSelectedRowKeys("all"); // or "excludeRecursive" | "leavesOnly"
- }
- this.getSelectedRowsData = () => {
- return this.treeList.getSelectedRowsData();
- }
- }
- get treeList() {
- return this.treeListRef.current.instance();
- }
- render() {
- return (
- <TreeList ...
- ref={this.treeListRef}>
- </TreeList>
- );
- }
- }
- export default App;
Clear Selection Settings
Call the deselectRows(keys) method to clear the selection of specific rows. With the non-recursive selection, one key deselects one row; with recursive - a row with its nested rows.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import TreeList from 'devextreme-react/tree-list';
- class App extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- selectedRowKeys: []
- }
- this.deselectRows = this.deselectRows.bind(this);
- this.handleOptionChange = this.handleOptionChange.bind(this);
- }
- deselectRows(keys) {
- let selectedRowKeys = [...this.state.selectedRowKeys];
- keys.forEach(function(item) {
- const index = selectedRowKeys.indexOf(item);
- if (index !== -1) {
- selectedRowKeys.splice(index, 1);
- }
- });
- this.setState({
- selectedRowKeys: selectedRowKeys
- });
- }
- handleOptionChange(e) {
- if(e.fullName === 'selectedRowKeys') {
- this.setState({
- selectedRowKeys: e.value
- });
- }
- }
- render() {
- return (
- <TreeList ...
- selectedRowKeys={this.state.selectedRowKeys}
- onOptionChanged={this.handleOptionChange}>
- </TreeList>
- );
- }
- }
- export default App;
The deselectAll() method clears selection of all visible rows and can be used when you apply a filter and want to keep the selection of invisible rows that do not meet the filtering conditions. To clear the selection of all rows regardless of their visibility, call the clearSelection() method.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import TreeList from 'devextreme-react/tree-list';
- class App extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- selectedRowKeys: []
- }
- this.treeListRef = React.createRef();
- this.deselectAllRows = this.deselectAllRows.bind(this);
- this.deselectVisibleRows = this.deselectVisibleRows.bind(this);
- }
- deselectAllRows() {
- this.setState({
- selectedRowKeys: []
- });
- }
- deselectVisibleRows() {
- this.treeListRef.current.instance().deselectAll();
- }
- handleOptionChange(e) {
- if(e.fullName === 'selectedRowKeys') {
- this.setState({
- selectedRowKeys: e.value
- });
- }
- }
- render() {
- return (
- <TreeList ...
- ref="treeListRef"
- selectedRowKeys={this.state.selectedRowKeys}
- onOptionChanged={this.handleOptionChange}>
- </TreeList>
- );
- }
- }
- export default App;
See Also
Events
The TreeList UI component raises the selectionChanged event when a row is selected or when the selection is cancelled. If the function that handles this event is going to remain unchanged, assign it to the onSelectionChanged property when you configure the UI component.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import TreeList from 'devextreme-react/tree-list';
- class App extends React.Component {
- constructor(props) {
- super(props);
- this.onSelectionChanged = this.onSelectionChanged.bind(this);
- }
- onSelectionChanged(e) {
- const currentSelectedRowKeys = e.currentSelectedRowKeys;
- const currentDeselectedRowKeys = e.currentDeselectedRowKeys;
- const allSelectedRowKeys = e.selectedRowKeys;
- const allSelectedRowsData = e.selectedRowsData;
- // ...
- }
- render() {
- return (
- <TreeList ...
- onSelectionChanged={this.onSelectionChanged}>
- </TreeList>
- );
- }
- }
- export default App;
See Also
If you have technical questions, please create a support ticket in the DevExpress Support Center.