Server side processing removing all data from JSON that isn't used in any column
Server side processing removing all data from JSON that isn't used in any column
When using serverSide: true
all fields that aren't used by one of the columns are automatically removed from the JSON that's stored under table.data()
and cannot be accessed for further processing.
For example I have some custom rendering logic, that adds a star depending on another value's boolean.
table = $('#snowflake').DataTable({
ajax: {
url: "/api/entries/",
},
processing: true,
serverSide: true,
searchDelay: 1500,
rowId: 'id',
order: [[0, 'asc']],
columns: [
{
data: 'order',
className: 'reorder',
}, {
data: "name",
name: "name",
render: function(data, type, row) {
if (row.enabled) { // row.enabled will be undefined
return data + ' *';
}
return data;
}
},
],
});
JSON returned from server:
[{
"id": 1,
"order": 1,
"name": "Hello world",
"enabled": true
}, ...]
However because enabled
does not have a definition in columns
it's not accessible by the name
render
method, as it's automatically discarded/set to undefined
by DataTables.
Any idea how to keep all data returned from the server – just like it would, if we were using the normal, local non-serverSide
mode?
PS: More on a sidenote; is it possible to call table.draw()
without fetching the data from the API again? I just want to update column visibility.
Answers
What you described works here:
http://live.datatables.net/pitawoho/1/edit
Maybe all the rows don't have the property
enabled
.Calling
draw()
will fetch data from the server script and doesn't have an option to not fetch fro the server with server side processing is enabed.You shouldn't need to call
draw()
when updating column visibility. This is also in the example I posted.If this doesn't help then please post a link to your page or a test case replicating the issues so we can help debug.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Thanks. You're right. I realized my backend is causing the filtering out as described when the "long serverSide-url" is active depending on the
columns[X][data]=...
setting. Sorry for the hassle.Regarding the visibility – this is how I'm doing it. Hiding the column works, but showing it again doesn't:
I updated the example to show that showing the column doesn't need
draw()
:http://live.datatables.net/pitawoho/2/edit
Again please provide a test case showing the issue so we can help debug.
Kevin