Custom Editor

View as Markdown
For writing a custom editor, you can use the injectColumnEditor inject function.
For any column (or column type - which can then get applied to multiple columns), you can specify a custom editor component to be used for editing the column's value, via the column.components.Editor property.
const columns: InfiniteTablePropColumns<Developer> = {
  id: {
    field: 'id',
    defaultEditable: false,
  },
  firstName: {
    field: 'firstName',
    components: {
      // this is using a custom editor component
      Editor: CustomEditor,
    },
  },
  age: {
    field: 'age',
    type: 'number',
    defaultEditable: false,
  },
  stack: { field: 'stack' },
  currency: { field: 'currency' },
};
The editor component should call injectColumnEditor (in a field initializer or in its constructor), to have access to cell-related information and to confirm, cancel or reject the edit. It returns a plain object: the editor is created when the edit starts, and initialValue is the value the edit starts with. The editor is responsible for focusing its input.
custom-editor.ts
import { Component, ElementRef, viewChild } from '@angular/core';
import type { AfterViewInit } from '@angular/core';
import { injectColumnEditor } from '@infinite-table/infinite-angular';

@Component({
  selector: 'custom-editor',
  template: `
    <div>
      <input
        #input
        style="width: 100%"
        [value]="editor.initialValue"
        (keydown)="onKeydown($event)"
      />
    </div>
  `,
})
export class CustomEditor implements AfterViewInit {
  protected readonly editor = injectColumnEditor();

  private readonly input =
    viewChild.required<ElementRef<HTMLInputElement>>('input');

  ngAfterViewInit() {
    this.input().nativeElement.focus();
  }

  protected onKeydown(event: KeyboardEvent) {
    const { key } = event;
    if (key === 'Enter' || key === 'Tab') {
      this.editor.confirmEdit(this.input().nativeElement.value);
    } else if (key === 'Escape') {
      this.editor.cancelEdit();
    } else {
      event.stopPropagation();
    }
  }
}
Inside any custom editor component, you can use injectColumnCell to get access to the cell-related information. It returns a signal.
#
Using a custom editor
In this example, the salary column is configured with a custom editor component.
open
loading demo…

Using Custom Date Editors#

A common use-case is integrating date editors. In Angular, a date editor is written like the editor above: a component that calls injectColumnEditor and renders a date input. The following example uses the browser's native <input type="date">; the date picker you already use in your project fits in the same way.
#
Using a native date input for editing dates in the DataGrid
Click any cell in the Birth Date column to edit it with the browser's date input: pick a day to confirm the edit, press Escape (or click elsewhere) to cancel it.
open
loading demo…