Grid Sorting
In Ignite UI for Angular, Sorting is enabled on a per-column level, meaning that the igx-grid can have a mix of sortable and non-sortable columns.
Demo
Additionally there is a custom contextmenu added for sorting using igx-grid's onContextMenu
Output.
This is done via the sortable
input. With the grid sorting, you can also set the sortingIgnoreCase
property to perform case sensitive sorting:
<igx-column field="ProductName" header="Product Name" [dataType]="'string'" sortable="true"></igx-column>
Sorting through the API
You can sort any column or a combination of columns through the grid API using the grid sort
method:
import { SortingDirection } from 'igniteui-angular';
// Perform a case insensitive ascending sort on the ProductName column.
this.grid.sort({ fieldName: 'ProductName', dir: SortingDirection.Asc, ignoreCase: true });
// Perform sorting on both the ProductName and Price columns.
this.grid.sort([
{ fieldName: 'ProductName', dir: SortingDirection.Asc, ignoreCase: true },
{ fieldName: 'Price', dir: SortingDirection.Desc }
]);
Note
Sorting is performed using our DefaultSortingStrategy
algorithm. Any IgxColumnComponent
or ISortingExpression
can use a custom implementation of the ISortingStrategy
as a substitute algorithm. This is useful when custom sorting needs to be defined for complex template columns, or image columns, for example.
As with the filtering behavior, you can clear sorting state using the clearSort
method:
// Removes the sorting state from the ProductName column
this.grid.clearSort('ProductName');
// Removes the sorting state from every column in the grid
this.grid.clearSort();
Note
The sorting operation DOES NOT change the underlying data source of the grid.
Initial sorting state
It is possible to set the initial sorting state of the grid by passing an array of sorting expressions to the sortingExpressions
property of the grid.
public ngOnInit() {
this.grid.sortingExpressions = [
{ fieldName: 'ProductName', dir: SortingDirection.Asc, ignoreCase: true },
{ fieldName: 'Price', dir: SortingDirection.Desc }
];
}
Note
If values of type string
are used by column of dataType
Date
, the grid won't parse it to Date
objects and using igxGrid sorting
won't work as expected. If you want to use string
objects, additional logic should be implemented on an application level, in order to parse the values to Date
object.
Remote Sorting
You can provide grid's remote sorting by subscribing to onDataPreLoad
and onSortingDone
outputs. More information on how to use it you can find in the Grid Virtualization and Performance
documentation.