Update v4.2 - New components, 10+ framework guides, and quality improvements. Visit Changelog

Tailwind CSS DataTable Plugin API

Headless, unstyled datatable plugin built with JavaScript and TypeScript, including installation, usage, options, methods, events, and selectors.

JavaScript Typescript

Installation

To get started, install DataTable plugin via npm, else you can skip this step if you are already using Preline UI as a package.

  1. Install the plugin

    Install @preline/datatable via npm

    Terminal
                              
                                npm i jquery datatables.net-dt @preline/datatable
                              
                            
  2. Add the plugin CSS

    Use @source to register the plugin's JavaScript path for Tailwind CSS scanning, then @import the plugin's CSS files into your Tailwind CSS file.

    main.css
                              
                                @import "tailwindcss";
                                
                                /* @preline/datatable */
                                /* [!code highlight:3] */
                                @source "../node_modules/@preline/datatable/*.js";
                                @import "./node_modules/@preline/datatable/variants.css";
                                @import "./node_modules/@preline/datatable/theme.css";
    
                                /* [!code highlight:5] */
                                .dt-layout-row:has(.dt-search),
                                .dt-layout-row:has(.dt-length),
                                .dt-layout-row:has(.dt-paging) {
                                  display: none !important;
                                }
                              
                            
  3. Add the plugin JavaScript

    Include the JavaScript <script> that powers the interactive elements near the end of your </body> tag:

    HTML
                              
                            

    Additional Initialization Options

    Use the non-auto entry if you need manual initialization. In this mode, automatic initialization on page load is not included, so the component should be initialized explicitly.

    HTML (non-auto)
                              
                            

    Via bundler

    When using a bundler (Vite, webpack, etc.), import the plugin directly as an ES module. jQuery and DataTable are referenced as globals — expose them on window before importing the plugin.

    JavaScript (auto)
                              
                                import jQuery from "jquery";
                                window.jQuery = jQuery;
                                window.$ = jQuery;
                                import DataTable from "datatables.net";
                                window.DataTable = DataTable;
                                
                                import "@preline/datatable";
                              
                            
    JavaScript (non-auto)
                              
                                import jQuery from "jquery";
                                window.jQuery = jQuery;
                                window.$ = jQuery;
                                import DataTable from "datatables.net";
                                window.DataTable = DataTable;
                                
                                import HSDataTable from "@preline/datatable/non-auto";
                                
                                HSDataTable.autoInit();
                                
                                // Or initialize a specific element manually
                                const el = document.querySelector("#datatable");
                                if (el) new HSDataTable(el);
                              
                            

Example

Here is a basic example of a datatable with advanced features.

Basic usage

Prefer to create your own style? Here is a completely unstylized example.

HTML
                      
                    

CSS

Please add the following CSS to the head. This is necessary because Datatables.net does not allow to add custom markup with options disabled, such as paging: false or searching: false.

HTML
                      
                    

Data Options

Name Description Options Default value
data-hs-datatable Activate a Datatable by specifying on an element. Should be added to the container.
:pageBtnClasses Sets the specified classes to each paging element inside the data-hs-datatable-paging-pages. string -
:rowSelectingOptions.selectAllSelector A valid CSS selector (the element must be a checkbox or radio button), when the element is changed, all elements specified by rowSelectingOptions.individualSelector (or, if not specified, then data-hs-datatable-row-selecting-individual) on the current page will be changed. string [data-hs-datatable-row-selecting-all]
:rowSelectingOptions.individualSelector A valid CSS selector (the element must be a checkbox or radio button), affects the change of the element specified in rowSelectingOptions.selectAllSelector, if not all elements are selected on the current page, then it switches to unchecked state, if vice versa, then in checked. string [data-hs-datatable-row-selecting-individual]

Class Options

Name Description
--exclude-from-ordering A modifier that allows you to disable the ordering functionality in a column. Should be added to the thead column.

Selectors

Name Description
data-hs-datatable-search Defines which element will be responsible for searching in the datatable (must be an input).
data-hs-datatable-page-entities Determines which element will be responsible for switching the number of elements on the page (must be a select, depends on the change event).
data-hs-datatable-paging Defines a paging container, that contains pagination, next and previous buttons.
data-hs-datatable-paging-prev Defines a previous button element.
data-hs-datatable-paging-next Defines a next button element.
data-hs-datatable-row-selecting-all Defines a "Select all" checkbox/radio, if it's not specified in the rowSelectingOptions.selectAllSelector parameter.
data-hs-datatable-row-selecting-individual Defines a checkbox/radio that will be responsible for selecting a specific row if it is not specified in the rowSelectingOptions.individualSelector parameter.
data-hs-datatable-info Defines an info container.
data-hs-datatable-info-from Defines an element that showing start number.
data-hs-datatable-info-to Defines an element that showing end number.
data-hs-datatable-info-length Defines an element that showing recordsTotal number.

Tailwind Modifiers

Name Description
hs-datatable-ordering-asc:* A modifier that allows you to set Tailwind classes to elements inside thead th or thead td when ordering changes to asc.
hs-datatable-ordering-desc:* A modifier that allows you to set Tailwind classes to elements inside thead th or thead td when ordering changes to desc.

Methods

The HSDataTable object is contained within the global window object

Method Description
Public methods
destroy() Destroys the instance, removes generated markup (if any), removes added classes and attributes.
Static methods
HSDataTable.getInstance(target, isInstance) Returns the element associated to the target.
  • target should be a Node or string (valid selector)
  • isInstance boolean. Returns the instance instead of Node if true

Destroy instance.

JavaScript
                      
                        const { element } = HSDataTable.getInstance('#datatable', true);
                        const destroyBtn = document.querySelector('#destroy-btn');

                        destroyBtn.addEventListener('click', () => {
                          element.destroy();
                        });
                      
                    

Events

Datatables.net

Please note that this component requires the use of the Datatables.net plugin. Most events available in the plugin are available in our wrapper.

When datatable redrawn event example.

JavaScript
                      
                        const { element } = HSDataTable.getInstance('#hs-datatable', true);
                        const { dataTable } = element;

                        dataTable.on('draw.dt', () => {
                          console.log('Table redrawn!');
                        });
                      
                    

Ready to use Components

Looking for prebuilt UI components based on the Tailwind CSS? Preline UI packs hundreds of component examples for all your website needs.

Datatables

Explore ready-to-use Tailwind CSS examples built with Preline UI.

View Datatables examples

Datatables

© 2026 Preline Labs.