createArrayFilter
Description
The function takes filter value as an input and returns a filtered arrayIt uses the createFilter
helper inside.
Usage
createArrayFilter:(
value: {
glue: "and" | "or",
rules: {
field: string,
filter?: string,
value?: string | number | date,
includes?: any[],
type?: "date"|"number"|"text"|"tuple",
}[]
},
options?: {
orNull?: boolean,
}
): (data:any[]) => data:any[];
Parameters
value
- (required) filter settings:glue
- (required) the logic for combining filtering rules:- "and" (default) - displays records that correspond to all rules at once
- "or" - shows records that correspond to at least one of the rules
rules
- (required) an array of filtering rules. It can be an individual rule or a group of rules. Each rule object has the following parameters:field
- (required) the id of a field. It should be the same as the corresponding field id of thefields
property.filter
- (optional) the filter operator available for this typevalue
- (optional) the value in the fieldincludes
- (optional) an array of the included values (strings, numbers or dates).type
- (optional) filter type: "text" (default) | "number" | "date" | "tuple"
options
- (optional) it's applied when no rules are set; if set to true, null will be returned; if false (default), an array will be returned
Returns
A function takes an array and returns its filtered copy.
Example
This example demonstrates how to use createArrayFilter
to generate a function to filter a data array.
<script>
import { getData } from "../data";
import { FilterBuilder, createArrayFilter } from "wx-svelte-filter";
const { value, fields, options, data } = getData();
let filteredData = $state([]);
function applyFilter(value) {
const filter = createArrayFilter(value);
filteredData = filter(data);
}
// Apply initial filter
applyFilter(value);
</script>
<FilterBuilder
{value}
{fields}
{options}
onchange={({ value }) => applyFilter(value)}
/>