Web Components Clasificación de cuadrículas de árboles

    The Ignite UI for Web Components Data Sorting feature in Web Components Tree Grid is enabled on a per-column level, meaning that the IgcTreeGridComponent 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 Tree Grid Sorting Overview Example

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

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

    Sorting Indicators

    Tener una cierta cantidad de columnas ordenadas puede resultar realmente confuso si no hay ninguna indicación del orden de clasificación.

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

    import { SortingDirection } from 'igniteui-webcomponents-grids';
    
    
    // Perform a case insensitive ascending sort on the Category column.
    this.treeGrid.sort([{ fieldName: 'Category', dir: SortingDirection.Asc, ignoreCase: true }]);
    
    // Perform sorting on both the Category and Price columns.
    this.treeGrid.sort([
        { fieldName: 'Category', dir: SortingDirection.Asc, ignoreCase: true },
        { fieldName: 'Price', dir: SortingDirection.Desc }
    ]);
    

    [!Note] Sorting is performed using our DefaultSortingStrategy algorithm. Any IgcColumnComponent 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 Category column
    this.treeGrid.clearSort('Category');
    
    // Removes the sorting state from every column in the Tree Grid
    this.treeGrid.clearSort();
    

    [!Note] The sortStrategy of the IgcTreeGridComponent is of different type compared to the sortStrategy of the IgcColumnComponent, since they work in different scopes and expose different parameters.

    [!Note] The sorting operation DOES NOT change the underlying data source of the IgcTreeGridComponent.

    Initial Sorting State

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

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

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

    El icono del indicador de clasificación en el encabezado de la columna se puede personalizar mediante una plantilla. Las siguientes propiedades están disponibles para crear plantillas del indicador de clasificación para cualquier estado de clasificación (ascendente, descendente, ninguno):

    • SortHeaderIconTemplate – re-templates the sorting icon when no sorting is applied.
    constructor() {
        var grid = this.grid = document.getElementById('grid') as IgcTreeGridComponent;
        grid.data = this.data;
        grid.sortHeaderIconTemplate = this.sortHeaderIconTemplate;
    }
    
    public sortHeaderIconTemplate = (ctx: IgcGridHeaderTemplateContext) => {
        return html`<igc-icon name="unfold_more"></igc-icon>`;
    }
    
    • SortAscendingHeaderIconTemplate – re-templates the sorting icon when the column is sorted in ascending order.
    constructor() {
        var grid = this.grid = document.getElementById('grid') as IgcTreeGridComponent;
        grid.data = this.data;
        grid.sortAscendingHeaderIconTemplate = this.sortAscendingHeaderIconTemplate;
    }
    
    public sortAscendingHeaderIconTemplate = (ctx: IgcGridHeaderTemplateContext) => {
        return html`<igc-icon name="expand_less"></igc-icon>`;
    }
    
    • SortDescendingHeaderIconTemplate – re-templates the sorting icon when the column is sorted in descending order.
    constructor() {
        var grid = this.grid = document.getElementById('grid') as IgcTreeGridComponent;
        grid.data = this.data;
        grid.sortDescendingHeaderIconTemplate = this.sortDescendingHeaderIconTemplate;
    }
    
    public sortDescendingHeaderIconTemplate = (ctx: IgcGridHeaderTemplateContext) => {
        return html`<igc-icon name="expand_more"></igc-icon>`;
    }
    

    Styling

    Además de los temas predefinidos, la cuadrícula se puede personalizar aún más configurando algunas de las propiedades CSS disponibles. En caso de que desee cambiar algunos de los colores, primero debe establecer una clase para la cuadrícula:

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

    Luego establezca las propiedades CSS relacionadas con esta clase:

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

    Demo

    API References

    Additional Resources

    Nuestra comunidad es activa y siempre da la bienvenida a nuevas ideas.