React Hierarchical Grid Sorting

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

    React Hierarchical Grid Sorting Overview Example

    Additionally there is a custom contextmenu added for sorting using IgrHierarchicalGrid's ContextMenu Output.

    EXAMPLE
    TSX
    CSS

    Like this sample? Get access to our complete Ignite UI for React toolkit and start building your own apps in minutes. Download it for free.

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

    <IgrColumn field="ProductName" header="Product Name" dataType="string" sortable="true"></IgrColumn>
    tsx

    Sorting Indicators

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

    The IgrHierarchicalGrid provides a solution for this problem by indicating the index of each sorted column.

    EXAMPLE
    TSX
    CSS

    Ignite UI for React | CTA Banner

    Sorting through the API

    You can sort any column or a combination of columns through the IgrHierarchicalGrid API using the IgrHierarchicalGrid sort method:

    import { SortingDirection } from "igniteui-react-grids";
    tsx
    // Perform a case insensitive ascending sort on the ProductName column.
    hierarchicalGridRef.current.sort([{ fieldName: 'ProductName', dir: SortingDirection.Asc, ignoreCase: true }]);
    
    // Perform sorting on both the ProductName and Price columns.
    hierarchicalGridRef.current.sort([
        { fieldName: 'ProductName', dir: SortingDirection.Asc, ignoreCase: true },
        { fieldName: 'Price', dir: SortingDirection.Desc }
    ]);
    tsx

    Sorting is performed using our DefaultSortingStrategy algorithm. Any IgrColumn 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 the sorting state by using the clearSort method:

    // Removes the sorting state from the ProductName column
    hierarchicalGridRef.current.clearSort('ProductName');
    
    // Removes the sorting state from every column in the Hierarchical Grid
    hierarchicalGridRef.current.clearSort();
    tsx

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

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

    Initial Sorting State

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

    useEffect(() => {
        hierarchicalGridRef.current.sortingExpressions = [
            { fieldName: 'UnitsInStock', dir: SortingDirection.Asc, ignoreCase: true },
            { fieldName: 'ProductName', dir: SortingDirection.Desc }
        ];
    }, [])
    tsx

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

    function sortHeaderIconTemplate(ctx: IgrGridHeaderTemplateContext) {
        return (
            <>
                <IgrIcon name='unfold_more'></IgrIcon>
            </>
        );
    }
    
    <IgrHierarchicalGrid sortHeaderIconTemplate={sortHeaderIconTemplate}></IgrHierarchicalGrid>
    tsx
    function sortAscendingHeaderIconTemplate(ctx: IgrGridHeaderTemplateContext) {
        return (
            <>
                <IgrIcon name='expand_less'></IgrIcon>
            </>
        );
    }
    
    <IgrHierarchicalGrid sortAscendingHeaderIconTemplate={sortAscendingHeaderIconTemplate}></IgrHierarchicalGrid>
    tsx
    function sortDescendingHeaderIconTemplate(ctx: IgrGridHeaderTemplateContext) {
        return (
            <>
                <IgrIcon name='expand_more'></IgrIcon>
            </>
        );
    }
    
    <IgrHierarchicalGrid sortDescendingHeaderIconTemplate={sortDescendingHeaderIconTemplate}></IgrHierarchicalGrid>
    tsx

    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:

    <IgrHierarchicalGrid className="grid">
    </IgrHierarchicalGrid>
    tsx

    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;
    }
    css

    Demo

    EXAMPLE
    TSX
    CSS

    API References

    Additional Resources

    Our community is active and always welcoming to new ideas.