Skip to content

Number range filter

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

Number range filter

Overview

The NumberRangeFilter class extends the AbstractRangeFilter to handle filtering based on numeric ranges. It allows specifying one or two conditions for numeric comparison operations, such as greater than and less than. This filter is ideal for handling ranges like prices, ages, or other numeric values.

Class: NumberRangeFilter

This class specializes in filtering numeric ranges, extending the generic AbstractRangeFilter to apply filtering logic for number values.

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 of the filter.
  • conditions?: Condition[]: An optional array of conditions to initialize the filter with. The array may contain one or two conditions, representing the range limits.

Remarks:

  • The filter can only accept one or two conditions at a time. If more than two conditions are provided, the parent class will throw a RangeError.

Properties

_conditions (protected)

protected _conditions: Condition[] = [];
  • Type: Condition[]
  • Description: The internal array of conditions representing the filter's range boundaries.

onApply (public)

onApply: EventEmitter;
  • Type: EventEmitter
  • Description: Emits an event when the filter is applied or updated with new conditions.

Methods

apply (public)

apply(value1: number, operation1: ComparisonOperation, value2: number, operation2: ComparisonOperation): void;
  • Type: void
  • Description: Applies two numeric conditions to the filter to define a range. The method creates conditions using the provided numeric values and their respective comparison operations, updating the filter accordingly.
  • Parameters:
    • value1: number: The first numeric value representing one boundary of the range (either start or end).
    • operation1: ComparisonOperation: The comparison operation for value1 (e.g., greater than, less than).
    • value2: number: The second numeric value representing the other boundary of the range.
    • operation2: ComparisonOperation: The comparison operation for value2.

Remarks:

The apply method replaces any existing conditions with the new numeric range conditions and emits an onApply event to signal that the filter has been updated.

Usage

The NumberRangeFilter class is useful for scenarios where you need to filter records based on a specific numeric range. By applying two numeric conditions, you can filter datasets by numerical intervals, such as prices, weights, or scores.

Example Usage

To filter data where the price is between 10 and 100:

const priceRangeFilter = new NumberRangeFilter("price", "Price Range");
priceRangeFilter.apply(
  10,
  ComparisonOperation.GreaterThanOrEqual,
  100,
  ComparisonOperation.LessThanOrEqual
); // Filters records where the price is between 10 and 100.

Clone this wiki locally