In order to work, why does the AJAX array need to be called 'data" in the "Child rows example"?
In order to work, why does the AJAX array need to be called 'data" in the "Child rows example"?
I would like to load data into a DataTable using "Child rows" attached to a parent table from a MongoDB data source. MongoDb allows for embedded documents as arrays and/or JSON objects. Some of that data will have the same structure as the sample AJAX used in the "Child rows" example page, with the exception that the name "data" might be "subcontractor" or "manager" instead . The following shows the start of the example AJAX with the name "data":
"data": [
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
}
I have no problems using the example JavaScript and AJAX data as long I don't modify the spelling for the name "data" in the source data. When I tried changing "data" to "subcontractor" or "data2" the DataTable wouldn't work. I tried modifying the JavaScript below by changing any reference to "data" to "subcontractor" or "data2" with no results. What do I need to change in the sample JavaScript below to use a different name than "data" in my source JSON?
$(document).ready(function() {
var table = $('#example').DataTable( {
"ajax": "../ajax/data/objects.txt",
"columns": [
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": ''
},
{ "data": "name" },
{ "data": "position" },
{ "data": "office" },
{ "data": "salary" }
],
"order": [[1, 'asc']]
} );
This is the link to the"Child rows" sample page:
This question has an accepted answers - jump to answer
Answers
You can use the
ajax.dataSrc
option to change where Datatables looks for the data.Kevin
I used the ajax.dataScr option as in the following and it didn't work:
I also changed the AJAX data to the following:
Again if I used the default "data" value in the AJAX data source I had no problems but if I used "staff" it wouldn't work. Any suggestions?
Hi @FernandoRoldan ,
In your first code, lines 11 to 14 should read
{"data":"name"}
, not "staff". Whatever you set for theajax.dataSrc
, it is alwayscolumns.data
,Cheers,
Colin
Hi @colin,
I had already tried setting lines 11 to 14 to read {"data":"name"}, and that didn't work either.
Oddly enough, the same code as below with lines 11 to 14 set to {"data": "name" }, as you have suggested worked only if the object property in the AJAX source was set to "data".
I say it is odd, only because the code worked even though "dataScr" was set to "staff".
$(document).ready(function() {
var table = $('#example').DataTable( {
"ajax": "../ajax/data/objects.txt",
"dataSrc": "staff",
"columns": [
{
"className": 'details-control',
"orderable": false,
"defaultContent": ''
},
{ "data": "name" },
{ "data": "position" },
{ "data": "office" },
{ "data": "salary" }
],
"order": [[1, 'asc']]
} );
I love using the DataTables plugin and I have no problems using "data" as the default object property if I'm using a relational database; although I am having second thoughts on using it with a document database. A single MongoDB document can have multiple embedded objects where each object will have its own unique property name.
Has anyone been able to use the source data in objects.txt that was provided in the "Child rows" example without using "data" as the default object property? If you have, can you provide a JavaScript example?
Thanks,
Fernando
I say it is odd, only because the code worked even though "dataScr" was set to "staff".
I'm confused by this comment. Just to clarify you are able to use
dataSrc
set tostaff
and it works, correct?When you say "default object property" are you referring to the the
columns.data
config option? If so then that is what Datatables uses to know the names of the objects being returned. The key ofdata
in this context has no relation to the data object being loaded into datatables. Its just a config option likecolumns.title
.Kevin
The
dataSrc
property needs to be a property of theajax
object. What you currently have:will be ignored by DataTables. Use:
See the
ajax.dataSrc
documentation for more details.Allan
@Allan,
That did it!
Thank you,
Fernando