User Interaction
With the TreeList UI component, a user can sort by single and multiple columns. Use the sorting.mode property to specify the current sorting mode.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import { TreeList, Sorting } from 'devextreme-react/tree-list';
- class App extends React.Component {
- render() {
- return (
- <TreeList>
- <Sorting mode="single"/> {/* or "multiple" | "none" */}
- </TreeList>
- );
- }
- }
- export default App;
In single mode, the user can click a column header to sort by the column. Subsequent clicks on the same header reverse the sort order. When the user sorts by a column, the sorting settings of other columns are canceled.
In multiple mode, the user clicks a column header while pressing the Shift key to sort by the column. Sorting settings of other columns remain intact.
In both modes, the user can use the column header's context menu to apply sorting. Note that no matter how user applies sorting, rows are sorted within their hierarchical level.
To cancel a column's sorting settings, the user clicks the column's header while pressing Ctrl or uses the context menu:
To disable sorting in the whole UI component, set the sorting.mode property to "none"; to disable sorting only in a specific column, use its allowSorting property.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import { TreeList, Column } from 'devextreme-react/tree-list';
- class App extends React.Component {
- render() {
- return (
- <TreeList>
- <Column allowSorting={false}/>
- </TreeList>
- );
- }
- }
- export default App;
See Also
Initial and Runtime Sorting
Rows are sorted according to the data source by default. Set the sortOrder property to sort rows in the required order. Specify the sortIndex property as well to sort by multiple columns. Otherwise, each sorted column gets a sort index according to its position in the columns array. For example, the following code sorts rows first by the "Surname", then by the "Name" column:
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import { TreeList, Column } from 'devextreme-react/tree-list';
- class App extends React.Component {
- render() {
- return (
- <TreeList>
- <Column
- dataField="City"
- defaultSortIndex={1}
- defaultSortOrder="asc" />
- <Column
- dataField="Country"
- defaultSortIndex={0}
- defaultSortOrder="asc" />
- </TreeList>
- );
- }
- }
- export default App;
Change the sortOrder and sortIndex properties using the columnOption method to sort at runtime.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import { TreeList, Column } from 'devextreme-react/tree-list';
- class App extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- countrySortOrder: "asc"
- };
- }
- render() {
- return (
- <TreeList ...
- onOptionChanged={this.onOptionChanged}>
- <Column
- dataField="Country"
- sortOrder={this.state.countrySortOrder} />
- </TreeList>
- );
- }
- sortByCountries = (order) => {
- this.setState({
- countrySortOrder: order
- });
- }
- onOptionChanged = (e) => {
- if (e.fullName === "columns[0].sortOrder") {
- this.sortByCountries(e.value);
- }
- }
- }
- export default App;
Custom Sorting
Implement a custom sorting routine using the calculateSortValue property if standard sorting does not meet your requirements. It accepts the name of the data source field that provides values to be used in sorting...
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import { TreeList, Column } from 'devextreme-react/tree-list';
- class App extends React.Component {
- render() {
- return (
- <TreeList>
- {/* dataField provides values for the column */}
- {/* calculateSortValue provides values to be used in sorting */}
- <Column
- dataField="Position"
- calculateSortValue="isOnVacation" />
- </TreeList>
- );
- }
- }
- export default App;
...or a function that returns such a value.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import { TreeList, Column } from 'devextreme-react/tree-list';
- class App extends React.Component {
- render() {
- return (
- <TreeList ...>
- <Column
- dataField="Position"
- defaultSortOrder="asc"
- calculateSortValue={this.calculateSortValue} />
- </TreeList>
- );
- }
- calculateSortValue(rowData) {
- if (rowData.Position == "CEO")
- return this.sortOrder == 'asc' ? "aaa" : "zzz"; // CEOs are always displayed at the top
- else
- return rowData.Position; // Others are sorted as usual
- }
- }
- export default App;
Clear Sorting Settings
You can clear sorting settings for all columns by calling the clearSorting() method, or for a specific column - by setting its sortIndex property to undefined using the columnOption method.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import { TreeList, Column } from 'devextreme-react/tree-list';
- class App extends React.Component {
- constructor(props) {
- super(props);
- this.treeListRef = React.createRef();
- }
- get treeList() {
- return this.treeListRef.current.instance();
- }
- render() {
- return (
- <TreeList ref={this.treeListRef} ...>
- <Column
- dataField="Name"
- defaultSortIndex={0} />
- </TreeList>
- );
- }
- clearNameColumnSorting = () => {
- this.treeList.columnOption("Name", "sortIndex", undefined);
- }
- clearAllSorting = () => {
- this.treeList.clearSorting();
- }
- }
- export default App;
See Also
If you have technical questions, please create a support ticket in the DevExpress Support Center.