Skip to main content

filter()

filters controls by some criteria

filter(rule?: function | object, config?: object, silent?: boolean): string;

Parameters:

  • rule?: function | object - the filtering criteria
    • If set as a function, filtering will be applied by the rule specified in the function. It takes as a parameter a data item and returns true/false
    • If set as an object, the parameter has the following attributes:
      • by?: string | number - optional, the id of a data field
      • match?: string - optional, a pattern to match
      • compare?: function - optional, a function for extended filtering that takes the following parameters:
        • value - the value to compare
        • match - a pattern to match
        • item - a data item the values of which should be compared
        • multi - the value of the multi attribute of the rule
      • multi?: boolean - optional, marks the field as holding several values at once (e.g. a multiselect column stores them as a comma-separated string). Passed to compare as its last argument
  • config?: object - optional, defines the parameters of filtering. The parameter may contain the following properties:
    • type?: string - optional, defines the area the filtering will be applied: "all", "level", "leafs"
    • level?: number - optional, the level the filtering will be applied to
    • add?: boolean - optional, defines whether each next filtering will be applied to the already filtered data (true), or to the initial data (false, default)
    • id?: string - optional, the id of the filter
    • permanent?: boolean - optional, true to make the current filter permanent. It is applied even if the next filtering doesn't have the add:true property in its configuration object. It is not dropped by a plain resetFilter() call and is reapplied to the data after parse() or load(); pass permanent:true to resetFilter() to remove it as well
  • silent?: boolean - optional, if set to true, the method will be called without triggering events, false by default
info

Note that after calling the method with the silent:true parameter, you may need to repaint the component with the paint() method.

Returns:

  • id: string - the id of the filter

Example:

const grid = new dhx.Grid("grid_container", {
type: "tree",
columns: [
// columns config
],
data: dataset,
});

// filtering data by a function
grid.data.filter(function (item) {
return item.value.toLowerCase().indexOf("a") !== -1;
});
const grid = new dhx.Grid("grid_container", {
type: "tree",
columns: [
// columns config
],
data: dataset,
});

// filtering data by the column
grid.data.filter({
by: "name",
match: "Angola"
});

Unless config.add is set, the method replaces the currently applied filters; calling it without a rule at all drops all non-permanent filters and restores the unfiltered order. Permanent filters are the exception: they always survive and are reapplied first. The new rule then narrows their result further, so an item remains in the result only if it matches both the permanent filter and the new rule.

Related sample: Grid (TreeGrid). Filter