Close
Angular React Web Components Blazor Web Components
Premium

Web Components Hierarchical Grid Sorting

The Ignite UI for Web Components Data Sorting feature in Web Components Hierarchical Grid is enabled on a per-column level, meaning that the IgcHierarchicalGrid can have a mix of sortable and non-sortable columns. Performing Web Components sort actions enables you to change the display order of the records based on specified criteria.

Web Components Hierarchical Grid Sorting Overview Example

Additionally there is a custom contextmenu added for sorting using IgcHierarchicalGrid‘s IgcIgcHierarchicalGrid.contextMenu Output.

This is done via the Column.sortable input. With the IgcHierarchicalGrid sorting, you can also set the IgcColumnState.sortingIgnoreCase property to perform case sensitive sorting:

<igc-column field="ProductName" header="Product Name" data-type="string" sortable="true"></igc-column>

Sorting Indicators

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

The IgcHierarchicalGrid 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 IgcHierarchicalGrid API using the Sort method:

import { SortingDirection } from 'igniteui-webcomponents-grids';

// Perform a case insensitive ascending sort on the ProductName column.
this.hierarchicalGrid.sort([{ fieldName: 'ProductName', dir: SortingDirection.Asc, ignoreCase: true }]);

// Perform sorting on both the ProductName and Price columns.
this.hierarchicalGrid.sort([
    { fieldName: 'ProductName', dir: SortingDirection.Asc, ignoreCase: true },
    { fieldName: 'Price', dir: SortingDirection.Desc }
]);

Sorting is performed using our IgcGridSortingStrategy algorithm. Any Column or ISortingExpression can use a custom implementation of the IgcGridSortingStrategy 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 the sorting state by using the IgcHierarchicalGrid.clearSort method:

// Removes the sorting state from the ProductName column
this.hierarchicalGrid.clearSort('ProductName');

// Removes the sorting state from every column in the Hierarchical Grid
this.hierarchicalGrid.clearSort();

The IgcHierarchicalGrid.sortStrategy of the IgcHierarchicalGrid is of different type compared to the IgcHierarchicalGrid.sortStrategy of the Column, since they work in different scopes and expose different parameters.

The sorting operation DOES NOT change the underlying data source of the IgcHierarchicalGrid.

Initial Sorting State

It is possible to set the initial sorting state of the IgcHierarchicalGrid by passing an array of sorting expressions to the IgcHierarchicalGrid.sortingExpressions property of the IgcHierarchicalGrid.

public connectedCallback() {
    this.hierarchicalGrid.sortingExpressions = [
        { fieldName: 'ProductName', dir: SortingDirection.Asc, ignoreCase: true },
        { fieldName: 'Price', dir: SortingDirection.Desc }
    ];
}

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

Sorting Indicators Templates

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

constructor() {
    var grid = this.grid = document.getElementById('grid') as IgcHierarchicalGridComponent;
    grid.data = this.data;
    grid.sortHeaderIconTemplate = this.sortHeaderIconTemplate;
}

public sortHeaderIconTemplate = (ctx: IgcGridHeaderTemplateContext) => {
    return html`<igc-icon name="unfold_more"></igc-icon>`;
}
constructor() {
    var grid = this.grid = document.getElementById('grid') as IgcHierarchicalGridComponent;
    grid.data = this.data;
    grid.sortAscendingHeaderIconTemplate = this.sortAscendingHeaderIconTemplate;
}

public sortAscendingHeaderIconTemplate = (ctx: IgcGridHeaderTemplateContext) => {
    return html`<igc-icon name="expand_less"></igc-icon>`;
}
constructor() {
    var grid = this.grid = document.getElementById('grid') as IgcHierarchicalGridComponent;
    grid.data = this.data;
    grid.sortDescendingHeaderIconTemplate = this.sortDescendingHeaderIconTemplate;
}

public sortDescendingHeaderIconTemplate = (ctx: IgcGridHeaderTemplateContext) => {
    return html`<igc-icon name="expand_more"></igc-icon>`;
}

Styling

In addition to the predefined themes, the grid could be further customized by setting some of the available CSS properties. In case you would like to change some of the colors, you need to set a class for the grid first:

<igc-hierarchical-grid class="grid">
</igc-hierarchical-grid>

Then set the related CSS properties to this class:

.grid {
    --ig-grid-sorted-header-icon-color: #ffb06a;
    --ig-grid-sortable-header-icon-hover-color: black;
}

Demo

API References

Additional Resources

Our community is active and always welcoming to new ideas.