React Tree Grid Sorting

    La función de ordenamiento de datos Ignite UI for React en React Tree Grid está habilitada a nivel de columna, lo que significa que IgrTreeGrid puede tener una combinación de columnas ordenables y no ordenables. Realizar acciones de ordenamiento React le permite cambiar el orden de visualización de los registros en función de criterios específicos.

    React Tree Grid Sorting Overview Example

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

    Esto se hace a través de la sortable entrada. Con la IgrTreeGrid ordenación, también puede establecer la propiedad para realizar la sortingIgnoreCase ordenación entre mayúsculas y minúsculas:

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

    Sorting Indicators

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

    Proporciona IgrTreeGrid una solución para este problema indicando el índice de cada columna ordenada.

    EXAMPLE
    DATA
    TSX
    CSS

    Ignite UI for React | CTA Banner

    Sorting through the API

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

    import { SortingDirection } from "igniteui-react-grids";
    tsx
    // Perform a case insensitive ascending sort on the Category column.
    treeGridRef.current.sort([{ fieldName: 'Category', dir: SortingDirection.Asc, ignoreCase: true }]);
    
    // Perform sorting on both the Category and Price columns.
    treeGridRef.current.sort([
        { fieldName: 'Category', 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.

    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 Category column
    treeGridRef.current.clearSort('Category');
    
    // Removes the sorting state from every column in the Tree Grid
    treeGridRef.current.clearSort();
    tsx

    The sortStrategy of the IgrTreeGrid 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 IgrTreeGrid.

    Initial Sorting State

    Es posible establecer el estado de ordenación inicial de la IgrTreeGrid pasando una matriz de expresiones de ordenación a la sortingExpressions propiedad de the IgrTreeGrid.

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

    If values of type string are used by a column of dataType Date, the IgrTreeGrid won't parse them to Date objects and using IgrTreeGrid 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.
    function sortHeaderIconTemplate(ctx: IgrGridHeaderTemplateContext) {
        return (
            <>
                <IgrIcon name='unfold_more'></IgrIcon>
            </>
        );
    }
    
    <IgrTreeGrid sortHeaderIconTemplate={sortHeaderIconTemplate}></IgrTreeGrid>
    tsx
    function sortAscendingHeaderIconTemplate(ctx: IgrGridHeaderTemplateContext) {
        return (
            <>
                <IgrIcon name='expand_less'></IgrIcon>
            </>
        );
    }
    
    <IgrTreeGrid sortAscendingHeaderIconTemplate={sortAscendingHeaderIconTemplate}></IgrTreeGrid>
    tsx
    function sortDescendingHeaderIconTemplate(ctx: IgrGridHeaderTemplateContext) {
        return (
            <>
                <IgrIcon name='expand_more'></IgrIcon>
            </>
        );
    }
    
    <IgrTreeGrid sortDescendingHeaderIconTemplate={sortDescendingHeaderIconTemplate}></IgrTreeGrid>
    tsx

    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:

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

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

    Demo

    EXAMPLE
    DATA
    TSX
    CSS

    API References

    Additional Resources

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