Reputation: 155
I'm populating a 'DataTable' table via ajax and it works fine. However, I'd like to also return a pipe-separated serialised string of unique IDs which form the rows to be displayed. I'm using the dataSrc option in a standard ajax call:
$('#displayRecords').dataTable( {
"lengthMenu": [ [10, 25, 50, 100, -1], [10, 25, 50, 100, "All"] ],
"ajax": {
"url": "globalJSON/recordsTable.php",
"dataSrc": "recordsTable"
}
});
So that works but on top of the recordsTable array within the json return, I'd also like to fetch the string 'recordsSerialised' and obviously use it in a wider scope.
I think using the ajax call as function is the answer but I am not sure how to implement it. The documentation reads as if I just fetch the json via an alternative $.ajax, $.post, $.whatever and pass the return into the function variables. Can anyone verify that I'm on the right lines. If not, any suggestions or corrections would be great.
Upvotes: 1
Views: 825
Reputation: 8276
You're on the right track. What you can do is call a function for dataSrc
, as long as you return
the appropriate data for the DataTable. Say your json response contains 2 arrays, recordsTable
and recordsSerialized
$('#displayRecords').dataTable({
"ajax": {
"lengthMenu": [ [10, 25, 50, 100, -1], [10, 25, 50, 100, "All"] ],
"url": "globalJSON/recordsTable.php",
"dataSrc": function (json) {
// do something with json.recordsSerialized here
return json.recordsTable;
}
}
});
I'm sure you've been looking at this already but here's the link to the documentation anyway: https://datatables.net/reference/option/ajax
Upvotes: 1