Blazor Tree Grid Sorting

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

    Blazor Tree Grid Sorting Overview Example

    EXAMPLE
    MODULES
    DATA
    RAZOR
    CSS

    This is done via the Sortable input. With the IgbTreeGrid sorting, you can also set the SortingIgnoreCase property to perform case sensitive sorting:

    <IgbColumn Field="Title" Sortable="true"></IgbColumn>
    razor

    Sorting Indicators

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

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

    EXAMPLE
    MODULES
    DATA
    RAZOR
    CSS

    Sorting through the API

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

    @code {
        this.grtreeGridid.SortAsync(new IgbSortingExpression[]
            {
                new IgbSortingExpression
                {
                    FieldName = "Category",
                    Dir = SortingDirection.Asc
                },
                new IgbSortingExpression
                {
                    FieldName = "Price",
                    Dir = SortingDirection.Desc
                }
            });
    }
    razor

    Sorting is performed using our DefaultSortingStrategy algorithm. Any IgbColumn 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:

    @code {
        @*Removes the sorting state from the Category column*@
        this.treeGrid.ClearSortAsync("Category");
    
        @*Removes the sorting state from every column in the Grid*@
        this.treeGrid.ClearSortAsync("");
    }
    razor

    The SortStrategy of the IgbTreeGrid is of different type compared to the SortStrategy of the IgbColumn, since they work in different scopes and expose different parameters.

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

    Initial Sorting State

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

    @code {
        protected override void OnAfterRender(bool first)
        {
            if (first)
            {
                this.treeGrid.SortingExpressions = new IgbSortingExpression[]{
                    new IgbSortingExpression()
                    {
                        FieldName = "Category",
                        Dir = SortingDirection.Asc
                    }};
            }
        }
    }
    razor

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

    <IgbTreeGrid SortHeaderIconTemplate="SortDefaultTemplate"></IgbTreeGrid>
    
    @code {
        public RenderFragment<IgbGridHeaderTemplateContext> SortDefaultTemplate = (ctx) =>
        {
            return @<IgbIcon Size="SizableComponentSize.Small" IconName="unfold_more" Collection="material"></IgbIcon>;
        };
    }
    razor
    <IgbTreeGrid SortAscendingHeaderIconTemplate="SortAscendingTemplate"></IgbTreeGrid>
    
    @code {
        public RenderFragment<IgbGridHeaderTemplateContext> SortAscendingTemplate = (ctx) =>
        {
            return @<IgbIcon Size="SizableComponentSize.Small" IconName="expand_less" Collection="material"></IgbIcon>;
        };
    }
    razor
    <IgbTreeGrid SortDescendingHeaderIconTemplate="SortDescendingTemplate"></IgbTreeGrid>
    
    @code {
        public RenderFragment<IgbGridHeaderTemplateContext> SortDescendingTemplate = (ctx) =>
        {
            return @<IgbIcon Size="SizableComponentSize.Small" IconName="expand_more" Collection="material"></IgbIcon>;
        };
    }
    razor

    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:

    <IgbTreeGrid class="grid">
    </IgbTreeGrid>
    razor

    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
    MODULES
    DATA
    RAZOR
    CSS

    API References

    Additional Resources

    Our community is active and always welcoming to new ideas.