Angular Classificação em grade

    No Ignite UI for Angular Grid, a classificação de dados é habilitada em um nível por coluna, o que significa que o igx-grid pode ter uma mistura de colunas classificáveis e não classificáveis. A execução de ações de classificação angular permite alterar a ordem de exibição dos registros com base em critérios especificados.

    Note

    Até agora, o agrupamento/classificação funcionava em conjunto. Na versão 13.2, é introduzido um novo comportamento que separa o agrupamento da classificação. Por exemplo - limpar o agrupamento não limpará as expressões de classificação na grade ou vice-versa. Ainda assim, se uma coluna for classificada e agrupada, as expressões agrupadas terão precedência.

    Angular Grid Sorting Overview Example

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

    <igx-column field="ProductName" header="Product Name" [dataType]="'string'" sortable="true"></igx-column>
    

    Sorting Indicators

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

    O IgxGrid fornece uma solução para esse problema, indicando o índice de cada coluna classificada.

    Sorting through the API

    You can sort any column or a combination of columns through the Grid API using the Grid sort method:

    import { SortingDirection } from 'igniteui-angular';
    // import { SortingDirection } from '@infragistics/igniteui-angular'; for licensed package
    
    // 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 IgxColumnComponent 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 ProductName column
    this.grid.clearSort('ProductName');
    
    // Removes the sorting state from every column in the Grid
    this.grid.clearSort();
    
    Note

    The sortStrategy of the Grid is of different type compared to the sortStrategy of the column, since they work in different scopes and expose different parameters.

    Note

    A operação de classificação NÃO altera a fonte de dados subjacente da Grid.

    Initial sorting state

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

    public ngAfterViewInit(): void {
        this.grid.sortingExpressions = [
            {
                dir: SortingDirection.Asc, fieldName: 'CategoryName',
                ignoreCase: true, strategy: DefaultSortingStrategy.instance()
            }
        ];
    }
    
    Note

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

    Remote Sorting

    The Grid supports remote sorting, which is demonstrated in the Grid Remote Data Operations topic.

    Sorting Indicators Templates

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

    • IgxSortHeaderIconDirective – re-templates the sorting icon when no sorting is applied.
    <ng-template igxSortHeaderIcon>
        <igx-icon>unfold_more</igx-icon>
    </ng-template>
    
    • IgxSortAscendingHeaderIconDirective – re-templates the sorting icon when the column is sorted in ascending order.
    <ng-template igxSortAscendingHeaderIcon>
        <igx-icon>expand_less</igx-icon>
    </ng-template>
    
    • IgxSortDescendningHeaderIconDirective – re-templates the sorting icon when the column is sorted in descending order.
    <ng-template igxSortDescendingHeaderIcon>
        <igx-icon>expand_more</igx-icon>
    </ng-template>
    

    Estilização

    To get started with styling the sorting behavior, we need to import the index file, where all the theme functions and component mixins live:

    @use "igniteui-angular/theming" as *;
    
    // IMPORTANT: Prior to Ignite UI for Angular version 13 use:
    // @import '~igniteui-angular/lib/core/styles/themes/index';
    

    Following the simplest approach, we create a new theme that extends the grid-theme and accepts the $sorted-header-icon-color and sortable-header-icon-hover-color parameters.

    $custom-theme: grid-theme(
      $sorted-header-icon-color: #ffb06a,
      $sortable-header-icon-hover-color: black
    );
    
    Note

    Instead of hardcoding the color values like we just did, we can achieve greater flexibility in terms of colors by using the palette and color functions. Please refer to Palettes topic for detailed guidance on how to use them.

    A última etapa é incluir os mixins de componentes:

    @include css-vars($custom-theme);
    

    Demo

    Note

    The sample will not be affected by the selected global theme from Change Theme.

    API References

    Additional Resources

    Nossa comunidade é ativa e sempre acolhedora para novas ideias.