JSON data request
JSON data request
I am working in a legacy java environment at the server side and find myself having problems to parse the JQuery Datatables request (either sent as GET or POST) because the nested '[]' notation (e.g. "columns[0][search][Regex]=false" using classical bean libraries as this notation doesn't really conform to a bean standard).
Therefore I was wondering whether I could have Datatable post the query parameters as a JSON string (e.g. what you get with 'table.ajax.params()'). That would sound logical since it also expects a JSON result. I am a novice at javascript and JQuery/Datatable so I don't see directly how to do this.
Answers
Searching this forum I found the following ajax option configuration to work:
"ajax" : {
url": "/myData.json",
"contentType" : "application/json",
"type" : 'POST',
"data": function(d) {
return JSON.stringify(d);
}
}
Thanks for posting back - yes, the
ajax.data
option can be used in exactly that way as shown in the last example on the documentation for that option.Just a note - I've changed the title of your post. It had your e-mail address before, which probably isn't want you want as the thread title
Allan
Ok, thanks.
Do you happen to know how I can add the search criteria of the searchBuilder plugin to the JSON data request. I can see the searchBuilder parameters (as request parameters) if I don't use "data" function above but Ican't find a way to add it to JSON data request.
Why doesn't something like the following work:
When debugging the loading of my page the 'dt' variable and 'dt.searchBuilder' exist, but 'dt.searchBuilder.getDetails()' will result in an unrecoverable error.
I'm not sure I'm afraid. If you can give me a link to the page in question I can take a look.
Allan
It is a local trial project not publicly available so I can't give you a link and since it uses serverside processing to load the data sending you the page won't help much.
Basically the Datatable part in the page looks like (omitting js,css includes):
Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide
Should have used back ticks:
I built a test case for you.
http://live.datatables.net/dehojelo/1/edit
It seems the problem isn't just with trying to use
searchBuilder.getDetails()
inajax.data
. It causes this error anytime its used.Kevin
There is a little error in the example -
#myTable
and#example
are used as the table ids which I think is causing the error with the button. The button works with the#example
id. However, it does not work is theajax.data
call as SearchBuilder itself hasn't completed its initialisation there.That said - I'm not sure what the goal is here. SearchBuilder already sends its data to the server when server-side processing is enabled:
That should work if you are using
ajax.data
to send the data as JSON in the request body does it not?Allan
Not enough coffee
Kevin
I am aware of the example to get the searchbuilder criteria in JSON format (cf. https://datatables.net/extensions/searchbuilder/examples/api/rebuild.html).
I do get the searchBuilder criteria's when the ajax call is executed but only if I leave the
ajax.data
to JSONIFY the query parameters away. When adding:to
ajax.data
the searchBuilder fields are missing in the generated JSON structure.Does that not only happen when there are no search builder criteria applied? If you add a condition, there should be parameters there.
Allan
No I tried adding a search builder criteria and I do see the getData XHR request going out, but it doesn't contain the searchBuilder criteria.
Could it be because I added manual column search criteria (as a first expirement) as shown in the initComplete function? I did something similar to https://datatables.net/examples/api/multi_filter.html but instead of adding the search input text fields to the footer I added them to the column headers.
Since allan pointed out that this is an initialization error I could get the following to work:
On page load the searchBuilder settings are skipped (not good for pre-defined searches?) but performing a search afterwards will pass the searchBulder criteria, e.g.:
But that doesn't solve the case when the page is initially loaded with a predefined search.
I'm afraid that in such a case you would probably need to add the predefined search condition manually I'm afraid. I don't currently have a way around that other than to have SearchBuilder initialise up front - which might be how we have to resolve this ultimately.
Allan
Indeed, even when specifying predefined searchCriteria the searchBuilder object isn't initialized when specifying the ajax.data option:
So any suggestions on how to get the searchBuilder object initialized?
For clarity, the above javascript will log the message 'SearchBuilder not yet iniitialized!' on page load and perform a server-side query without SearchBuilder criteria's. Any search triggered afterwards (defining global search parameter, pagination action, specify SearchBuilder condition,...) will send a server-side request where the searchBuilder critera's are passed in the json field 'SearchBuilder'.
Well I suppose could always do the following:
Not really nice, but it will work.
Yeah that's more or less what I was thinking. As you say, it is a rubbish solution, but I don't really have a better one atm I'm afraid.
Allan