Skip to content

Select filter

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

Select filter

Overview

The AbstractSelectFilter class is an abstract base class for implementing a select filter that operates with predefined options such as single or multi-select. This filter allows the user to select from options and applies equality or inclusion operations to filter data.

Class: AbstractSelectFilter

The class is generic and uses three type parameters:

  • T: The data type that the options operate on (e.g., string, number).
  • R: The type of operations that the filter supports, either:
    • EqualOperation.Equal
    • InOperation
  • V: The type of option used by the filter, which can either be:
    • SelectOption
    • MultiSelectOption

Constructor

constructor(column: string, label: string, options: V[], 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.
  • options: V[]: The list of selectable options in the filter.
  • conditions?: Condition[]: An optional array of initial conditions for the filter.

Properties

_options (protected)

protected _options: V[];
  • Type: V[]
  • Description: A protected array of selectable options in this filter.

options (public)

get options(): V[];
  • Type: V[]
  • Description: Getter method that retrieves the available options for the filter.
  • Returns: An array of options that can be selected in the filter.

Methods

selectOption (public abstract)

abstract selectOption(value: T): void;
  • Type: void
  • Description: Abstract method for selecting an option within the filter. Subclasses must implement this method to define how an option is selected.
  • Parameters:
    • value: T: The value to be selected from the options.

apply (public)

apply(): void;
  • Type: void
  • Description: Applies the filter and emits the filter's onApply event. This method signals that the filter has been applied.
  • Remarks: It should be called after selecting or modifying options, but it does not modify any conditions.

Usage

The AbstractSelectFilter class provides a foundation for creating select-based filters where the user can choose from predefined options. Subclasses of this class must implement the selectOption method to handle option selection logic.

Example Usage

Assume you want to create a select filter for filtering a dataset based on a list of user roles. You could create a subclass of the AbstractSelectFilter class to implement this functionality (see here).

class RoleSelectFilter extends AbstractSelectFilter<
  T,
  EqualOperation.Equal,
  SelectOption<T>
> {
  public selectOption(value: string): void {
    // Custom selection logic
  }
}

Once created, the RoleSelectFilter can be instantiated and used as follows:

const roleOptions = [
  new SelectOption("Admin", "admin"),
  new SelectOption("User", "user"),
  new SelectOption("Guest", "guest"),
];

const roleFilter = new RoleSelectFilter("role", "User Role", roleOptions);
roleFilter.selectOption("Admin");
roleFilter.apply(); // Triggers the onApply event

Clone this wiki locally