Sorting
Index
Demo
Description
Sorting on the client side is really easy, you simply need to enable sortable
(when not provided, it is considered as disabled) on each columns you want to sort and it will sort as a type string. Oh but wait, sorting as string might not always be ideal, what if we want to sort by number or by date? The answer is to simply pass a type
as shown below.
Usage
To use any of them, you can use the FieldType
interface or enter a type via a string as shown below. Also please note that FieldType.string
is the default and you don't necessarily need to define it, though you could if you wish to see it in your column definition.
How to Sort Complex Objects?
You can sort complex objects using the dot (.) notation inside the field
property defined in your Columns Definition.
For example, let say that we have this dataset
We can now filter the zip code from the buyer's address using this filter:
Custom Sort Comparer
If the builtin sort comparer methods are not sufficient for your use case, you could add your own custom Sort Comparer in your Column Definitions as shown below. Note that we are only showing a simple numeric sort, just adjust it to your needs.
similarly with a complex object
Update Sorting Dynamically
You can update/change the Sorting dynamically (on the fly) via the updateSorting
method from the SortService
. Note that calling this method will override all sorting (sorters) and replace them with the new array of sorters provided. For example, you could update the sorting from a button click or a select dropdown list with predefined filter set.
View
Component
Extra Arguments
The updateSorting
method has 2 extra arguments:
2nd argument, defaults to true, is to emit a sort changed event (the GridStateService uses this event)
optional and defaults to true
updateSorting([], true)
3rd argument is to trigger a backend query (when using a Backend Service like OData/GraphQL), this could be useful when using updateFilters & updateSorting and you wish to only send the backend query once.
optional and defaults to true
updateSorting([], true, true)
Dynamic Query Field
What if you a field that you only know which field to query only at run time and depending on the item object (dataContext
)? We can defined a queryFieldNameGetterFn
callback that will be executed on each row when Filtering and/or Sorting.
Last updated