React Tree Grid Sorting

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

    React Tree Grid Sorting Overview Example

    Isso é feito via entradasortable. Com aIgrTreeGrid ordenação, você também pode configurar asortingIgnoreCase propriedade para realizar ordenação sensível de maiúsculas e minúsculas:

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

    Sorting Indicators

    Ter uma certa quantidade de colunas classificadas pode ser realmente confuso se não houver indicação da ordem classificada.

    OIgrTreeGrid fornece uma solução para esse problema indicando o índice de cada coluna ordenada.

    Sorting through the API

    Você pode ordenar qualquer coluna ou combinação de colunas pelaIgrTreeGrid API usando oIgrTreeGrid sort Método:

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

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

    Assim como no comportamento de filtragem, você pode limpar o estado de ordenação usando oclearSort método:

    // 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();
    

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

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

    Initial Sorting State

    É possível definir o estado inicial de ordenação de oIgrTreeGrid passando um array de expressões de ordenação para asortingExpressions propriedade de oIgrTreeGrid.

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

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

    O ícone do indicador de classificação no cabeçalho da coluna pode ser personalizado usando um modelo. As seguintes propriedades estão disponíveis para criar um modelo do indicador de classificação para qualquer estado de classificação (ascendente, decrescente, nenhum):

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

    Styling

    Além dos temas predefinidos, a grade pode ser ainda mais personalizada ao definir algumas das propriedades CSS disponíveis. Caso você queira alterar algumas das cores, precisa definir uma classe para a grade primeiro:

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

    Em seguida, defina as propriedades CSS relacionadas a esta classe:

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

    Demo

    API References

    Additional Resources

    Nossa comunidade é ativa e sempre acolhedora para novas ideias.