Tailwind CSS DataTable Plugin API
Headless, unstyled datatable plugin built with JavaScript and TypeScript, including installation, usage, options, methods, events, and selectors.
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.
-
Install the plugin
Install
@preline/datatablevia npmTerminalnpm i jquery datatables.net-dt @preline/datatable -
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; } -
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-autoentry 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.
jQueryandDataTableare referenced as globals — expose them onwindowbefore 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);
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.
Methods
The HSDataTable object is contained within the global window object
Destroy instance.
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.
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