Alter Data During SeachBuilder preXhr Event
Alter Data During SeachBuilder preXhr Event
Link to test case:
Debugger code (debug.datatables.net): https://debug.datatables.net/oqugup
Error messages shown: None
Description of problem:
Is it possible to alter Search Builder data during the preXhr event for server side processing?
Running a function on preXhr.dt (the namespace is irrelevant to the call, simply using preXhr works) after initializing the DataTable object does fire on a Search Builder request, however the data does not include the searchBuilder property.
Reviewing the source code, under SearchBuilder.prototype._setUp I found this:
this.s.dt.on('preXhr', function (e, settings, data) {
if (_this.s.dt.page.info().serverSide) {
data.searchBuilder = _this._collapseArray(_this.getDetails(true));
}
});
It would seem this mutation occurs after any other user functions have been registered. Unless I am missing something obvious, it currently does not seem possible to alter data.searchBuilder during any preXhr events.
Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Answers
Interesting. It sounds like you are right that the SearchBuilder parameters are applied after the
preXhrevent. Built a simple test case to see this:http://live.datatables.net/milupisu/1/edit
@allan or @sandy can provide more information.
Kevin
Hi @Shaazaam,
You're right, it's not possible to do it that way with the way that SearchBuilder is currently setup. I'm not sure why you would want to change that data though?
It will pass the data to the serverside in the format that the serverside integration is expecting. If you set
searchBuilder.preDefinedthen this will be included in the first call to the server. What's the use case for changing SearchBuilders data at that point?One possible way around this is to use the
ajaxoption and make the ajax call yourself. You could access and manipulate the data before you make the call there, which would be after SearchBuilder has set it.Thanks,
Sandy
Neither the
ajaxoption nor settingsearchBuilder.preDefinedwork for accessing thesearchBuilderproperty on thedataobject.The test case Kevin had created supports these arguments as it has both
searchBuilder.preDefinedandajax.dataas a function, yet the console log contains no search builder data.As an additional note to avoid further confusion, ensure using a
console.logduring any testing is performed as Kevin has done (either performing aJSON.stringifyor extending into a new temporary variable). Logging thedataobject directly is untrustworthy since thedataobject is being mutated.I am sending a modified payload to my server. Instead of sending key/value pairs the data needs to be a JSON string. The serverside integrations you mentioned are not an option, as I do not use any of the runtimes supported.
I'm not sure my use case is entirely relevant to the issue, as I would think users should be able to alter any aspect of the payload before the request is sent.
Hi @Shaazaam ,
Kevins example is not showing the
ajaxoption as a function, but instead theajax.dataoption. Usingajaxas a function will allow you to edit the data immediately before you make your own ajax call.Thanks,
Sandy