Third-Party Plugins
Enhance HTML tables with Tailwind CSS Datatables, featuring search, paging, and sorting. Improve user experience with efficient data navigation and management.
Note that this component requires the use of the third-party DataTables.net and jQuery libraries.
Note that this component requires the use of Preline UI's Datatables plugin, else you can skip this message if you are already using Preline UI as a package.
Please note that Datatables may not be the ideal library for modern JavaScript frameworks. If you're looking to integrate with React, Vue, Angular, or similar frameworks, you might want to consider alternative libraries such as TanStack Tables
Install jquery
via npm
npm i jquery
Install datatables.net-dt
via npm
npm i datatables.net-dt
Add CSS for hiding some default elements:
.dt-layout-row:has(.dt-search),
.dt-layout-row:has(.dt-length),
.dt-layout-row:has(.dt-paging) {
display: none !important;
}
Include the JavaScript <script>
near the end of your </body>
tag:
cmd+a
in text fields. By default, Datatable blocks this functionality.
Include the JavaScript after initializing the Datatables:
window.addEventListener('load', () => {
...
const inputs = document.querySelectorAll('.dt-container thead input');
inputs.forEach((input) => {
input.addEventListener('keydown', function (evt) {
if ((evt.metaKey || evt.ctrlKey) && evt.key === 'a') this.select();
});
});
});
Using the most basic table markup, here's how tables look.
All the examples are responsive - tables allow tables to be scrolled horizontally with ease.
Name
|
Age
|
Address
|
Action |
---|---|---|---|
Christina Bersh | 45 | 4222 River Rd, Columbus | |
David Harrison | 27 | 2952 S Peoria Ave, Tulsa | |
Anne Richard | 31 | 255 Dock Ln, New Tazewell | |
Samia Kartoon | 45 | 4970 Park Ave W, Ohio | |
David Harrison | 27 | 4222 River Rd, Columbus | |
Brian Halligan | 31 | 2952 S Peoria Ave, Tulsa | |
Andy Clerk | 45 | 1818 H St NW, Washington | |
Bart Simpson | 27 | 3 Grace Dr, New Mexico | |
Camila Welters | 45 | 4531 W Saile Dr, North Dakota | |
Oliver Schevich | 27 | 2126 N Eagle, Meridian, Illinois | |
Inna Ivy | 31 | 3817 Beryl Rd, Nebraska | |
Jessica Williams | 27 | 4807 3rd Ave, New Hampshire | |
James Collins | 31 | Melbourne No. 1 Lake Park | |
Costa Quinn | 27 | New York No. 1 Lake Park | |
Jim Green | 27 | LA No. 1 Lake Park | |
Joe Black | 31 | Sidney No. 1 Lake Park | |
Isabella Cherry | 27 | 4222 River Rd, Columbus | |
Alex Tacker | 31 | 2952 S Peoria Ave, Tulsa | |
Endy Ruiz | 45 | 1818 H St NW, Washington | |
Jack Li | 27 | 3 Grace Dr, New Mexico |
Tbody makes a scroll.
Name
|
Age
|
Address
|
Action |
---|---|---|---|
Christina Bersh | 45 | 4222 River Rd, Columbus | |
David Harrison | 27 | 2952 S Peoria Ave, Tulsa | |
Anne Richard | 31 | 255 Dock Ln, New Tazewell | |
Samia Kartoon | 45 | 4970 Park Ave W, Ohio | |
David Harrison | 27 | 4222 River Rd, Columbus | |
Brian Halligan | 31 | 2952 S Peoria Ave, Tulsa | |
Andy Clerk | 45 | 1818 H St NW, Washington | |
Bart Simpson | 27 | 3 Grace Dr, New Mexico | |
Camila Welters | 45 | 4531 W Saile Dr, North Dakota | |
Oliver Schevich | 27 | 2126 N Eagle, Meridian, Illinois | |
Inna Ivy | 31 | 3817 Beryl Rd, Nebraska | |
Jessica Williams | 27 | 4807 3rd Ave, New Hampshire | |
James Collins | 31 | Melbourne No. 1 Lake Park | |
Costa Quinn | 27 | New York No. 1 Lake Park | |
Jim Green | 27 | LA No. 1 Lake Park | |
Joe Black | 31 | Sidney No. 1 Lake Park | |
Isabella Cherry | 27 | 4222 River Rd, Columbus | |
Alex Tacker | 31 | 2952 S Peoria Ave, Tulsa | |
Endy Ruiz | 45 | 1818 H St NW, Washington | |
Jack Li | 27 | 3 Grace Dr, New Mexico |
Rows can be selectable by making first column as a selectable column.
|
Name
|
Age
|
Address
|
Action |
---|---|---|---|---|
|
Christina Bersh | 45 | 4222 River Rd, Columbus | |
|
David Harrison | 27 | 2952 S Peoria Ave, Tulsa | |
|
Anne Richard | 31 | 255 Dock Ln, New Tazewell | |
|
Samia Kartoon | 45 | 4970 Park Ave W, Ohio | |
|
David Harrison | 27 | 4222 River Rd, Columbus | |
|
Brian Halligan | 31 | 2952 S Peoria Ave, Tulsa | |
|
Andy Clerk | 45 | 1818 H St NW, Washington | |
|
Bart Simpson | 27 | 3 Grace Dr, New Mexico | |
|
Camila Welters | 45 | 4531 W Saile Dr, North Dakota | |
|
Oliver Schevich | 27 | 2126 N Eagle, Meridian, Illinois | |
|
Inna Ivy | 31 | 3817 Beryl Rd, Nebraska | |
|
Jessica Williams | 27 | 4807 3rd Ave, New Hampshire | |
|
James Collins | 31 | Melbourne No. 1 Lake Park | |
|
Costa Quinn | 27 | New York No. 1 Lake Park | |
|
Jim Green | 27 | LA No. 1 Lake Park | |
|
Joe Black | 31 | Sidney No. 1 Lake Park | |
|
Isabella Cherry | 27 | 4222 River Rd, Columbus | |
|
Alex Tacker | 31 | 2952 S Peoria Ave, Tulsa | |
|
Endy Ruiz | 45 | 1818 H St NW, Washington | |
|
Jack Li | 27 | 3 Grace Dr, New Mexico |
Search is used for making dropdown items searchable.
|
Name
|
Age
|
Address
|
Action |
---|---|---|---|---|
|
Christina Bersh | 45 | 4222 River Rd, Columbus | |
|
David Harrison | 27 | 2952 S Peoria Ave, Tulsa | |
|
Anne Richard | 31 | 255 Dock Ln, New Tazewell | |
|
Samia Kartoon | 45 | 4970 Park Ave W, Ohio | |
|
David Harrison | 27 | 4222 River Rd, Columbus | |
|
Brian Halligan | 31 | 2952 S Peoria Ave, Tulsa | |
|
Andy Clerk | 45 | 1818 H St NW, Washington | |
|
Bart Simpson | 27 | 3 Grace Dr, New Mexico | |
|
Camila Welters | 45 | 4531 W Saile Dr, North Dakota | |
|
Oliver Schevich | 27 | 2126 N Eagle, Meridian, Illinois | |
|
Inna Ivy | 31 | 3817 Beryl Rd, Nebraska | |
|
Jessica Williams | 27 | 4807 3rd Ave, New Hampshire | |
|
James Collins | 31 | Melbourne No. 1 Lake Park | |
|
Costa Quinn | 27 | New York No. 1 Lake Park | |
|
Jim Green | 27 | LA No. 1 Lake Park | |
|
Joe Black | 31 | Sidney No. 1 Lake Park | |
|
Isabella Cherry | 27 | 4222 River Rd, Columbus | |
|
Alex Tacker | 31 | 2952 S Peoria Ave, Tulsa | |
|
Endy Ruiz | 45 | 1818 H St NW, Washington | |
|
Jack Li | 27 | 3 Grace Dr, New Mexico |
This example shows how to filter a DataTable.
|
Name
|
Age
|
Address
|
Action |
---|---|---|---|---|
|
Christina Bersh | 45 | 4222 River Rd, Columbus | |
|
David Harrison | 27 | 2952 S Peoria Ave, Tulsa | |
|
Anne Richard | 31 | 255 Dock Ln, New Tazewell | |
|
Samia Kartoon | 45 | 4970 Park Ave W, Ohio | |
|
David Harrison | 27 | 4222 River Rd, Columbus | |
|
Brian Halligan | 31 | 2952 S Peoria Ave, Tulsa | |
|
Andy Clerk | 45 | 1818 H St NW, Washington | |
|
Bart Simpson | 27 | 3 Grace Dr, New Mexico | |
|
Camila Welters | 45 | 4531 W Saile Dr, North Dakota | |
|
Oliver Schevich | 27 | 2126 N Eagle, Meridian, Illinois | |
|
Inna Ivy | 31 | 3817 Beryl Rd, Nebraska | |
|
Jessica Williams | 27 | 4807 3rd Ave, New Hampshire | |
|
James Collins | 31 | Melbourne No. 1 Lake Park | |
|
Costa Quinn | 27 | New York No. 1 Lake Park | |
|
Jim Green | 27 | LA No. 1 Lake Park | |
|
Joe Black | 31 | Sidney No. 1 Lake Park | |
|
Isabella Cherry | 27 | 4222 River Rd, Columbus | |
|
Alex Tacker | 31 | 2952 S Peoria Ave, Tulsa | |
|
Endy Ruiz | 45 | 1818 H St NW, Washington | |
|
Jack Li | 27 | 3 Grace Dr, New Mexico |
(function () {
const minEl = document.querySelector('#hs-input-number-min-age');
const maxEl = document.querySelector('#hs-input-number-max-age');
const { dataTable } = new HSDataTable('#hs-datatable-filter');
dataTable.search.fixed('range', function (searchStr, data, index) {
const min = parseInt(minEl.value, 10);
const max = parseInt(maxEl.value, 10);
const age = parseFloat(data[2]) || 0;
if (
(isNaN(min) && isNaN(max)) ||
(isNaN(min) && age <= max) ||
(min <= age && isNaN(max)) ||
(min <= age && age <= max)
) return true;
return false;
});
minEl.addEventListener('input', () => dataTable.draw());
maxEl.addEventListener('input', () => dataTable.draw());
})();
This example shows how to filter a DataTable column.
|
|
Age
|
Address
|
Action |
---|---|---|---|---|
|
Christina Bersh | 45 | 4222 River Rd, Columbus | |
|
David Harrison | 27 | 2952 S Peoria Ave, Tulsa | |
|
Anne Richard | 31 | 255 Dock Ln, New Tazewell | |
|
Samia Kartoon | 45 | 4970 Park Ave W, Ohio | |
|
David Harrison | 27 | 4222 River Rd, Columbus | |
|
Brian Halligan | 31 | 2952 S Peoria Ave, Tulsa | |
|
Andy Clerk | 45 | 1818 H St NW, Washington | |
|
Bart Simpson | 27 | 3 Grace Dr, New Mexico | |
|
Camila Welters | 45 | 4531 W Saile Dr, North Dakota | |
|
Oliver Schevich | 27 | 2126 N Eagle, Meridian, Illinois | |
|
Inna Ivy | 31 | 3817 Beryl Rd, Nebraska | |
|
Jessica Williams | 27 | 4807 3rd Ave, New Hampshire | |
|
James Collins | 31 | Melbourne No. 1 Lake Park | |
|
Costa Quinn | 27 | New York No. 1 Lake Park | |
|
Jim Green | 27 | LA No. 1 Lake Park | |
|
Joe Black | 31 | Sidney No. 1 Lake Park | |
|
Isabella Cherry | 27 | 4222 River Rd, Columbus | |
|
Alex Tacker | 31 | 2952 S Peoria Ave, Tulsa | |
|
Endy Ruiz | 45 | 1818 H St NW, Washington | |
|
Jack Li | 27 | 3 Grace Dr, New Mexico |
(function () {
const personEl = document.querySelector('#hs-select-column-name');
const minEl = document.querySelector('#hs-input-number-column-age-min');
const maxEl = document.querySelector('#hs-input-number-column-age-max');
const addressEl = document.querySelector('#hs-input-text-column-address');
const { dataTable } = new HSDataTable('#hs-datatable-with-column-filter');
dataTable.search.fixed('range', function (searchStr, data, index) {
const person = personEl.value === 'all' ? '' : personEl.value;
const name = data[1] || '';
const min = parseInt(minEl.value, 10);
const max = parseInt(maxEl.value, 10);
const age = parseFloat(data[2]) || 0;
const address = addressEl.value.toLowerCase();
const city = data[3].toLowerCase() || '';
if (
(
person === name ||
person === ''
) &&
(
city.includes(address) ||
address === ''
) &&
(
(isNaN(min) && isNaN(max)) ||
(isNaN(min) && age <= max) ||
(min <= age && isNaN(max)) ||
(min <= age && age <= max)
)
) return true;
return false;
});
personEl.addEventListener('change', () => dataTable.draw());
minEl.addEventListener('input', () => dataTable.draw());
maxEl.addEventListener('input', () => dataTable.draw());
addressEl.addEventListener('input', () => dataTable.draw());
})();
Header is fixed until the end of the table.
Install datatables.net-fixedheader
via npm
npm i datatables.net-fixedheader
Include the JavaScript <script>
near the end of your </body>
tag:
<script src="./node_modules/datatables.net-fixedheader/js/dataTables.fixedHeader.min.js"></script>
Name
|
Age
|
Address
|
Action |
---|---|---|---|
Christina Bersh | 45 | 4222 River Rd, Columbus | |
David Harrison | 27 | 2952 S Peoria Ave, Tulsa | |
Anne Richard | 31 | 255 Dock Ln, New Tazewell | |
Samia Kartoon | 45 | 4970 Park Ave W, Ohio | |
David Harrison | 27 | 4222 River Rd, Columbus | |
Brian Halligan | 31 | 2952 S Peoria Ave, Tulsa | |
Andy Clerk | 45 | 1818 H St NW, Washington | |
Bart Simpson | 27 | 3 Grace Dr, New Mexico | |
Camila Welters | 45 | 4531 W Saile Dr, North Dakota | |
Oliver Schevich | 27 | 2126 N Eagle, Meridian, Illinois | |
Inna Ivy | 31 | 3817 Beryl Rd, Nebraska | |
Jessica Williams | 27 | 4807 3rd Ave, New Hampshire | |
James Collins | 31 | Melbourne No. 1 Lake Park | |
Costa Quinn | 27 | New York No. 1 Lake Park | |
Jim Green | 27 | LA No. 1 Lake Park | |
Joe Black | 31 | Sidney No. 1 Lake Park | |
Isabella Cherry | 27 | 4222 River Rd, Columbus | |
Alex Tacker | 31 | 2952 S Peoria Ave, Tulsa | |
Endy Ruiz | 45 | 1818 H St NW, Washington | |
Jack Li | 27 | 3 Grace Dr, New Mexico |
This example shows how actions can be added to DataTable columns.
Install datatables.net-colreorder-dt
via npm
npm i datatables.net-colreorder-dt
Include the JavaScript <script>
near the end of your </body>
tag:
<script src="./node_modules/datatables.net-colreorder/js/dataTables.colReorder.min.js"></script>
|
Name
|
Age
|
Address
|
Action |
---|---|---|---|---|
|
Christina Bersh | 45 | 4222 River Rd, Columbus | |
|
David Harrison | 27 | 2952 S Peoria Ave, Tulsa | |
|
Anne Richard | 31 | 255 Dock Ln, New Tazewell | |
|
Samia Kartoon | 45 | 4970 Park Ave W, Ohio | |
|
David Harrison | 27 | 4222 River Rd, Columbus | |
|
Brian Halligan | 31 | 2952 S Peoria Ave, Tulsa | |
|
Andy Clerk | 45 | 1818 H St NW, Washington | |
|
Bart Simpson | 27 | 3 Grace Dr, New Mexico | |
|
Camila Welters | 45 | 4531 W Saile Dr, North Dakota | |
|
Oliver Schevich | 27 | 2126 N Eagle, Meridian, Illinois | |
|
Inna Ivy | 31 | 3817 Beryl Rd, Nebraska | |
|
Jessica Williams | 27 | 4807 3rd Ave, New Hampshire | |
|
James Collins | 31 | Melbourne No. 1 Lake Park | |
|
Costa Quinn | 27 | New York No. 1 Lake Park | |
|
Jim Green | 27 | LA No. 1 Lake Park | |
|
Joe Black | 31 | Sidney No. 1 Lake Park | |
|
Isabella Cherry | 27 | 4222 River Rd, Columbus | |
|
Alex Tacker | 31 | 2952 S Peoria Ave, Tulsa | |
|
Endy Ruiz | 45 | 1818 H St NW, Washington | |
|
Jack Li | 27 | 3 Grace Dr, New Mexico |
This example shows how you can add an action such as "Copy", "PDF", etc. to a DataTable.
Install datatables.net-buttons
via npm
npm i datatables.net-buttons jszip pdfmake
Include the JavaScript <script>
near the end of your </body>
tag:
<script src="./node_modules/datatables.net-buttons/js/dataTables.buttons.min.js"></script><script src="./node_modules/jszip/dist/jszip.min.js"></script><script src="./node_modules/pdfmake/build/pdfmake.min.js"></script><script src="./node_modules/pdfmake/build/vfs_fonts.js"></script><script src="./node_modules/datatables.net-buttons/js/buttons.html5.min.js"></script><script src="./node_modules/node_modules/datatables.net-buttons/js/buttons.print.min.js"></script>
Name
|
Age
|
Address
|
Action |
---|---|---|---|
Christina Bersh | 45 | 4222 River Rd, Columbus | |
David Harrison | 27 | 2952 S Peoria Ave, Tulsa | |
Anne Richard | 31 | 255 Dock Ln, New Tazewell | |
Samia Kartoon | 45 | 4970 Park Ave W, Ohio | |
David Harrison | 27 | 4222 River Rd, Columbus | |
Brian Halligan | 31 | 2952 S Peoria Ave, Tulsa | |
Andy Clerk | 45 | 1818 H St NW, Washington | |
Bart Simpson | 27 | 3 Grace Dr, New Mexico | |
Camila Welters | 45 | 4531 W Saile Dr, North Dakota | |
Oliver Schevich | 27 | 2126 N Eagle, Meridian, Illinois | |
Inna Ivy | 31 | 3817 Beryl Rd, Nebraska | |
Jessica Williams | 27 | 4807 3rd Ave, New Hampshire | |
James Collins | 31 | Melbourne No. 1 Lake Park | |
Costa Quinn | 27 | New York No. 1 Lake Park | |
Jim Green | 27 | LA No. 1 Lake Park | |
Joe Black | 31 | Sidney No. 1 Lake Park | |
Isabella Cherry | 27 | 4222 River Rd, Columbus | |
Alex Tacker | 31 | 2952 S Peoria Ave, Tulsa | |
Endy Ruiz | 45 | 1818 H St NW, Washington | |
Jack Li | 27 | 3 Grace Dr, New Mexico |