DataTables has the ability to use data from almost data JSON data source through the use of the
columns.dataDT
option. In its simplest case,
it can be used to read arbitrary object properties, but can also be extended to n levels of
nested objects / arrays through the use of standard Javascript dotted object notation. Each dot
(.
) in the columns.dataDT
option represents
another object level.
In this example hr.position
refers to the position
property of the
hr
object in the row's data source object, while contact.0
refers to the
first element of the contact
array. Any number of dots can be used to obtain deeply nested
data.
The example below shows DataTables reading information for the columns from nested objects and arrays, where the structure of the row's data source in this example is:
{
"name": "Tiger Nixon",
"hr": {
"position": "System Architect",
"salary": "$3,120",
"start_date": "2011/04/25"
},
"contact": [
"Edinburgh",
"5421"
]
}
Name | Position | Office | Extn. | Start date | Salary |
---|---|---|---|---|---|
Name | Position | Office | Extn. | Start date | Salary |
The Javascript shown below is used to initialise the table shown in this example:
$(document).ready(function() {
$('#example').dataTable( {
"processing": true,
"ajax": "data/objects_deep.txt",
"columns": [
{ "data": "name" },
{ "data": "hr.position" },
{ "data": "contact.0" },
{ "data": "contact.1" },
{ "data": "hr.start_date" },
{ "data": "hr.salary" }
]
} );
} );
In addition to the above code, the following Javascript library files are loaded for use in this example:
The HTML shown below is the raw HTML table element, before it has been enhanced by DataTables:
This example uses a little bit of additional CSS beyond what is loaded from the library files (below), in order to correctly display the table. The additional CSS used is shown below:
The following CSS library files are loaded for use in this example to provide the styling of the table:
This table loads data by Ajax. The latest data that has been loaded is shown below. This data will update automatically as any additional data is loaded.
The script used to perform the server-side processing for this table is shown below. Please note that this is just an example script using PHP. Server-side processing scripts can be written in any language, using the protocol described in the DataTables documentation.