Providing a Custom Filter Editor

View as Markdown
Almost certainly, our current string and number filters are not enough for you. You will definitely need to write your custom filter editor.
Fortunately, doing this is straightforward - it involves writing a component that uses the injectColumnFilterEditor inject function.
The next snippet shows our implementation of the number filter editor:
@Component({
  // the host element is the input
  selector: 'input[numberFilterEditor]',
  host: {
    type: 'number',
    '[attr.aria-label]': 'editor.ariaLabel()',
    '[disabled]': '!!editor.disabled()',
    '[value]': 'value()',
    '[class]': 'editor.className',
    '(input)': 'onInput($event)',
  },
  template: '',
})
export class NumberFilterEditor {
  protected readonly editor = injectColumnFilterEditor();

  protected readonly value = computed(() => this.editor.value() ?? '');

  protected onInput(event: Event) {
    const target = event.target as HTMLInputElement;
    const value = isNaN(target.valueAsNumber)
      ? target.value
      : target.valueAsNumber;
    this.editor.setValue(value);
  }
}
This NumberFilterEditor is configured in the components.FilterEditor property for the number filter type.
If you want to import the NumberFilterEditor, you can do so with the following code:
import {
  NumberFilterEditor,
  StringFilterEditor,
} from '@infinite-table/infinite-angular';
As an exercise, let's write a custom filter editor that shows a checkbox and uses that to filter the values.
First step is to define the bool filter type:
Defining the bool filter type with one emptyValue
// <infinite-data-source [filterTypes]="filterTypes" />
const filterTypes: DataSourcePropFilterTypes<Developer> = {
  bool: {
    label: 'Boolean',
    defaultOperator: 'eq',
    // when the filter checkbox is indeterminate state, that's mapped to `null`
    emptyValues: [null],
    operators: [
      // operators will come here
    ],
  },
};
Note in the code above, we have emptyValues: [null] - so when the filter checkbox is in indeterminate state, it should show all the rows.
Now it's time to define the operators - more exactly, just one operator, eq:
Defining the eq operator
// <infinite-data-source [filterTypes]="filterTypes" />
const filterTypes: DataSourcePropFilterTypes<Developer> = {
  bool: {
    defaultOperator: 'eq',
    emptyValues: [null],
    operators: [
      {
        name: 'eq',
        label: 'Equals',
        fn: ({ currentValue, filterValue }) => currentValue === filterValue,
      },
    ],
  },
};
The last part of the bool filter type will be to specify the FilterEditor component - this can be either specified as part of the filter type or as part of the operator definition (each operator can override the components.FilterEditor).
Specifying the FilterEditor component
// <infinite-data-source [filterTypes]="filterTypes" />
const filterTypes: DataSourcePropFilterTypes<Developer> = {
  bool: {
    defaultOperator: 'eq',
    emptyValues: [null],
    components: {
      FilterEditor: () => renderComponent(BoolFilterEditor),
      FilterOperatorSwitch: () => null,
    },
    operators: [
      {
        name: 'eq',
        label: 'Equals',
        fn: ({ currentValue, filterValue }) => currentValue === filterValue,
      },
    ],
  },
};
components.FilterEditor is a function that returns what to render - for a component, renderComponent(BoolFilterEditor). The component is created with the injector of the filter cell, so it can use injectColumnFilterEditor.
Now it's time to write the actual BoolFilterEditor that the bool filter type is using:
BoolFilterEditor
import { Component } from '@angular/core';
import {
  InfiniteCheckBox,
  injectColumnFilterEditor,
} from '@infinite-table/infinite-angular';

@Component({
  selector: 'div[boolFilterEditor]',
  imports: [InfiniteCheckBox],
  host: {
    '[class]': 'editor.className',
    style: 'text-align: center',
  },
  template: `
    <input
      infiniteCheckBox
      [checked]="editor.value()"
      (checkedChange)="onChange($event)"
    />
  `,
})
export class BoolFilterEditor {
  protected readonly editor = injectColumnFilterEditor<Developer>();

  protected onChange(newValue: boolean | null) {
    const value = this.editor.value();
    if (value === true) {
      // after the value was true, make it go to indeterminate state
      newValue = null;
    }
    if (value === null) {
      // from indeterminate, goto false
      newValue = false;
    }
    this.editor.setValue(newValue);
  }
}
In the snippet above, note how we're using the injectColumnFilterEditor inject function to get the current value of the filter and also to retrieve the setValue function that we need to call when we want to update filtering.
value is a signal: read it in the template (editor.value()), so the editor is updated when the filter changes.
#
Writing a bool filter type with a custom filter editor
The canDesign column is using a custom bool filter type with a custom filter editor.
open
loading demo…