A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.infragistics.com/products/ignite-ui-angular/angular/components/grid/sorting.html below:

Angular Grid Sorting - Ignite UI for Angular

Angular Grid Sorting

In Ignite UI for Angular Grid, data sorting is enabled on a per-column level, meaning that the igx-grid can have a mix of sortable and non-sortable columns. Performing angular sort actions enables you to change the display order of the records based on specified criteria.

Note

Up until now, grouping/sorting worked in conjunction with each other. In 13.2 version, a new behavior which decouples grouping from sorting is introduced. For example - clearing the grouping will not clear sorting expressions in the grid or vice versa. Still, if a column is both sorted and grouped, grouped expressions take precedence.

Angular Grid Sorting Overview Example

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 Indicators

Having a certain amount of sorted columns could be really confusing if there is no indication of the sorted order.

The IgxGrid provides a solution for this problem by indicating the index of each sorted 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';
// import { SortingDirection } from '@infragistics/igniteui-angular'; for licensed package

// 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 }
]);

As with the filtering behavior, you can clear the sorting state by 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 sortStrategy of the Grid is of different type compared to the sortStrategy of the column, since they work in different scopes and expose different parameters.

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 ngAfterViewInit(): void {
    this.grid.sortingExpressions = [
        {
            dir: SortingDirection.Asc, fieldName: 'CategoryName',
            ignoreCase: true, strategy: DefaultSortingStrategy.instance()
        }
    ];
}
Note

If values of type string are used by a column of dataType Date, the Grid won't parse them to Date objects and using Grid 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 objects.

Remote Sorting

The Grid supports remote sorting, which is demonstrated in the Grid Remote Data Operations topic.

Sorting Indicators Templates

The sorting indicator icon in the column header can be customized using a template. The following directives are available for templating the sorting indicator for any sorting state (ascending, descending, none):

<ng-template igxSortHeaderIcon>
    <igx-icon>unfold_more</igx-icon>
</ng-template>
<ng-template igxSortAscendingHeaderIcon>
    <igx-icon>expand_less</igx-icon>
</ng-template>
<ng-template igxSortDescendingHeaderIcon>
    <igx-icon>expand_more</igx-icon>
</ng-template>
Styling

To get started with styling the sorting behavior, we need to import the index file, where all the theme functions and component mixins live:

@use "igniteui-angular/theming" as *;

// IMPORTANT: Prior to Ignite UI for Angular version 13 use:
// @import '~igniteui-angular/lib/core/styles/themes/index';

Following the simplest approach, we create a new theme that extends the grid-theme and accepts the $sorted-header-icon-color and sortable-header-icon-hover-color parameters.

$custom-theme: grid-theme(
  $sorted-header-icon-color: #ffb06a,
  $sortable-header-icon-hover-color: black
);
Note

Instead of hardcoding the color values like we just did, we can achieve greater flexibility in terms of colors by using the palette and color functions. Please refer to Palettes topic for detailed guidance on how to use them.

The last step is to include the component mixins:

@include css-vars($custom-theme);
Demo Note

The sample will not be affected by the selected global theme from Change Theme.

API References Additional Resources Our community is active and always welcoming to new ideas.

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4