Adding conditions to SearchBuilder
Adding conditions to SearchBuilder
This test case has the SearchBuilder and 3 buttons that use it: live.datatables.net/joneviti/1/edit
One resets everything and the other two prefilter the SearchBuilder. My goal is to click on Filter 1 which filters with the SearchFilter and then to click on Filter 2. Filter 2 should take over all criteria from Filter 1 and apply its own Filter 2 criteria while keeping the logic AND. I tried to do this with table.searchBuilder.getDetails() but with no success with the following code:
var sbfilter = table.searchBuilder.getDetails();
var query = '';
function makeQuery(criteria, logic) {
if(criteria !== undefined){
criteria.forEach((c, idx, array) => {
if (c.criteria) {
query += '('
makeQuery(c.criteria, c.logic)
query += ')'
} else {
query += '(' + c.data + c.condition + c.value[0] + ')'
}
if (idx != array.length - 1) {
query += logic
}
})
}
}
makeQuery(sbfilter.criteria, sbfilter.logic);
crit = JSON.stringify(sbfilter.criteria);
logic = JSON.stringify(sbfilter.logic);
(Code from https://datatables.net/forums/discussion/comment/189062/#Comment_189062)
I am not sure how to integrate the JSON from "crit" into the criteria of Filter 2.
This question has accepted answers - jump to:
Answers
Hi @trongart ,
I think you are making this more complicated than it needs to be...
Can you not just push the filter into the existing criteria array that comes back from
searchBuilder.getDetails()
? Something like this...Thanks,
Sandy
This is exactly what I was looking for. Thank you!
@Sandy There is a small issue: Once I click on Filter 1 and then on Filter 2, both filters are applied correctly with the AND logic. However, when I remove Filter 2 from the SearchBuilder by clicking on the X button on the LEFT side (not on the RIGHT side), the same filter results remain from both Filter 1 and 2 even though Filter 2 is removed.
If I click on the X on the RIGHT side, the SearchBuilder refilters correctly, but this does not work on the LEFT side.
I think this applies to SeachBuilder in general - when removing an indent criteria, the SearchBuilder is not rebuilt.
Hi @trongart ,
You are quite right, something isn't right there, thanks for pointing it out. I've raised an issue internally (DD-2177 for my reference) and will report back here when there is an update.
Thanks,
Sandy
Hi @trongart ,
That was a quick fix! That's the issue fixed now - you may need to break the cache on the script tag to get the latest nightly build, which may take a few minutes to update. This will be available in the next SearchBuilder release which we hope will be in the next few weeks. Until then you can access the fix from the nightly builds.
Thanks,
Sandy
@sandy Thank you so much for the quick fix! I've applied the nightly build to the test case, but it still seems to be broken: live.datatables.net/fuqixidi/3/edit
Is this the correct build?
https://nightly.datatables.net/searchbuilder/js/dataTables.searchBuilder.js
Hi @trongart ,
Try this example. Sometimes the browser needs an extra nudge to update the file it pulls in. I've just appended
?somerandomcharacters
- that tends to be enough!Thanks,
Sandy
I confirm this works! Thank you again.
@Sandy I have a related question: Using the same method, is it possible to add criteria to the SearchBuilder not as "indent criteria". The reason for this is that it seems datatables process the addition of indent criteria much slowlier than normal criteria. You can see this by clicking many times (5-10 times) on Filter 1 or Filter 2. The performance decreases quickly.
For example, clicking on Filter 1 and then on Filter 2, should produce instead of this:
The following:
Hi @trongart ,
Yeah that is possible, you just have to change what the function is doing. Take a look at this example.
Thanks,
Sandy
On the performance note, I'm going to take a look into that this morning and see if there are any areas we can speed this up.
Thanks,
Sandy
@sandy This is excellent- Thank you very much again!
As you mentioned, the performance is still slow especially if larger tables or many criteria are used. I believe this is because everytime a button is clicked and a new criteria set added, all previous criteria are rebuilt from scratch with the new ones. It would be much faster if there was a way to only add new criteria to the previous ones instead of rebuilding the whole thing on every button click. When adding new criteria manually (without buttons by clicking on Add Conditions), the performance is much faster.
Hi @trongart ,
I've made a change that should hopefully speed up the rebuild calls. This will be available in the next SearchBuilder release which we hope will be in the next few weeks. Until then you can access the fix from the nightly builds.
That is an interesting suggestion and one we will consider but I can see one or two hurdles. The main one I can see is that you would need to be able to identify which group you want to add the criteria to. Given that you can have as many groups as you want nested as deep as you want this could pose some difficulty. At the moment the groups don't have any way of being able to tell them apart either which makes this even trickier!
I've raised an issue internally (DD-2211 for my reference) and will report back here if there is an update, although it isn't going to be something that we look at any time soon unfortunately.
Thanks,
Sandy
@sandy Thank you so much for looking into this! I've accessed the latest nightly build. I think it is slightly faster, but overall still relatively slow especially when 3-4 criteria are added at the same time and there's larget table etc. Hopefully something can be done to make this faster as it is an amazing feature to have!
@Sandy There was a real speed improvement with this in my project earlier today, but it seems to be gone now. Was there maybe another update to the nightly build which removed your change?
Hi @trongart ,
There haven't been any changes since that fix, the nightly should have the most up to date version. I was seeing load time decrease from 2 seconds to around 600ms when rebuilding with a large number of criteria.
Thanks,
Sandy
@Sandy Thank you very much for confirmation this.
I’ve found a small workaround which increases performance considerably: First you use the general search filter to get results which do not exist so you get no results in the table. After that every button click to add criteria to the SearchBuilder is almost instantaneous. Once all criteria are added, there are no further delays when sorting or filtering the table.
Maybe this is something that can be considered to improve performance.
Hi @trongart,
If that works for you then that's fine, but this wouldn't be something we would include in SearchBuilder. It should work with the standard search filter rather than actively change it to improve performance. In fact in some cases I can see this being detrimental to performance as you would be triggering more
draw
events.Thanks,
Sandy
@sandy Understood- thank you again for your help with this Sandy!