Skip to content
Job Haast edited this page Sep 18, 2024 · 4 revisions

Filter

Overview

The Filter class is an abstract base class that represents a filter used for managing conditions. This class provides common functionality required to implement filters in an application, such as handling filter conditions, triggering events when filters are applied or reset, and managing filter metadata (e.g., column name and display label).

Class: Filter

The Filter class is generic and uses two type parameters:

  • T: The data type of the value on which the filter operates (e.g., string, number, Date).
  • R: The type of operations that the filter supports. This could be one of the following:
    • ComparisonOperation
    • EqualOperation
    • LikeOperation
    • InOperation

These operations represent different ways of filtering the data (e.g., comparison, equality, pattern matching, inclusion).

Constructor

constructor(column: string, label: string, conditions?: Condition<T, R>[])

Parameters:

  • column: string: The name of the column or field that this filter will apply to.
  • label: string: The label or display name for this filter (used for UI purposes).
  • conditions?: Condition<T, R>[]: An optional array of initial filter conditions, specifying how the filter will operate on the data.

Properties

column (public readonly)

public readonly column: string;
  • Type: string
  • Description: The name of the column or field that this filter applies to.

label (public readonly)

public readonly label: string;
  • Type: string
  • Description: The label or display name of this filter. This is typically used in the user interface to describe the filter.

_conditions (protected)

protected _conditions: Condition<T, R>[] = [];
  • Type: Condition<T, R>[]
  • Description: A list of conditions associated with this filter. These conditions define the logic applied to filter the dataset.

conditions (public)

conditions: Condition < T, R > [];
  • Type: Condition<T, R>[]
  • Description: Getter method that returns the list of filter conditions.
  • Returns: An array of Condition instances representing the current filter conditions.

_onReset (private)

_onReset: EventEmitter;
  • Type: EventEmitter
  • Description: An event emitter that emits an event when the filter is reset.

onReset (public)

onReset: EventEmitter;
  • Type: EventEmitter
  • Description: Getter method that returns the EventEmitter triggered when the filter is reset.

_onApply (private)

_onApply: EventEmitter;
  • Type: EventEmitter
  • Description: An event emitter that emits an event when the filter is applied.

onApply (public)

onApply: EventEmitter;
  • Type: EventEmitter
  • Description: Getter method that returns the EventEmitter triggered when the filter is applied.

Methods

reset (public)

reset(): void;
  • Type: void
  • Description: Clears all conditions from the filter and emits the onReset event to notify that the filter has been reset.

Usage

The Filter class provides a flexible and reusable foundation for creating filters in an application. It manages filter metadata, handles filter conditions, and triggers important events when the filter state changes.

Example usage

Suppose you have a column age that you want to filter using comparison operations (>, <, >=, <=). You could create a subclass of the Filter class to implement this specific functionality (see here).

class AgeFilter extends Filter<number, ComparisonOperation> {
  // Custom filter logic can go here
}

Once created, the AgeFilter could be instantiated as follows:

const ageFilter = new AgeFilter("age", "Age Filter");
ageFilter.reset(); // Clears conditions and emits the reset event.

Clone this wiki locally