To allow the user to sort the DikeGrid rows, you must set a flag at the grid scope by providing an input property named allowSorting. Its default value is false.
We have the following sortable columns with the previous configuration: Name and Surname under the Complete Name group, Age column under the Personal Info group, and the Hire Date column.
You can make a column sortable at runtime using the corresponding API. For example, see the following code snippet:
row-sorting.component.ts
onSetColumnSortable(): void {if (this.columnControl.valid) {// Get the selected column:constcolumn=this.columnControl.value asDikeColumnDef;if (isDikeDataColumnDef(column)) {// Toggle the sortable flag for the selected column:this.dikeGrid.columnDef.setColumnSortable(column,!column.sortable); } }}
As you can see, once you have got the column you want to set as sortable or non-sortable, use the DikeGridColumnDef service instance to change the column state.
Before using the column API, you must grab the instance of the DikeGridColumnDef service.
Sorting rows through the UI
Once you define columns as sortable, you can sort the DikeGrid rows by these sortable columns.
Clicking on the column header
You can sort the DikeGrid rows by clicking on the column header.
When you have clicked on the column header of a sortable column, you will see an arrow that indicates the direction of the sorting operation.
Column Context Menu
When you define a column as sortable, you will see the sorting submenu in the Column Context Menu. Then, you will see the sorting options.
DikeGrid Sorting API
You can sort the DikeGrid rows using the sorting API.
Before using the sorting API, you must retrieve the corresponding DikeGrid instance by querying the component's view.
onSelectionChange(data: MatSelectChange): void {constcolumn=data.value asDikeColumnDef;if (isDikeDataColumnDef(column)) {if (!column.sortable) {this.rowSortingForm.get('direction').disable(); } else {this.rowSortingForm.get('direction').enable(); } }}onSortBy(): void {if (this.rowSortingForm.valid) {constcolumn=this.rowSortingForm.get('column').value asDikeColumnDef;constdirection=this.rowSortingForm.get('direction').value as'asc'|'desc';// if the DikeGrid instance is sorted, get the current columns and the sort direction:constcurrentSortable=this.dikeGrid.sorting.getCurrentSortable();/** * We will sort the DikeGrid rows, if only if: * * 1. The DikeGrid is not sorted. * 2. Or, the DikeGrid is sorted but the selected column is different than the current column. * 3. Or, the DikeGrid is sorted by the selected column but the selected direction is different. */if (isDikeDataColumnDef(column) && (!currentSortable ||currentSortable.sortedColumn.columnId !==column.columnId ||currentSortable.type !== direction)) {this.dikeGrid.sorting.sortBy(column, direction); } }}onSetColumnSortable(): void {if (this.columnControl.valid) {// Get the selected column:constcolumn=this.columnControl.value asDikeColumnDef;if (isDikeDataColumnDef(column)) {// Toggle the sortable flag for the selected column:this.dikeGrid.columnDef.setColumnSortable(column,!column.sortable); } }}
The previous HTML code generates the following output:
With the previous UI, you can do the following tasks:
You can choose any data column from the dropdown control. Sortable columns have an icon indicating they are sortable.
You can change the sortable state of a data column.
To sort the DikeGrid rows, you must select the direction of the sorting operation. Then click on the Sort By button.
It is essential to notice that we are listening to any change in the DikeGrid's columns, attaching a callback to the columnsChange output property from the DikeGridComponent.
Every time we receive a new set of columns, we flat them into a single array, invoking the method flatColumns(). So indeed, we are flattening the column groups, traversing them recursively.
If you see the content of the dropdown control, you see the columns Employee Id, Personal Info group, Complete Name group, then Name and Surname columns, etc.
The column groups are disabled, such as Personal Info and Complete Name.
Sort event
Every time we sort the DikeGrid rows, the DikeGrid instance emits the related event.
The emitted object is of type DikeColumnSortEvent, which contains the column and the direction of the current sorting operation.
Open the dev console to see the output of the sortChange event.
You can also listen to the sortChange event from the DikeGridSorting instance.
Summary
To perform the sorting operation, you must allow it by providing an input property named allowSorting. Then, when creating columns, you can define them as sortable. Nevertheless, you can change their state at runtime using the related API. You can perform the sorting operation through the UI or the sorting API.