Alter Data During SeachBuilder preXhr Event

Alter Data During SeachBuilder preXhr Event

ShaazaamShaazaam Posts: 2Questions: 1Answers: 0
edited March 2022 in SearchBuilder

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

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794

    Interesting. It sounds like you are right that the SearchBuilder parameters are applied after the preXhr event. Built a simple test case to see this:
    http://live.datatables.net/milupisu/1/edit

    @allan or @sandy can provide more information.

    Kevin

  • sandysandy Posts: 913Questions: 0Answers: 236

    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.preDefined then 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 ajax option 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

  • ShaazaamShaazaam Posts: 2Questions: 1Answers: 0

    Neither the ajax option nor setting searchBuilder.preDefined work for accessing the searchBuilder property on the data object.

    The test case Kevin had created supports these arguments as it has both searchBuilder.preDefined and ajax.data as a function, yet the console log contains no search builder data.

    As an additional note to avoid further confusion, ensure using a console.log during any testing is performed as Kevin has done (either performing a JSON.stringify or extending into a new temporary variable). Logging the data object directly is untrustworthy since the data object 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.

  • sandysandy Posts: 913Questions: 0Answers: 236

    Hi @Shaazaam ,

    Kevins example is not showing the ajax option as a function, but instead the ajax.data option. Using ajax as a function will allow you to edit the data immediately before you make your own ajax call.

    Thanks,
    Sandy

Sign In or Register to comment.