-
Notifications
You must be signed in to change notification settings - Fork 0
Filter
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).
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:ComparisonOperationEqualOperationLikeOperationInOperation
These operations represent different ways of filtering the data (e.g., comparison, equality, pattern matching, inclusion).
constructor(column: string, label: string, conditions?: Condition<T, R>[])-
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.
public readonly column: string;-
Type:
string - Description: The name of the column or field that this filter applies to.
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.
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: Condition < T, R > [];-
Type:
Condition<T, R>[] - Description: Getter method that returns the list of filter conditions.
-
Returns: An array of
Conditioninstances representing the current filter conditions.
_onReset: EventEmitter;-
Type:
EventEmitter - Description: An event emitter that emits an event when the filter is reset.
onReset: EventEmitter;-
Type:
EventEmitter -
Description: Getter method that returns the
EventEmittertriggered when the filter is reset.
_onApply: EventEmitter;-
Type:
EventEmitter - Description: An event emitter that emits an event when the filter is applied.
onApply: EventEmitter;-
Type:
EventEmitter -
Description: Getter method that returns the
EventEmittertriggered when the filter is applied.
reset(): void;-
Type:
void -
Description: Clears all conditions from the filter and emits the
onResetevent to notify that the filter has been reset.
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.
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.