Angular TreeList - Sorting

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.

HTML
TypeScript
  • <dx-tree-list ... >
  • <dxo-sorting
  • mode="single"> <!-- or "multiple" | "none" -->
  • </dxo-sorting>
  • </dx-tree-list>
  • import { DxTreeListModule } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • // ...
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxTreeListModule
  • ],
  • // ...
  • })

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.

DevExtreme HTML5 JavaScript jQuery Angular Knockout UI component TreeList Sorting

To cancel a column's sorting settings, the user clicks the column's header while pressing Ctrl or uses the context menu:

DevExtreme HTML5 JavaScript jQuery Angular Knockout UI component TreeList Sorting

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.

HTML
TypeScript
  • <dx-tree-list ... >
  • <dxi-column [allowSorting]="false"></dxi-column>
  • </dx-tree-list>
  • import { DxTreeListModule } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • // ...
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxTreeListModule
  • ],
  • // ...
  • })
See Also

API

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:

HTML
TypeScript
  • <dx-tree-list ... >
  • <dxi-column dataField="Name" [sortIndex]="1" sortOrder="asc"></dxi-column>
  • <dxi-column dataField="Surname" [sortIndex]="0" sortOrder="asc"></dxi-column>
  • </dx-tree-list>
  • import { DxTreeListModule } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • // ...
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxTreeListModule
  • ],
  • // ...
  • })

Change the sortOrder and sortIndex properties using the columnOption method to sort at runtime.

TypeScript
  • import { ..., ViewChild } from "@angular/core";
  • import { DxTreeListModule, DxTreeListComponent } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • @ViewChild(DxTreeListComponent, { static: false }) treeList: DxTreeListComponent;
  • // Prior to Angular 8
  • // @ViewChild(DxTreeListComponent) treeList: DxTreeListComponent;
  • sortBySurnames () {
  • this.treeList.instance.columnOption("Surname", {
  • sortOrder: "desc",
  • sortIndex: 2
  • });
  • }
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxTreeListModule
  • ],
  • // ...
  • })

View Demo

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...

HTML
TypeScript
  • <dx-tree-list ... >
  • <dxi-column
  • dataField="Position" <!--provides values for the column -->
  • calculateSortValue="isOnVacation"> <!-- provides values to be used in sorting -->
  • </dxi-column>
  • </dx-tree-list>
  • import { DxTreeListModule } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • // ...
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxTreeListModule
  • ],
  • // ...
  • })

...or a function that returns such a value.

TypeScript
HTML
  • import { DxTreeListModule } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • customSortingFunction (rowData) {
  • let column = this as any;
  • if (rowData.Position == "CEO")
  • return column.sortOrder == 'asc' ? "aaa" : "zzz"; // CEOs are always displayed at the top
  • else
  • return rowData.Position; // Others are sorted as usual
  • }
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxTreeListModule
  • ],
  • // ...
  • })
  • <dx-tree-list ... >
  • <dxi-column
  • dataField="Position"
  • sortOrder="asc"
  • [calculateSortValue]="customSortingFunction">
  • </dxi-column>
  • </dx-tree-list>

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.

TypeScript
  • import { ..., ViewChild } from "@angular/core";
  • import { DxTreeListModule, DxTreeListComponent } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • @ViewChild(DxTreeListComponent, { static: false }) treeList: DxTreeListComponent;
  • // Prior to Angular 8
  • // @ViewChild(DxTreeListComponent) treeList: DxTreeListComponent;
  • clearSortingByNames () {
  • this.treeList.instance.columnOption("Name", "sortIndex", undefined);
  • }
  • clearAllSorting () {
  • this.treeList.instance.clearSorting();
  • }
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxTreeListModule
  • ],
  • // ...
  • })
See Also