React Grid Sorting
The Ignite UI for React Data Sorting feature in React Grid is enabled on a per-column level, meaning that the IgrGrid
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 Grid Sorting Overview Example
This is done via the sortable
input. With the IgrGrid
sorting, you can also set the sortingIgnoreCase
property to perform case sensitive sorting:
<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.
The IgrGrid
provides a solution for this problem by indicating the index of each sorted column.
Sorting through the API
You can sort any column or a combination of columns through the IgrGrid
API using the IgrGrid
sort
method:
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. AnyIgrColumn
orISortingExpression
can use a custom implementation of theISortingStrategy
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 acontece com o comportamento de filtragem, você pode limpar o estado de classificação usando o clearSort
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 theIgrGrid
is of different type compared to thesortStrategy
of theIgrColumn
, 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
It is possible to set the initial sorting state of the IgrGrid
by passing an array of sorting expressions to the sortingExpressions
property of the IgrGrid
.
useEffect(() => {
gridRef.current.sortingExpressions = [
{ fieldName: 'UnitsInStock', dir: SortingDirection.Asc, ignoreCase: true },
{ fieldName: 'ProductName', dir: SortingDirection.Desc }
];
}, [])
[!Note] If values of type
string
are used by a column ofdataType
Date
, theIgrGrid
won't parse them toDate
objects and usingIgrGrid
Sorting
won't work as expected. If you want to usestring
objects, additional logic should be implemented on an application level, in order to parse the values toDate
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):
sortHeaderIconTemplate
– re-templates the sorting icon when no sorting is applied.
function sortHeaderIconTemplate(ctx: IgrGridHeaderTemplateContext) {
return (
<>
<IgrIcon name='unfold_more'></IgrIcon>
</>
);
}
<IgrGrid sortHeaderIconTemplate={sortHeaderIconTemplate}></IgrGrid>
sortAscendingHeaderIconTemplate
– re-templates the sorting icon when the column is sorted in ascending order.
function sortAscendingHeaderIconTemplate(ctx: IgrGridHeaderTemplateContext) {
return (
<>
<IgrIcon name='expand_less'></IgrIcon>
</>
);
}
<IgrGrid sortAscendingHeaderIconTemplate={sortAscendingHeaderIconTemplate}></IgrGrid>
sortDescendingHeaderIconTemplate
– re-templates the sorting icon when the column is sorted in descending order.
function sortDescendingHeaderIconTemplate(ctx: IgrGridHeaderTemplateContext) {
return (
<>
<IgrIcon name='expand_more'></IgrIcon>
</>
);
}
<IgrGrid sortDescendingHeaderIconTemplate={sortDescendingHeaderIconTemplate}></IgrGrid>
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:
<IgrGrid className="grid">
</IgrGrid>
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
- Virtualização e desempenho
- Paginação
- Classificação
- Resumos
- Movimentação de Colunas
- Fixação de coluna
- Redimensionamento de colunas
- Escolha
Nossa comunidade é ativa e sempre acolhedora para novas ideias.