Frappe Datatable

repository·master·Indexed 23 days ago

https://github.com/frappe/datatable

A modern, interactive, and high-performance datatable library for the web designed to handle large datasets. It features inline editing, custom formatters, column reordering, resizing, sorting, and tree-structured organization. The library provides a DataTable class for rendering tables via CSS selectors or DOM elements, with support for custom translations, event management, and dynamic row heights.

Tokens
1.3K
Snippets
2
Records
12
Agent score
80%

What's inside frappe-datatable

  1. Key features of Frappe Datatable

    master

    Frappe Datatable provides high-performance rendering for large datasets with the following core capabilities:

    Cell Features

    • Inline editing and custom formatters.
    • Mouse selection and keyboard navigation.
    • Ability to copy cell content.

    Column Features

    • Reordering, resizing, and sorting.
    • Hiding/removing columns.
    • Adding custom actions.

    Row Features

    • Row selection.
    • Tree-structured organization.
    • Inline filters.
    • Support for dynamic row heights to handle large datasets efficiently.
  2. Install frappe-datatable

    master

    You can install frappe-datatable using npm or yarn.

    Important: You must also install sortablejs as a dependency for the library to function correctly.

    yarn add frappe-datatable
    # or
    npm install frappe-datatable
  3. Initialize a DataTable instance

    master

    To render a datatable, create a new DataTable instance by passing a CSS selector for the container element and an options object. The options object requires columns (an array of column names) and data (an array of arrays representing the rows).

    const datatable = new DataTable('#datatable', {
      columns: [ 'First Name', 'Last Name', 'Position' ],
      data: [
        [ 'Don', 'Joe', 'Designer' ],
        [ 'Mary', 'Jane', 'Software Developer' ]
      ]
    });
  4. Configure DataTable translations

    master

    You can customize the language and specific text strings used in the DataTable via the constructor options.

    Options:

    • language: A string representing the language code (defaults to 'en').
    • translations: An object containing key-value pairs for custom translations.

    Example:

    const table = new DataTable('#container', {
        language: 'fr',
        translations: {
            'Save Sorting': 'Sauvegarder le tri'
        }
    });
  5. Manipulate columns and sorting

    master

    The DataTable class provides methods to modify the table structure and sorting state:

    • sortColumn(colIndex, sortOrder): Manually trigger sorting on a specific column.
    • removeColumn(colIndex): Removes a column from the table.
    • setColumnSticky(colIndex, sticky): Sets whether a column is sticky (fixed).
    • saveSorting(colIndex, nextSortOrder): Saves the current sorting state for a column (useful when options.saveSorting is enabled).
  6. Manage DataTable events

    master

    The DataTable supports both user-defined events and internal event listeners.

    User Events

    User events are defined in the options.events object during initialization. When an event is triggered via fireEvent(eventName, ...args), the corresponding handler in your options object is executed.

    Internal Event Listeners

    You can register custom listeners for specific events using the .on(event, handler) method. This is useful for intercepting internal lifecycle events or custom events triggered by the table.

    Example:

    // Registering an internal listener
    table.on('onSortColumn', (column) => {
        console.log('Column sorted:', column);
    });
  7. Access DataTable data and columns

    master

    Use the following methods to retrieve data and structural information from the table:

    • getColumns(): Returns the current column definitions.
    • getRows(): Returns the current row data.
    • getColumn(colIndex): Returns the column definition at the specified index.
    • getCell(colIndex, rowIndex): Returns the cell data at the specified column and row index.
  8. Initialize a DataTable

    master

    To create a new DataTable, instantiate the DataTable class by passing a DOM element or a CSS selector as the first argument, and an options object as the second. The constructor handles DOM preparation, component initialization, and initial data rendering if data is provided in the options.

    Arguments:

    • wrapper: An HTMLElement or a string (CSS selector) representing the container for the table.
    • options: An object containing configuration such as data, columns, language, translations, and events.

    Note: If options.data is present during initialization, the table will automatically call refresh() and apply default or saved sorting.

  9. Import the DataTable class

    master
    The DataTable class is the primary entrypoint for the library. You can import it directly from the package to initialize a new datatable instance. The library also exports a __version__ property on the DataTable object which reflects the current version of the package.
  10. Refresh DataTable with new data

    master

    Use the refresh(data, columns) method to update the DataTable with a new set of data and column definitions. This method initializes the DataManager, re-renders the table, and recalculates dimensions.

    Arguments:

    • data: The new array of row data.
    • columns: The new array of column definitions.
  11. Retrieve available translations with getTranslations()

    master
    The getTranslations function returns an object containing the translation dictionaries for all supported languages. The keys in the returned object correspond to the language codes (e.g., en, de, fr, it), and the values are the respective translation data objects imported from JSON files.