Forums › Forums › Search & Filter Pro › Hide Fields which would return 0 results
- This topic has 7 replies, 2 voices, and was last updated 5 years, 9 months ago by
Ross.
-
Ross Moderator(Private) June 15, 2020 at 11:30 am #248747
Hi Matija
Its not an option in our settings, but it might be possible if you are a developer.
If you can develop in JavaScript, I can take you through the logic I would use to approach this?
Thanks
Anonymous(Private) June 15, 2020 at 11:46 am #248761Hey Ross, that’s great news! I was thinking that I could probably write a short script that would, for each field, check for the number of items displayed and hide the whole field if the number is sufficiently low. Something along those lines. Glad to hear it’s possible!
Can you please walk me through it?
Thanks!
Ross Moderator(Private) June 15, 2020 at 12:49 pm #248804Hi Matija
Just to be clear on what you are trying to do, you essentially want to hide a field completely, if there are no options with results in it?
For this I would use the
sf:ajaxfinishJS event here:
https://searchandfilter.com/documentation/faq/#will-my-lightbox-or-other-fancy-javascript-work-with-this-pluginInside the event, as the second param, you have various data, such as the search form ID, results container, etc to help in getting the right elements to perform your checks:
$(document).on("sf:ajaxfinish", ".searchandfilter", function(event, data){ console.log("ajax complete"); console.log(data); });If your form is updating dynamcally before the results (ie,
Update the Search Form on user interactionis enabled, andauto submitis disabled) then we have an undocumented event:$(document).on("sf:ajaxformfinish", ".searchandfilter", function(event, data){ console.log("fired finish"); });The rest of your logic looks correct. In a future version of S&F, we will be provding the filter data as a JSON object, so you will be able to do easy checks to see if fields have values etc.
Thanks
Anonymous(Private) June 16, 2020 at 1:44 pm #248987Hi Ross,
Yup, that’s what I’m trying to do.
I’ve added the script to the pages where we’re using S&F. Like so:
console.log('1'); document.querySelector(".searchandfilter").addEventListener("sf:ajaxfinish", function (event, data) { console.log('ajax complete'); }, false); document.querySelector(".searchandfilter").addEventListener("sf:ajaxformfinish", function (event, data) { console.log('fired finish'); }, false);However, neither of the events seem to be firing. Instead, each time I change something in the form, the script just executes and logs ‘1’ – probably since the page reloads every time with new results. I’m fine with this. Before contacting you, this is what I expected it to be doing.
However, I’m not sure why the events aren’t firing, so just wanted to check with you to make sure I’m not making a royal mess of this.
Thanks
Ross Moderator(Private) June 16, 2020 at 6:41 pm #249034Hi Matija
As WP always ships with jQuery on the front end, why not use jQuery as I showed in the example?
In addition, you are listening on
.searchandfilterdirectly, but when the search form is replaced (in an ajax request), the object is gone, my example above (attaching todocument, and using jQuery, allows for current and all future instances to be caught).Anyway, try my code exactly as shared, and let me know if you have the same issue?
Thanks
Ross Moderator(Private) June 17, 2020 at 5:09 pm #249194Do you have a link I could look at?
Please use the “private reply” feature for any sensitive information.
Thanks
-
AuthorPosts