@timlassiter11/yatl
    Preparing search index...

    Class DataTable<T>

    Represents a dynamic and interactive table with features like sorting, searching, filtering, column resizing, column rearranging, and virtual scrolling.

    type DataType = {id: number, name: string, age: number};

    const dataTable = new DataTable<DataType>('#myTable', [
    { field: 'id', title: 'ID', sortable: true },
    { field: 'name', title: 'Name', searchable: true },
    { field: 'age', title: 'Age', sortable: true, valueFormatter: (value) => `${value} years` }
    ], {
    data: [
    { id: 1, name: 'Alice', age: 30 },
    { id: 2, name: 'Bob', age: 24 },
    ],
    virtualScroll: true,
    resizable: true,
    rearrangeable: true,
    });

    Type Parameters

    • T

    Hierarchy

    • EventTarget
      • DataTable
    Index

    Constructors

    • Initializes a new instance of the DataTable.

      Type Parameters

      • T

      Parameters

      • table: string | HTMLTableElement

        The HTMLTableElement or a CSS selector string for the table.

      • columns: ColumnInitOptions<T>[]

        List of ColumnOptions for the table.

      • options: TableInitOptions<T> = {}

        Optional configuration options for the DataTable.

      Returns DataTable<T>

      If the table selector does not find an element.

      If the provided table element is not an HTMLTableElement or if columns option is not an array.

    Accessors

    • get table(): HTMLTableElement

      Gets the underlying HTMLTableElement managed by this DataTable instance.

      Returns HTMLTableElement

    Methods

    • Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.

      The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.

      When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.

      When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.

      When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.

      If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.

      The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.

      MDN Reference

      Type Parameters

      Parameters

      Returns void

    • Parameters

      • type: string
      • listener: EventListenerOrEventListenerObject
      • Optionaloptions: boolean | AddEventListenerOptions

      Returns void

    • Deletes a row at a specific original index from the table.

      Parameters

      • index: number

        The original index of the row to delete.

      Returns void

    • Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.

      MDN Reference

      Parameters

      • event: Event

      Returns boolean

    • Export the current visible table data to a CSV file.

      Parameters

      • filename: string

        The name of the file to save.

      • all: boolean = false

        If true, exports all original data (ignoring filters). If false (default), exports only the currently visible (filtered and sorted) rows.

      Returns void

    • Applies filters to the table rows. Filters can be an object where keys are field names and values are the criteria to filter by, or a callback function that receives a row and its index and returns true if the row should be included.

      Parameters

      • Optionalfilters: null | FilterCallback<T> | Partial<{ [K in string]: any }>

        An object defining field-based filters or a custom filter callback function.

      Returns void

      If filters is not an object or a function.

    • Finds the original index of the first row where the specified field matches the given value. This searches through the original, unfiltered dataset.

      Parameters

      • field: string

        The field name within the row data to search.

      • value: any

        The value to match against the field's content.

      Returns number

      The original index of the found row, or -1 if no match is found.

      const index = dataTable.indexOf('id', 12345);
      if (index >= 0) {
      dataTable.updateRow({description: "Updated description"}, index);
      }
    • Refreshes the table display. This re-applies filters, sorting, and updates headers and rows.

      Returns void

    • Removes the event listener in target's event listener list with the same type, callback, and options.

      MDN Reference

      Type Parameters

      Parameters

      Returns void

    • Parameters

      • type: string
      • listener: EventListenerOrEventListenerObject
      • Optionaloptions: boolean | EventListenerOptions

      Returns void

    • Scrolls the table to bring the row at the specified original index into view.

      Parameters

      • index: number

        The original index of the row (from the initial dataset).

      Returns void

    • Filters rows based on a search query. The search is performed on columns marked as searchable and extraSearchFields.

      Parameters

      • Optionalquery: null | string | RegExp

        The search term (string) or a regular expression. An empty string clears the search.

      Returns void

    • Sets the display order of the columns in the table.

      Parameters

      • fields: NestedKeyOf<T>[]

        An array of field names representing the new order of columns. Columns not included in the array will be placed at the end.

      Returns void

      If fields is not an array.

    • Sets the visibility of a specified column.

      Parameters

      • colName: NestedKeyOf<T>

        The field name of the column.

      • visisble: boolean

        true to show the column, false to hide it.

      Returns void

    • Displays a message in the table body, typically used for "no data" or "no results" states. The message is shown in a single row spanning all columns.

      Parameters

      • text: string

        The text or HTML message to display.

      • ...classes: string[]

        A string or array of strings for CSS classes to apply to the message row.

      Returns void

    • Sorts the table by a specified column and order. If order is null, the sort on this column is removed.

      Parameters

      • colName: NestedKeyOf<T>

        The field name of the column to sort by.

      • order: null | SortOrder

        The sort order: 'asc', 'desc', or null to remove sorting for this column.

      Returns void

    • Updates the data of a row at a specific original index.

      Parameters

      • index: number

        The original index of the row to update.

      • data: Partial<T>

        An object containing the new data to assign to the row. Existing fields will be updated, and new fields will be added.

      Returns void

      const index = dataTable.indexOf('id', 12345);
      if (index >= 0) {
      dataTable.updateRow(index, {description: "Updated description"});
      }
    • Executes a callback function without triggering table updates (like re-rendering or event dispatches) until the callback has completed. This is useful for batching multiple operations.

      Parameters

      • callback: (dataTable: DataTable<T>) => void

        A function to execute. It receives the DataTable instance as its argument.

      Returns void

      dataTable.withoutUpdates(dt => { dt.sort('name', 'asc'); dt.filter({ age: '>30' }); });