DataTables example Orthogonal data

To try and make life easy, by default, DataTables expects arrays to be used as the data source for rows in the table. However, this isn't always useful, and you may wish to have DataTables use objects as the data source for each row (i.e. each row has its data described by an object) as this can make working with the data much more understandable, particularly if you are using the API and you don't need to keep track of array indexes.

This can be done quite simply by using the columns.data option which you use to tell DataTables which property to use from the data source object for each column.

In this example the Ajax source returns an array of objects, which DataTables uses to display the table. The structure of the row's data source in this example is:

{
	"name":	   "Tiger Nixon",
	"position":   "System Architect",
	"salary":	 "$3,120",
	"start_date": {
		"display": "Mon 25th Apr 11",
		"timestamp": "1303682400"
	},
	"office":	 "Edinburgh",
	"extn":	   "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({ ajax: 'data/orthogonal.txt', columns: [ { data: 'name' }, { data: 'position' }, { data: 'office' }, { data: 'extn' }, { data: { _: 'start_date.display', sort: 'start_date.timestamp', }, }, { data: '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.