I don't have much experience with JavaScript, so I believe there may be a misconfiguration or something that I'm overlooking.
My current setup involves using Datatables v1.10.7
. I have a table with the required parts - a thead
, tfoot
, and a tbody
in that order.
I'm implementing server-side processing to fetch data and populate the table.
In addition to Datatables functionality, I also want to perform some actions related to the fetched data. This is why I wanted to use a callback function.
$('#target-list-li').DataTable({
processing: true,
serverSide: true,
pageLength: 100,
ajax: {
url: ajax_url,
success: function(data) {
// Perform additional tasks here
return data;
}
},
columns: [
{
data: 'trans_source_id',
render: function (data, type, row) {
var html = '';
html += '<input type="checkbox" id="check-' + row.trans_source_id + '" ';
},
orderable: false
},
// More columns would go here but they have been removed as they are not relevant to the question
});
The issue or misunderstanding of how it works most likely lies within this particular line of code: success: function(data)
.
I anticipated being able to manipulate the data before returning it. Note that I do not intend to modify the original data, just extract some information from it.
success: function(data) {
// Perform some tasks here
return data;
}
However, this approach does not work at all. Even if I simply return the data without any changes, the table remains empty. The Ajax call seems to get completed, yet nothing appears in the table.
https://i.stack.imgur.com/jnnmT.png
The recommended solution for working with Ajax seems to be using dataSrc
. According to the documentation,
dataSrc: function(data) {
return data;
}
This method "sort of" works - the table gets populated with data, though it's an improvement compared to using success
.
Here is how my table looks with the dataSrc
attribute implemented.
https://i.stack.imgur.com/RGqvf.png
The documentation lacks clarity on this topic, or perhaps I am unable to find a relevant solution to my issue.
My expectation was to make an Ajax call, manipulate the fetched data for some callbacks without altering the original data, then return the original data. However, this doesn't seem to be the case here.
If anyone can provide guidance on resolving this issue, I would greatly appreciate it.