SearchBuilder v1.7.0 interferes with server-side ajax requests.
SearchBuilder v1.7.0 interferes with server-side ajax requests.
Description of problem:
SearchBuilder v1.7.0 interferes with server-side ajax requests.
Previously using v 1.3.4 I could convert the datatables data object to be sent to JSON format using a function like:
"ajax" : {
"url": 'https://mytargeturl',
"contentType" : "application/json",
"type" : 'POST',
"data": function(d) {
var sb = $('#myTable').DataTable().searchBuilder;
var sbDetails = null;
try {
sbDetails = sb.getDetails();
} catch(e) {
console.log('SearchBuilder not yet initialised!');
sbDetails = {"criteria":[{"condition":"<=","data":"ID","origData":"ID","type":"num","value":["5000"]}],"logic":"AND"}};
}
if (Object.keys(sbDetails).length > 0) {
d.searchBuilder = sbDetails;
}
return JSON.stringify(d);
}
I.e. where normally a FORM is posted I now convert the FORM data to a JSON structure and send an XHR request. This worked well with v1.3.4.
But I recently upgraded to the latest version(s) of datatables and it now uses v1.7.0 of SearchBuilder. The above data function still works but after that searchBuilder.js is still trying to do some stuff on the data which now fails as my function returns a string and not a datatable object.
Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Uncaught TypeError: can't assign to property "searchBuilder" on "{\"draw\":2,\"columns\":[{\"data\":\"ID\",\"name\":\"Id\",\"searchable\":false,\"orderable\":false,\"search\":{\"value\":\"\",\"regex\":false,\"fixed\":[]}},{\"data\":\"ONDERNEMINGSNUMMER\",\"name\":\"Ondernemingsnummer\",\"searchable\":true,\"orderable\":true,\"search\":{\"value\":\"\",\"regex\":false,\"fixed\":[]}},{\"data\":\"NAAM\",\"name\":\"Naam\",\"searchable\":true,\"orderable\":true,\"search\":{\"value\":\"\",\"regex\":false,\"fixed\":[]}},{\"data\":\"OPSTARTDATUM\",\"name\":\"Opstartdatum\",\"searchable\":true,\"orderable\":true,\"search\":{\"value\":\"\",\"regex\":false,\"fixed\":[]}},{\"data\":\"ADDRESS\",\"name\":\"Adres\",\"searchable\":true,\"orderable\":true,\"search\":{\"value\":\"\",\"regex\":false,\"fixed\":[]}},{\"data\":\"REKENINGNUMMER\",\"name\":\"Rekeningnummer\",\"searchable\":true,\"orderable\":true,\"search\":{\"value\":\"\",\"regex\":false,\"fixed\":[]}},{\"data\":\"ACTIEF\",\"name\":\"Actief\",\"searchable\":true,\"orderable\":true,\"search\":{\"value\":\"\",\"regex\":false,\"fixed\":[]}},{\"data\":\"ID\",\"name\":\"Action\",\"searchable\":false,\"orderable\":false,\"search\":{\"value\":\"\",\"regex\":false,\"fixed\":[]}}],\"order\":[{\"column\":0,\"dir\":\"asc\",\"name\":\"Id\"}],\"start\":0,\"length\":20,\"search\":{\"value\":\"\",\"regex\":false,\"fixed\":[]},\"searchBuilder\":{\"criteria\":[{\"condition\":\"<=\",\"data\":\"ID\",\"origData\":\"ID\",\"type\":\"num\",\"value\":[\"5000\"]}],\"logic\":\"AND\"}}": not an object
_setUp https://cdn.datatables.net/searchbuilder/1.7.0/js/dataTables.searchBuilder.min.js:4
Answers
That is indeed a problem. What version of DataTables were you using before? I've been looking at this, and I would have expected 1.13.7 and later to have this issue. I haven't looked back at earlier versions yet.
Allan
I'm having the exact same issue. SearchBuilder versions below v1.7.0 seem to be in-compatible w/ DataTables v2 so downgrading as a quick fix isn't easy as one would have to find versions of various other add-ons that work w/ each other. Is there a SearchBuilder v.1.8 in the works that would address this issue?
Not yet there isn't, but it is something that I will be looking into when I can.
Allan
Well I initially made this as a demo for a customer 2 years ago and then I was using v1.12.1 of DataTables with v1.3.4 of SearchBuilder.
Due to the renewed interest I tried to update the demo using the latest libraries: v2.0.3 of Datatables and v1.7.0 of SearchBuilder and hit on above problem.
Have been fighting with this for a while myself. The issue is that the searchBuilder-constructor tries to create a searchBuilder-property in the request data; if the data is already sent through JSON.stringify(d), then it can't assign that property to the resulting string in the preXhr-call, obviously. However, loading the string and adding the property also doesn't work right - it gets "forgotten" somewhere along the preXHR-handling.
My quick and dirty workaround is to simply not try to insert the searchBuilder into the object if that is a string:
And to include the searchBuilder-property in the ajax frontend calls like this (table being my dataTable-object:
And a custom preXhr-handling:
Perhaps this helps someone else facing the same problem. Or at least, that my dirty implementation gets someone annoyed enough to actually make the proper changes.
Chris
Thanks for the workaround Chris. I've still to look into this - apologies! Possibly it could be that the event should happen before the
ajax.data
function is called.Allan