Filtering
Disabling operators
Disable filtering without any code in the field settings in the Forest UI.
Substitution
Operation substitution serves two purposes:
- Performance: provide a more efficient way to perform a given filtering operation
- Capabilities: enable filtering on a computed field or other non-filterable fields
collection.replaceFieldOperator('fullName', 'Equal', (value, context) => {
const [firstName, ...lastNames] = value.split(' ');
return {
aggregator: 'And',
conditions: [
{ field: 'firstName', operator: 'Equal', value: firstName },
{ field: 'lastName', operator: 'Equal', value: lastNames.join(' ') },
],
};
});
Operators to support to enable search
| Column Type | Operator to support |
|---|
| Number | Equal |
| Enum | Equal |
| String | IContains OR Contains OR Equal |
| Uuid | Equal |
Use the replaceFieldOperator method to unlock the operators.
Emulation
Filtering emulation allows making fields filterable automatically.
It is a convenient way to get things working quickly for collections that have a low number of records (in the thousands at most).
This emulation forces the back-end to retrieve all the collection records and compute the field values for each one of them.
As a consequence, filtering emulation performance cost is linear with the number of records in the collection, so activate it sparingly and with great care.
// Add support for all operators
collection.emulateFieldFiltering('fullName');
// Add support for a single operator
collection.emulateFieldOperator('fullName', 'Equal');
Sorting
Depending on the data source, not all fields may be sortable, or you may want to change how the native sorting works.
Use the replaceFieldSorting and emulateFieldSorting methods to change a single column’s sorting behavior.
Substitution
Provide replacement sort clauses. In this example, we’re telling Forest “When a user sorts by full name, I want to sort by the last name, and then by the first name”.
collection.replaceFieldSorting('fullName', [
{ field: 'lastName', ascending: true },
{ field: 'firstName', ascending: true },
]);
Another very common reason is performance. For instance, with auto-incrementing ids, sorting by creationDate is equivalent to sorting by the primary key in reverse order.
Using sort substitution where needed can save you from adding many indexes to your database.
// Sorting by creationDate ascending <=> Sorting by id descending
collection.replaceFieldSorting('creationDate', [{ field: 'id', ascending: false }]);
Emulation
Sorting emulation allows making any field automatically sortable. It will sort records by lexicographical order.
It is a convenient way to get things working quickly for collections that have a low number of records (in the thousands at most).
This emulation forces the back-end to retrieve all the collection records and compute the field values for each one of them.
As a consequence, sorting emulation performance cost is linear with the number of records in the collection, so activate it sparingly and with great care.
collection.emulateFieldSorting('fullName');