Please can somebody advise how I use the AJAX option on a JQuery datatable. I am currently using AJAX to retrieve data and then passing this as a data variable to be used when setting up the table:
$table = $('#cat_content_datatable').DataTable ( {
select: {
style: 'single'
},
data:data,
"bFilter": false, // remove search filter
"order": [],
responsive: true,
columns: [
{ 'data': null },
{ 'data': 'content_id' },
{ 'data': 'employer' }
],
"columnDefs": [
{
"targets": -3,
"orderable": false,
"data": null,
"defaultContent": "<button type = 'button' class='btn btn-xs btn-primary'>Select</button>"
},
{
"targets": [ 1 ],
"visible": false,
"searchable": false
}
]
});
This works fine but I want to make use of the AJAX reload option on the datatable.
The data being passed to the table is:
[{"content_id":"47","employer":"ADAS"}]
I've tried the documentation AJAX option and am calling the following function:
function populateCatEmpDT (catDesc, catID, action) {
$table = $('#cat_content_datatable').DataTable ( {
select: {
style: 'single'
},
ajax: {
url: '../workflow_ajax/fields/ajax.php',
method: 'GET',
data: {catDesc: catDesc, catID:catID, emp:'BT', action: action},
dataType: 'json',
type: 'POST'
},
"bFilter": false, // remove search filter
"order": [],
responsive: true,
columns: [
{ 'data': null },
{ 'data': 'content_id' },
{ 'data': 'employer' }
],
"columnDefs": [
{
"targets": -3,
"orderable": false,
"data": null,
"defaultContent": "<button type = 'button' class='btn btn-xs btn-primary'>Select</button>"
},
{
"targets": [ 1 ],
"visible": false,
"searchable": false
}
]
});
}
I can see from the console that I'm retrieving the same data:
[{"content_id":"47","employer":"ADAS"}]
but the datatable iteself just says "Loading..." and in the console I get an error:
TypeError: f is undefined
can anyone please help? Many thanks.
Bindrid, thank you for your help and apologies for the delay in responding. In the end I used the following code:
function populateTooltipDT(contentID) {
$table = $('#tooltip_datatable').DataTable ( {
select: {
style: 'single'
},
destroy: true,
ajax: {
url: '../workflow_ajax/tooltips/ajax.php',
type: 'POST',
data: {contentID: contentID},
dataType: 'json',
dataFilter: function(data){
// DataFilter is where you can change the json data format from what your server sends to
// what DataTable expects.
// if your data is serialized at this point, deserialize it
var jData = JSON.parse(data);
// then what the DataTables expect and reserialize
var dtData =JSON.stringify( {"data": jData});
console.log(dtData);
return dtData;
}
},
"bFilter": false, // remove search filter
"order": [],
responsive: true,
columns: [
{ 'data': null },
{ 'data': 'id' },
{ 'data': 'keyword' },
{ 'data': 'tip' },
{ 'data': null }
],
"columnDefs": [
{
"targets": -5,
"orderable": false,
"data": null,
"defaultContent": "<button type = 'button' class='btn btn-xs btn-warning'>Edit</button>"
},
{
"targets": [4],
"orderable": false,
"data": null,
"defaultContent": "<button type = 'button' class='btn btn-xs btn-danger'>Delete</button>"
},
{
"targets": [ 1 ],
"visible": false,
"searchable": false
}
]
});
}