DataTables has most features enabled by default, so all you need to do to use it with your own tables is to call the construction function: $().DataTable();
.
Searching, ordering and paging goodness will be immediately added to the table, as shown in this example.
When using tables to display data, you will often wish to display column information in groups. DataTables fully supports colspan
and rowspan
in the table's header, assigning the required order listeners to the th tag
cells suitable for the columns the element covers.
DataTables will automatically apply ordering listeners and icons to all cells in the table header. If you wish to disable this, you can use the data-dt-order
property on the th/td
cells in the table header. The value of this attribute (if present, it is entirely optional) controls the ordering listeners and icons:
the HR info has data-dt-order="disable"
applied to it. The other cells in the header are all clickable and have ordering status icons shown.
DataTables saves the state of a table (its paging position, ordering state etc). When the stateSave
option is enabled, it can be restored when the user reloads a page, or comes back to the page after visiting a sub-page.
The built in state saving method uses the HTML5 localStorage
and sessionStorage
APIs for efficient storage of the data. Please
note that this means that the built in state saving option will not work with IE6/7 as these browsers do not support these APIs. Alternative
options of using cookies or saving the state on the server through Ajax can be used through the stateSaveCallback
and stateLoadCallback
options.
The duration for which the saved state is valid and can be used to restore the table state can be set using the stateDuration
initialisation
parameter (2 hours by default). This parameter also controls if localStorage
(0 or greater) or sessionStorage
(-1) is used to store
the data.
The example below simply shows state saving enabled in DataTables with the stateSave
option.
This example shows a vertically scrolling DataTable that makes use of the CSS3 vh unit in order to dynamically resize the viewport based on the browser window height. The vh unit is effectively a percentage of the browser window height. So the 50vh used in this example is 50% of the window height. The viewport size will update dynamically as the window is resized.
A relatively modern browser is required for vh units to operate correctly. IE9+ supports the vh unit and all other evergreen browsers.