I have 2 datatables jsfiddle with 3 json
sources. It works very well.
But I need change all json
files:
Original:
{
"data": [{
"id": "11",
"cat1": "add1.1",
"cat2": "add1.2",
"cat3": "add1.3"
}, {
"id": "12",
"cat1": "add2.1",
"cat2": "add2.2",
"cat3": "add2.3"
}]
}
New: removed {"data":
in first 2 lines and last }
[{
"id": "11",
"cat1": "add1.1",
"cat2": "add1.2",
"cat3": "add1.3"
}, {
"id": "12",
"cat1": "add2.1",
"cat2": "add2.2",
"cat3": "add2.3"
}]
I hope if remove .data
in jsfiddle, and changed "ajax": url
to:
"ajax": {
"url": url,
"dataSrc": ""
},
it will be works.
Original:
$.when(call1, call2).done(function(a1, a2){
var data = a1[0].data;
data.splice.apply(data, [2, 0].concat(a2[0].data));
drawTable("#tab1","",data)
});
drawTable("#tab2","https://api.myjson.com/bins/4lpow","")
function drawTable(id, url, data) {
$(id).dataTable( {
"ajax": url,
data: data,
columnDefs: [
{ className: "hide", "targets": [ 0 ] },
],
order: [],
ordering: false,
columns: [
{ "data": "id"},
{ "data": "cat1"},
{ "data": "cat2"},
{ "data": "cat3"}
]
});
}
New:
$.when(call1, call2).done(function(a1, a2){
//removed .data
var data = a1[0];
//removed .data
data.splice.apply(data, [2, 0].concat(a2[0]));
drawTable1("#tab11","",data)
});
drawTable1("#tab21","https://api.myjson.com/bins/3tuls","")
function drawTable1(id, url, data) {
$(id).dataTable( {
"ajax": {
"url": url,
"dataSrc": ""
},
data: data,
columnDefs: [
{ className: "hide", "targets": [ 0 ] },
],
order: [],
ordering: false,
columns: [
{ "data": "id"},
{ "data": "cat1"},
{ "data": "cat2"},
{ "data": "cat3"}
]
});
}
Problem is, that it works with warning in jsfiddle
:
Please use POST request
If I test this file locally warning is:
DataTables warning: table id=tab11 - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1