Introduction
DataTables is a powerful Javascript library for adding interaction features to HTML tables. This article demonstrates creating a DataTable instance and loading the dynamic data.
Installing Javascript/CSS
The key part of installing DataTable involves including the DataTable's Javascript and CSS files. The CSS file is optional but provides default styling for your table.
The required files can be installed in several different ways,
- Using the DataTable's CDN
- Locally
- Using package manager (NPM)
CDN
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.js">
</script>
Local installation
<link rel="stylesheet" type="text/css" href="/DataTables/datatables.css">
<script type="text/javascript" charset="utf8" src="/DataTables/datatables.js"></script>
NPM packages
DataTables and its extensions are available as NPM packages. The package base name is datatables.net, and the extensions and styling integration options are available as individual packages
- npm install datatables.net # Core library
- npm install datatables.net-dt # Styling
How to Add HTML?
For DataTables to enhance an HTML table, the table must be valid, well-formatted HTML with a header (thread) and a single body (tbody). An optional footer (tfoot) can also be used.
<table id="dtExample" class="display">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1 Data 1</td>
<td>Row 1 Data 2</td>
</tr>
<tr>
<td>Row 2 Data 1</td>
<td>Row 2 Data 2</td>
</tr>
</tbody>
</table>
Note that DataTables can generate the thread and tbody for you, along with all of the rows and cells of the table, if you are using Ajax-sourced data, but for the moment, we'll focus on plain HTML.
Please also note that while DataTables supports colspan and rowspan in the table's header and footer, they are not supported in the table's tbody and must not be present.
Initialising DataTables
We've got the HTML table we want to enhance and all the software and styles we need.
$(document).ready( function () {
$('#dtExample').DataTable();
} )
DataTables will add ordering, searching, paging, and information to your table by default, allowing your end users to find the information they want as quickly as possible.
If you want to customize your DataTable, this can be done by specifying configuration parameters in an object passed to the DataTable() function.
Configure DataTable
We have in-built options and features in the dataTable plugin to enable/ disable them in our table.
Some commonly used options/features are,
- paging- To enable pagination.
- ordering- To enable Sorting.
- info- It will display the number of records available on each page.
- language- To display custom text rather than the default.
- columnDefs- It is nothing but Column Definition. To set properties for each column as we wish.
- Scroll X& Y- Set Scroll for width & Height.
- Column Rendering- To display not only the text in the td but also we can render any HTML elements like checkbox and dropdownlist in the dataTable.
Events
We have events to catch the datatable changes.
- Order
- Search
- Page
$('#dtExample')
.on( 'order.dt', function () { alert( 'Order' ); } )
.on( 'search.dt', function () { alert( 'Search' ); } )
.on( 'page.dt', function () { alert( 'Page' ); } )
.DataTable();
DataSources
DataTables can obtain data from four different fundamental sources:
- HTML document (DOM)
- Javascript (array/objects)
- Ajax sourced data(client-side)
- Ajax sourced data(server-side)


Join the conversation! Your thoughts help the community grow.