Here are links to (and notes about) some key pages from the DataTables documentation and web site. The documentation is extensive - and sometimes feels like a bit of a sprawl. These are items I have been referring to more frequently recently.
A blog post covering:
rows().every()
, columns().every()
and cells().every()
,
The differences, and when to use each one.
But it may be easier to iterate over a single column. The following function sums up values in the first column, if the checkbx in the second column is checked:
|
|
map()
- Create a new API instance with the result set defined by the values returned from the callback function.
toArray()
- Create a native Javascript array object from an API instance.
to$()
- Convert the API instance to a jQuery object, with the objects from the instance’s result set in the jQuery result set.
The main server side processing manual page.
Sent parameters - the data which DataTables automatically sends to the server.
Returned data - the data which is passed from the server to DataTables.
Understanding the two sets of data which are passed to and from DataTables is key to understanding how to make server-side processing work.
A simple JSON structure sent from DataTables to the server might look like this:
|
|
And a simple JSON structure sent from the server to DataTables might look like this:
|
|
I don’t use PHP, but a PHP example can be seen in the “Server-side script” tab on this page.
That script uses this:
require( 'ssp.class.php' );
You can see the referenced script in GitHub here.
Other approaches which may (or may not) help for large volumes of data:
buttons.exportData( [options] )
- choose which data to export from a DataTable to a spreadsheet, PDF, etc.
Excel options - In particular, see the customize
and exportOptions
descriptions:
customize
- modify the XSLX file that is created by Buttons. It takes 3 parameters:
exportOptions
- selects the data to be exported, using the buttons.exportData()
function described above.
Excel built-in styles - a short list of formatting styles provided in the spreadsheet created by DataTables.
The structure of the Excel files used in the above customize
parameter:
|
|
This is how the xlsx.xl.worksheets['sheet1.xml']
reference is constructed (see example below).
|
|
Another exportOptions
fragment - this time for modifying header data (and there is also format.body
and format.footer
):
|
|
This section is quite out-of-date at this point in time (2024).
You may have better fortunes here: Custom filtering - range search
The DataTables search plug-in is accessed using the following:
$.fn.dataTable.ext.search
This allows you to provide completely customized searching.
The official example for a range search uses the following code:
|
|
Be aware: $.fn.dataTable.ext.search
is an array - which is why you push()
your search function onto it. If you want to use multiple different custom search functions, you need to pop()
the unwanted one off the array first:
|
|
Or, you can create two or more separate functions:
|
|
See the documentation.
An example:
|
|
You can create a reusable renderer.
Example:
|
|
The function can then be used as follows:
|
|
DataTables uses the standard JavaScript dotted object notation to drill down into JSON data structures.
Accessing nested objects is described here.
Example:
|
|
And:
|
|
Note how contact.0
refers to the first element of the contact
array. This is a DataTables extension to the JavaScript dot-notation syntax.
For arrays, there is an additional array bracket syntax, which can be used to concatenate array values.
Example:
|
|
And:
|
|
This results in each cell containing the concatenated values from the array, with each value separated by a comma and a space (,
):
Nixon, Tiger
See here for details.
JavaScript dotted notation has some limitations:
When working with dot notation, property identifiers can only be alphanumeric (and _ and $). Properties cannot start with a number, or contain periods.
If a JSON key contains dots as part of its text, for example…
"foo" : { "baz.bat": "value" }
…then this will interfere with the dot notation process.
Alternatively, you can use JavaScript bracket notation to navigate into a data hierarchy:
foo["baz.bat"]
DataTables has two naming standards:
If you are using the latest version of DataTables (from version 1.10 onwards), you can use either the new naming standard or the old one:
There is a page in the official documentation which documents the mapping between the two naming standards:
Converting parameter names for 1.10
Download builder: https://datatables.net/download/
Compatibility charts: https://datatables.net/download/compatibility
Orthogonal data is described here. It allows you to store different versions of a data item. For example, the display value of a date may be Mon 23rd Feb, 2019
but the sort value may be 20190223
.
Orthogonal data has the following types:
display
sort
filter
type
The first three are self-explanatory. The type
type is used for “type detection”.
What does that mean?
DataTables supports automatic detection of different data types. See columns.type
for background. The type
type, as described on this page:
should normally match the sort value - see the type detection plug-in documentation.
The type detection plug-in documentation does not shed much additional light on this. An answer from the DataTables forum indicates that this is not something we should typically need to handle, outside of the above advice to “match the sort value”.
There are three main ways to access the DataTables API:
$( selector ).DataTable();
$( selector ).dataTable().api();
new $.fn.dataTable.Api( selector );
See here for details and notes explaining the differences between these syntaxes. In summary:
The result from each is an instance of the DataTables API object which has the tables found by the selector in its context. It is important to note the difference between
$( selector ).DataTable()
and$( selector ).dataTable()
. The former returns a DataTables API instance, while the latter returns a jQuery object.
If you need to access the DataTables API from within a DataTable callback function, you can use the JavaScript this
keyword.
A DataTables example is shown here:
|
|
There are various ways to handle the display, filtering, and sorting of datetimes in DataTables.
The monent.js
library can be used:
https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js
See the monent.js
documentation. It also has extensive internationalization support.
Assume a field containing the following date:
|
|
The following render function can be used in combination with DataTables orthogonal data support:
|
|
This is introduced here. The plug-in can be incorporated using this - but also relies on the above moment.js
library:
https://cdn.datatables.net/plug-ins/1.10.22/sorting/datetime-moment.js
Example:
|
|
DataTables will automatically checking to see if the data in a column matches any of the given datetime types you have registered using $.fn.dataTable.moment()
.
It also uses the DataTables ordering extension ($.fn.dataTable.ext.type.order
) to automatically provide sorting for your registered date formats.
Another “orthogonal data” approach is to use DataTables support for HTML5 attributes. For example:
|
|
DataTables has built-in support for the following:
data-sort
and data-order
- for ordering datadata-filter
and data-search
- for searching dataRead more about these HTML5 custom data-*
attributes here.
As mentioned on the official moment.js web site, this library is now a legacy project
We now generally consider Moment to be a legacy project in maintenance mode. It is not dead, but it is indeed done.
They recommend a few more modern alternatives.
The various download packages are available from the main web site here.
The GitHub repositories are here.
In GitHub the PHP source files for server-side processing examples are here. This is where you can find the ssp.class.php
file which is used by this example.
An example which places the filters in the footer.
An example which places the filters in the header. This example also happens to use fixed headers.