columnControl not populated in ajax.data and stateSaveParams
columnControl not populated in ajax.data and stateSaveParams
Wernfried
Posts: 10Questions: 3Answers: 0
When you use columnControl together with ajax.data, serverSide: true and/or stateSave: true, stateSaveParams then the columnControl search settings are not included in callback data parameter:
$("#example").DataTable({
columns: [...],
columnControl: [ "order", [ 'searchClear', { 'search' } ] ],
serverSide: true,
ajax: {
url: '/ajax/data',
contentType: 'application/json',
submitAs: 'json',
data: function (data) {
console.log(data.columnControl); // -> prints 'undefined'
}
},
stateSave: true,
stateSaveParams: (settings, data) => {
console.log(data.columnControl); // -> prints 'undefined'
})
})
The data is properly send for Ajax and state is properly saved, including all columnControl settings, that's working fine. However, if someone likes to manipulate data.columnControl then it is not possible, because they are not accessible.
For me this issue is not critical, because I don't need to manipulate data.columnControl, I only wanted to see it for debugging (where other possibilities are available).
Kind Regards
Wernfried
This question has an accepted answers - jump to answer
Answers
After some testing, I have to revert my statement "The data is properly send for Ajax and state is properly saved, including all columnControl settings, that's working fine."
Ajax works fine. However, the columnControl state is saved properly, but when you reload the page, i.e. when state is loaded then saved search values are used only for plain searches but
searchListare not used.Are you able to provide a link to a test case showing the issue please?
Thanks,
Allan
Let's split my post and focus to one issue each.
ajax.data not complete
When you use Server-Side processing and columnControl, then sent data is enriched with
columnControldata, see Server-side processingHere full config to demonstrate.
Open the page. No data is loaded/shown, but this does not matter. Set some filter, e.g.
console.log in
dataprintsbut console.log in
beforeSendprintsThe result should be the same.
The data which is sent to server is 100% correct, however ajax.data does not include the
columnControladdon. So, there is no easy way to manipulatecolumnControlrelated data before it is send to the server.Next issue:
stateSaveParams.data not complete
Open the table and do some filtering, like in previous example.
stateSaveParamsprintsbut in localStorage you have
The issue is similar to the previous one.
The data which is sent to localStorage is 100% correct, however stateSaveParams.data does not include the
columnControladdon. So, there is no easy way to manipulatecolumnControlrelated data before it is send to localStorage.Next issue:
searchList dropdowns are not restored from localStorage
Use the same setup as before, and do some
columnControlfilteringRefresh the page.
Filtering for
ageis restored from localStorage, howevernameis not:Please note, in the table the filter is applied properly (you see only one row for Bradley Greer). But the dropdown list in columnControl search is not updated. It may look like only a cosmetic issue. However, when you use server-side processing, then the server has no information about any
searchListfiltering which was restored from the localStorage.Interesting one. The issue is that
preXhr, which ColumnControl uses to add its data, is executed after theajax.datafunction. In this case that is certainly wrong, but I'm hesitant to immediately change the order in case there is something I'm not thinking of that would cause a problem. The documentation forpreXhrdoesn't say where in the sequence it will trigger, so it is something that could change. I've got a note to look into this more.What what it is worth though, if you want to submit a JSON string to the server, use
ajax.submitAs.The
stateSaveParamscallback is happening before thestateSaveParamsevent. If you dotable.on('stateStaveParams', ...)instead then it should work.I will have a think about how I can have the extension events happen before the developer's listeners. That is an important point there.
This is very much a bug. Thanks for letting me know about it. I've committed a fix and will be releasing ColumnControl 1.2 with the change (and your PR) shortly.
Allan
Thank you, I will test it after Christmas.
The 3rd issue was most relevant for me, thanks for the fix. Issue 1 and 2 are more cosmetic.
Wernfried