TableFilter JavaScript Library

repository·master·Indexed 43 days ago

https://github.com/koalyptus/tablefilter

A JavaScript library (version 0.7.3) that transforms standard HTML tables into advanced, filterable grid components. It provides features such as column filtering, sorting, pagination, and row manipulation. Supports various filter input types (input, select, multiple, checklist) and data types (string, number, formatted-number, date, ipaddress).

Tokens
2.3K
Snippets
12
Records
12
Agent score
56%

What's inside TableFilter

  1. Use TableFilter via distribution scripts

    master

    If you are not using a module system, you can include the distribution scripts directly in your HTML pages. You can either reference the files within your node_modules folder or manually copy the tablefilter directory from dist into your project and reference it locally.

    <!-- Referencing node_modules -->
    <script src="path_to/node_modules/tablefilter/dist/tablefilter/tablefilter.js"></script>
    
    <!-- Referencing manually placed scripts -->
    <script src="path/to/my/scripts/tablefilter/tablefilter.js"></script>
  2. Install TableFilter via npm

    master

    You can install TableFilter using npm. To install the current stable version, use the standard install command. To access upcoming features, you can install from the next release channel.

    # Install stable version
    npm install tablefilter --save-dev
    
    # Install from the next release channel
    npm install tablefilter@next --save-dev
  3. Import TableFilter as a module

    master

    If you are using a module bundler, you can import TableFilter using ES2015 modules or CommonJS/AMD modules.

    // ES2015 modules
    import TableFilter from 'tablefilter';
    
    // CommonJS or AMD modules
    var TableFilter = require('tablefilter');
  4. Initialize TableFilter on an HTML table

    master

    To use TableFilter, instantiate the TableFilter class by passing the target HTML table element and a configuration object.

    Important: You must define the base_path property in the configuration object to reflect the path to the TableFilter scripts. If base_path is omitted, it defaults to the /tablefilter directory relative to your page.

    <script>
    var tf = new TableFilter(document.querySelector('.my-table'), {
        base_path: 'path/to/my/scripts/tablefilter/'
    });
    tf.init();
    </script>
  5. TableFilter API Reference

    master

    The core API for interacting with TableFilter involves the TableFilter constructor and the .init() method.

    // Constructor
    // new TableFilter(tableElement, configurationObject)
    
    // Configuration Object Keys
    // base_path: string (The path to the TableFilter scripts)
    
    // Methods
    // tf.init(): Initializes the filter functionality on the table
  6. Configure Toolbar CSS classes and target container

    master

    When configuring the toolbar object in your TableFilter settings, you can customize the DOM structure and styling of the toolbar using the following keys:

    • container_css_class: The CSS class for the main toolbar container. Defaults to 'inf'.
    • left_cont_css_class: The CSS class for the left-side inner container. Defaults to 'ldiv'.
    • right_cont_css_class: The CSS class for the right-side inner container. Defaults to 'rdiv'.
    • center_cont_css_class: The CSS class for the middle inner container. Defaults to 'mdiv'.
    • target_id: The ID of a custom DOM element where the toolbar should be placed. If not provided, the toolbar defaults to being placed in a <caption> element just above the table.

    If gridLayout is enabled in TableFilter, the toolbar will be appended to the grid layout's main container instead of using the default <caption> placement.

    // Example TableFilter configuration
    const settings = {
      toolbar: {
        container_css_class: 'my-toolbar-container',
        left_cont_css_class: 'my-left-section',
        right_cont_css_class: 'my-right-section',
        center_cont_css_class: 'my-center-section',
        target_id: 'custom-toolbar-location'
      }
    };
  7. Initialize TableFilter using the TableFilter class

    master

    The main entry point for the library is the TableFilter class. You can import it from the root module to initialize filtering capabilities on an HTML table. While the specific constructor arguments are defined in the distribution files, the entry point allows you to require the library and access the TableFilter constructor directly.

    const TableFilter = require('tablefilter');
    
    // Initialize TableFilter on a table element
    const tf = new TableFilter({
      // configuration options go here
    });
  8. Get a toolbar container by position

    master

    The Toolbar.container(position, el) method allows you to retrieve one of the three internal container elements within the toolbar. This is useful if you want to manually inject custom DOM elements into specific parts of the toolbar.

    Parameters

    • position (String): The desired position. Use one of the exported constants: LEFT, CENTER, or RIGHT. Defaults to RIGHT.
    • el (DOMElement, optional): An optional DOM element to append directly to the requested container.

    Return Value

    • Returns the DOMElement corresponding to the requested position.
    import { LEFT, CENTER, RIGHT } from 'tablefilter';
    
    // Assuming 'toolbar' is an instance of the Toolbar class
    const leftContainer = toolbar.container(LEFT);
    const centerContainer = toolbar.container(CENTER);
    
    // You can also append an element directly during the call
    const myDiv = document.createElement('div');
    myDiv.textContent = 'Hello Toolbar';
    toolbar.container(RIGHT, myDiv);
  9. Configure data types for filtering

    master

    TableFilter uses data types to understand how to process and compare values in your table cells. Supported data types include:

    • string: Standard text comparison.
    • number: Numeric comparison.
    • formatted-number: Numbers that may contain formatting (like currency or separators).
    • date: Date-based comparison.
    • ipaddress: IP address comparison.
    // Example of specifying a data type in column configuration
    const config = {
      columns: [
        { selector: 'th:nth-child(4)', type: 'date' },
        { selector: 'th:nth-child(5)', type: 'number' }
      ]
    };
  10. Toolbar position constants

    master

    The Toolbar module exports three constants representing the available alignment positions within the toolbar container. Use these when calling the container() method to ensure type safety and avoid string errors.

    • LEFT: 'left'
    • CENTER: 'center'
    • RIGHT: 'right'
    export const LEFT = 'left';
    export const RIGHT = 'right';
    export const CENTER = 'center';
  11. Configure filter input types

    master

    When configuring TableFilter, you can specify the type of input element used for filtering. The available filter types are:

    • input: Standard text input.
    • select: A single-selection dropdown.
    • multiple: A multiple-selection dropdown.
    • checklist: A list of checkboxes.
    • none: No filter input is provided for the column.
    // Example of using the constants for configuration
    import { INPUT, SELECT, CHECKLIST } from './path/to/src/const.js';
    
    const config = {
      columns: [
        { selector: 'th:nth-child(1)', filterType: INPUT },
        { selector: 'th:nth-child(2)', filterType: SELECT },
        { selector: 'th:nth-child(3)', filterType: CHECKLIST }
      ]
    };