Clasificación de cuadrículas Web Components

    La función de clasificación de datos Ignite UI for Web Components en Web Components Grid está habilitada a nivel de columna, lo que significa que IgcGridComponent puede tener una combinación de columnas ordenables y no ordenables. La realización de acciones de clasificación Web Components le permite cambiar el orden de visualización de los registros según criterios específicos.

    Web Components Grid Sorting Overview Example

    Esto se hace a través de la entrada sortable. Con la clasificación IgcGridComponent, también puede configurar la propiedad sortingIgnoreCase para realizar una clasificación que distinga entre mayúsculas y minúsculas:

    <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.

    IgcGridComponent proporciona una solución a este problema indicando el índice de cada columna ordenada.

    Sorting through the API

    Puede ordenar cualquier columna o una combinación de columnas a través de la API IgcGridComponent utilizando el método sort IgcGridComponent:

    import { SortingDirection } from 'igniteui-webcomponents-grids';
    
    
    // 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 }
    ]);
    

    [!Note] Sorting is performed using our DefaultSortingStrategy algorithm. Any IgcColumn 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.

    Al igual que con el comportamiento de filtrado, puede borrar el estado de clasificación utilizando el método clearSort:

    // 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 IgcGridComponent is of different type compared to the sortStrategy of the IgcColumn, since they work in different scopes and expose different parameters.

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

    Initial Sorting State

    Es posible establecer el estado de clasificación inicial de IgcGridComponent pasando una matriz de expresiones de clasificación a la propiedad sortingExpressions de IgcGridComponent.

    public connectedCallback() {
        this.grid.sortingExpressions = [
            { fieldName: 'ProductName', 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 IgcGridComponent won't parse them to Date objects and using IgcGridComponent 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: vuelve a crear una plantilla para el icono de clasificación cuando no se aplica ninguna clasificación.
    constructor() {
        var grid = this.grid = document.getElementById('grid') as IgcGridComponent;
        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 IgcGridComponent;
        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 IgcGridComponent;
        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-grid class="grid">
    </igc-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.