React Grid Sorting

    La función Ignite UI for React Ordenación de Datos en React Grid está habilitada a nivel por columna, lo que significa que puedenIgrGrid tener una mezcla de columnas ordenables y no ordenables. Realizar React acciones de ordenación te permite cambiar el orden de visualización de los registros según criterios especificados.

    React Grid Sorting Overview Example

    Esto se hace mediante lasortable entrada. Con laIgrGrid ordenación, también puedes configurar lasortingIgnoreCase propiedad para realizar ordenación con distinción de mayúsculas y minúsculas:

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

    Sorting Indicators

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

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

    Sorting through the API

    Puedes ordenar cualquier columna o una combinación de columnas a través de laIgrGrid API usando laIgrGrid sort Método:

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

    [!Note] 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.

    Al igual que con el comportamiento de filtrado, puedes borrar el estado de ordenación usando elclearSort método:

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

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

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

    Initial Sorting State

    Es posible establecer el estado inicial de ordenamiento de pasandoIgrGrid un array de expresiones de ordenación a lasortingExpressions propiedad de laIgrGrid.

    const sortingExpressions: IgrSortingExpression[] = [
        { fieldName: 'UnitsInStock', dir: SortingDirection.Asc, ignoreCase: true },
        { fieldName: 'ProductName', dir: SortingDirection.Desc }
    ];
    
    <IgrGrid
        data={productSales}
        sortingExpressions={sortingExpressions}>
    </IgrGrid>
    

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

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

    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:

    <IgrGrid className="grid">
    </IgrGrid>
    

    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.