The information read from an Ajax data source can be arbitrarily complex, but still be displayed by DataTables through the columns.data
option, which is
particularly useful for working with JSON feeds in an already defined format.
The columns.data
option has
the ability to read information not only from objects, but also from arrays using the same dotted object syntax as for objects. In addition to this, when working
with an array data source columns.data
can process the data to combine and display the data in simple forms (more complex forms can be defined
by using columns.data
as a
function).
This example shows two different aspects of using columns.data
to read arrays:
- The Name column is sourced from an array of two elements (first and last name), which are automatically concatenated together. This is done by
using array bracket syntax, with the characters between the brackets being used as the glue between elements (e.g.
name[, ]
).
- The Position, Start date and Salary columns are read directly from array elements using dotted object notation (e.g.
hr.0
). Note that the order in which the data can be used in the columns does not have to match the order in which it is defined in the data
source. The structure of the row's data source in this example is:
1 2 3 4 5 6 7 8 9 10 11 12 13 | {
"name": [
"Nixon",
"Tiger"
],
"hr": [
"System Architect",
"$3,120",
"2011/04/25"
],
"office": "Edinburgh",
"extn": "5421"
}
|
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_subarrays.txt" ,
"columns" : [
{ "data" : "name[, ]" },
{ "data" : "hr.0" },
{ "data" : "office" },
{ "data" : "extn" },
{ "data" : "hr.2" },
{ "data" : "hr.1" }
]
} );
} );
|
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | < table id = "example" class = "display" cellspacing = "0" width = "100%" >
< thead >
< tr >
< th >Name</ th >
< th >Position</ th >
< th >Office</ th >
< th >Extn.</ th >
< th >Start date</ th >
< th >Salary</ th >
</ tr >
</ thead >
< tfoot >
< tr >
< th >Name</ th >
< th >Position</ th >
< th >Office</ th >
< th >Extn.</ th >
< th >Start date</ th >
< th >Salary</ th >
</ tr >
</ tfoot >
</ table >
|
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.
{
"data": [
{
"name": [
"Nixon",
"Tiger"
],
"hr": [
"System Architect",
"$320,800",
"2011/04/25"
],
"office": "Edinburgh",
"extn": "5421"
},
{
"name": [
"Winters",
"Garrett"
],
"hr": [
"Accountant",
"$170,750",
"2011/07/25"
],
"office": "Tokyo",
"extn": "8422"
},
{
"name": [
"Cox",
"Ashton"
],
"hr": [
"Junior Technical Author",
"$86,000",
"2009/01/12"
],
"office": "San Francisco",
"extn": "1562"
},
{
"name": [
"Kelly",
"Cedric"
],
"hr": [
"Senior Javascript Developer",
"$433,060",
"2012/03/29"
],
"office": "Edinburgh",
"extn": "6224"
},
{
"name": [
"Satou",
"Airi"
],
"hr": [
"Accountant",
"$162,700",
"2008/11/28"
],
"office": "Tokyo",
"extn": "5407"
},
{
"name": [
"Williamson",
"Brielle"
],
"hr": [
"Integration Specialist",
"$372,000",
"2012/12/02"
],
"office": "New York",
"extn": "4804"
},
{
"name": [
"Chandler",
"Herrod"
],
"hr": [
"Sales Assistant",
"$137,500",
"2012/08/06"
],
"office": "San Francisco",
"extn": "9608"
},
{
"name": [
"Davidson",
"Rhona"
],
"hr": [
"Integration Specialist",
"$327,900",
"2010/10/14"
],
"office": "Tokyo",
"extn": "6200"
},
{
"name": [
"Hurst",
"Colleen"
],
"hr": [
"Javascript Developer",
"$205,500",
"2009/09/15"
],
"office": "San Francisco",
"extn": "2360"
},
{
"name": [
"Frost",
"Sonya"
],
"hr": [
"Software Engineer",
"$103,600",
"2008/12/13"
],
"office": "Edinburgh",
"extn": "1667"
},
{
"name": [
"Gaines",
"Jena"
],
"hr": [
"Office Manager",
"$90,560",
"2008/12/19"
],
"office": "London",
"extn": "3814"
},
{
"name": [
"Flynn",
"Quinn"
],
"hr": [
"Support Lead",
"$342,000",
"2013/03/03"
],
"office": "Edinburgh",
"extn": "9497"
},
{
"name": [
"Marshall",
"Charde"
],
"hr": [
"Regional Director",
"$470,600",
"2008/10/16"
],
"office": "San Francisco",
"extn": "6741"
},
{
"name": [
"Kennedy",
"Haley"
],
"hr": [
"Senior Marketing Designer",
"$313,500",
"2012/12/18"
],
"office": "London",
"extn": "3597"
},
{
"name": [
"Fitzpatrick",
"Tatyana"
],
"hr": [
"Regional Director",
"$385,750",
"2010/03/17"
],
"office": "London",
"extn": "1965"
},
{
"name": [
"Silva",
"Michael"
],
"hr": [
"Marketing Designer",
"$198,500",
"2012/11/27"
],
"office": "London",
"extn": "1581"
},
{
"name": [
"Byrd",
"Paul"
],
"hr": [
"Chief Financial Officer (CFO)",
"$725,000",
"2010/06/09"
],
"office": "New York",
"extn": "3059"
},
{
"name": [
"Little",
"Gloria"
],
"hr": [
"Systems Administrator",
"$237,500",
"2009/04/10"
],
"office": "New York",
"extn": "1721"
},
{
"name": [
"Greer",
"Bradley"
],
"hr": [
"Software Engineer",
"$132,000",
"2012/10/13"
],
"office": "London",
"extn": "2558"
},
{
"name": [
"Rios",
"Dai"
],
"hr": [
"Personnel Lead",
"$217,500",
"2012/09/26"
],
"office": "Edinburgh",
"extn": "2290"
},
{
"name": [
"Caldwell",
"Jenette"
],
"hr": [
"Development Lead",
"$345,000",
"2011/09/03"
],
"office": "New York",
"extn": "1937"
},
{
"name": [
"Berry",
"Yuri"
],
"hr": [
"Chief Marketing Officer (CMO)",
"$675,000",
"2009/06/25"
],
"office": "New York",
"extn": "6154"
},
{
"name": [
"Vance",
"Caesar"
],
"hr": [
"Pre-Sales Support",
"$106,450",
"2011/12/12"
],
"office": "New York",
"extn": "8330"
},
{
"name": [
"Wilder",
"Doris"
],
"hr": [
"Sales Assistant",
"$85,600",
"2010/09/20"
],
"office": "Sidney",
"extn": "3023"
},
{
"name": [
"Ramos",
"Angelica"
],
"hr": [
"Chief Executive Officer (CEO)",
"$1,200,000",
"2009/10/09"
],
"office": "London",
"extn": "5797"
},
{
"name": [
"Joyce",
"Gavin"
],
"hr": [
"Developer",
"$92,575",
"2010/12/22"
],
"office": "Edinburgh",
"extn": "8822"
},
{
"name": [
"Chang",
"Jennifer"
],
"hr": [
"Regional Director",
"$357,650",
"2010/11/14"
],
"office": "Singapore",
"extn": "9239"
},
{
"name": [
"Wagner",
"Brenden"
],
"hr": [
"Software Engineer",
"$206,850",
"2011/06/07"
],
"office": "San Francisco",
"extn": "1314"
},
{
"name": [
"Green",
"Fiona"
],
"hr": [
"Chief Operating Officer (COO)",
"$850,000",
"2010/03/11"
],
"office": "San Francisco",
"extn": "2947"
},
{
"name": [
"Itou",
"Shou"
],
"hr": [
"Regional Marketing",
"$163,000",
"2011/08/14"
],
"office": "Tokyo",
"extn": "8899"
},
{
"name": [
"House",
"Michelle"
],
"hr": [
"Integration Specialist",
"$95,400",
"2011/06/02"
],
"office": "Sidney",
"extn": "2769"
},
{
"name": [
"Burks",
"Suki"
],
"hr": [
"Developer",
"$114,500",
"2009/10/22"
],
"office": "London",
"extn": "6832"
},
{
"name": [
"Bartlett",
"Prescott"
],
"hr": [
"Technical Author",
"$145,000",
"2011/05/07"
],
"office": "London",
"extn": "3606"
},
{
"name": [
"Cortez",
"Gavin"
],
"hr": [
"Team Leader",
"$235,500",
"2008/10/26"
],
"office": "San Francisco",
"extn": "2860"
},
{
"name": [
"Mccray",
"Martena"
],
"hr": [
"Post-Sales support",
"$324,050",
"2011/03/09"
],
"office": "Edinburgh",
"extn": "8240"
},
{
"name": [
"Butler",
"Unity"
],
"hr": [
"Marketing Designer",
"$85,675",
"2009/12/09"
],
"office": "San Francisco",
"extn": "5384"
},
{
"name": [
"Hatfield",
"Howard"
],
"hr": [
"Office Manager",
"$164,500",
"2008/12/16"
],
"office": "San Francisco",
"extn": "7031"
},
{
"name": [
"Fuentes",
"Hope"
],
"hr": [
"Secretary",
"$109,850",
"2010/02/12"
],
"office": "San Francisco",
"extn": "6318"
},
{
"name": [
"Harrell",
"Vivian"
],
"hr": [
"Financial Controller",
"$452,500",
"2009/02/14"
],
"office": "San Francisco",
"extn": "9422"
},
{
"name": [
"Mooney",
"Timothy"
],
"hr": [
"Office Manager",
"$136,200",
"2008/12/11"
],
"office": "London",
"extn": "7580"
},
{
"name": [
"Bradshaw",
"Jackson"
],
"hr": [
"Director",
"$645,750",
"2008/09/26"
],
"office": "New York",
"extn": "1042"
},
{
"name": [
"Liang",
"Olivia"
],
"hr": [
"Support Engineer",
"$234,500",
"2011/02/03"
],
"office": "Singapore",
"extn": "2120"
},
{
"name": [
"Nash",
"Bruno"
],
"hr": [
"Software Engineer",
"$163,500",
"2011/05/03"
],
"office": "London",
"extn": "6222"
},
{
"name": [
"Yamamoto",
"Sakura"
],
"hr": [
"Support Engineer",
"$139,575",
"2009/08/19"
],
"office": "Tokyo",
"extn": "9383"
},
{
"name": [
"Walton",
"Thor"
],
"hr": [
"Developer",
"$98,540",
"2013/08/11"
],
"office": "New York",
"extn": "8327"
},
{
"name": [
"Camacho",
"Finn"
],
"hr": [
"Support Engineer",
"$87,500",
"2009/07/07"
],
"office": "San Francisco",
"extn": "2927"
},
{
"name": [
"Baldwin",
"Serge"
],
"hr": [
"Data Coordinator",
"$138,575",
"2012/04/09"
],
"office": "Singapore",
"extn": "8352"
},
{
"name": [
"Frank",
"Zenaida"
],
"hr": [
"Software Engineer",
"$125,250",
"2010/01/04"
],
"office": "New York",
"extn": "7439"
},
{
"name": [
"Serrano",
"Zorita"
],
"hr": [
"Software Engineer",
"$115,000",
"2012/06/01"
],
"office": "San Francisco",
"extn": "4389"
},
{
"name": [
"Acosta",
"Jennifer"
],
"hr": [
"Junior Javascript Developer",
"$75,650",
"2013/02/01"
],
"office": "Edinburgh",
"extn": "3431"
},
{
"name": [
"Stevens",
"Cara"
],
"hr": [
"Sales Assistant",
"$145,600",
"2011/12/06"
],
"office": "New York",
"extn": "3990"
},
{
"name": [
"Butler",
"Hermione"
],
"hr": [
"Regional Director",
"$356,250",
"2011/03/21"
],
"office": "London",
"extn": "1016"
},
{
"name": [
"Greer",
"Lael"
],
"hr": [
"Systems Administrator",
"$103,500",
"2009/02/27"
],
"office": "London",
"extn": "6733"
},
{
"name": [
"Alexander",
"Jonas"
],
"hr": [
"Developer",
"$86,500",
"2010/07/14"
],
"office": "San Francisco",
"extn": "8196"
},
{
"name": [
"Decker",
"Shad"
],
"hr": [
"Regional Director",
"$183,000",
"2008/11/13"
],
"office": "Edinburgh",
"extn": "6373"
},
{
"name": [
"Bruce",
"Michael"
],
"hr": [
"Javascript Developer",
"$183,000",
"2011/06/27"
],
"office": "Singapore",
"extn": "5384"
},
{
"name": [
"Snider",
"Donna"
],
"hr": [
"Customer Support",
"$112,000",
"2011/01/25"
],
"office": "New York",
"extn": "4226"
}
]
}