createFilterRule
Description
Creates a common filtering rule for several input fieldsUsage
createFilterRule(
fields: {
id: string,
[key: string]: any,
}[],
type: string,
value: number | string,
): {
glue: "or",
rules: {
field: string,
filter: string,
value: number | string,
}[];
} | null
Parameters
fields
- (required) an array of field objects. Each object must contain at least theid
property, which is the field identifier.type
- (required) filter operator, e.g. "equal", "greater", "contains", and othersvalue
- (required) the value used for filtering. If this is falsy (e.g., an empty string), the function returns null
Returns
It returns an object with rules connected with OR logic that can be used for filtering. The object contains:
glue
- (required) the "or" logic for combining filtering rulesrules
- (required) an array of filtering rules. Each rule object has the following parameters:field
- (required) the id of a field. It is the same as the corresponding field id in thefields
parameter.filter
- (required) the filter operatorvalue
- (required) value for filtering
Example
This example demonstrates how to use the createFilterRule
helpers to convert fields configuration into rules and then use them in the filtering function returned by the createFilter
helper
import { getData } from "../data";
import { createFilterRule, createFilter } from "wx-svelte-filter";
const { fields, data } = getData();
const rules = createFilterRule(fields, "contains", "b");
const filter = createFilter(rules);
const filteredData = data.filter(filter);