DataTables example Ajax data source (objects)

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.dataDT 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:

1
2
3
4
5
6
7
8
{
    "name":    "Tiger Nixon",
    "position":   "System Architect",
    "salary":    "$3,120",
    "start_date": "2011/04/25",
    "office":    "Edinburgh",
    "extn":    "5421"
}
NamePositionOfficeExtn.Start dateSalary
NamePositionOfficeExtn.Start dateSalary
Loading...
Showing 0 to 0 of 0 entries

The Javascript shown below is used to initialise the table shown in this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
$(document).ready(function() {
    $('#example').dataTable( {
        "ajax": "data/objects.txt",
        "columns": [
            { "data": "name" },
            { "data": "position" },
            { "data": "office" },
            { "data": "extn" },
            { "data": "start_date" },
            { "data": "salary" }
        ]
    } );
} );

In addition to the above code, the following Javascript library files are loaded for use in this example: