Skip to content

Operation filter

Job Haast edited this page Sep 20, 2024 · 2 revisions

Operation filter

Overview

The AbstractOperationFilter class is an abstract class that represents a filter supporting operations such as comparison, equality, or "like" operations. This class restricts the filter to only one condition per instance, ensuring that filters remain focused on a single operation at a time.

Class: AbstractOperationFilter

This class is generic and uses two type parameters:

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

Constructor

constructor(column: string, label: string, conditions?: Condition[])

Parameters:

  • column: string: The name of the column or field that the filter applies to.
  • label: string: The label or display name for the filter.
  • conditions?: Condition[]: An optional array of conditions to initialize the filter with. This array must contain at most one condition.

Throws:

If more than one condition is provided, the constructor throws a RangeError because this filter supports only a single condition.

Methods

apply (public)

apply(value: T, operation: R): void;
  • Type: void
  • Description: Applies a new condition to the filter. This method sets a new condition using the provided value and operation, overwriting any existing condition, and emits the onApply event.
  • Parameters:
    • value: T: The value to be used for filtering.
    • operation: R: The operation to apply (e.g., comparison, equality, or "like" match).

Usage

The AbstractOperationFilter class is designed to allow filtering based on a single condition using a variety of operations such as comparison or equality. It is especially useful in cases where only one filtering condition is necessary at a time.

Example Usage

Suppose you want to create a filter for selecting users based on their name using a "like" operation (see here):

class NameFilter extends AbstractOperationFilter {
  // Additional custom methods can be added here
}

const nameFilter = new NameFilter("name", "Name Filter");
nameFilter.apply("John", LikeOperation.Like); // Filters for names that are similar to "John"

Clone this wiki locally