Skip Navigation

[Resolved] Hide search results on reset

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 1 voice.

Last updated by Sammut 5 days, 15 hours ago.

Assisted by: Minesh.

Author
Posts
#2792307

We create a search function atop a page here: hidden link

Everything is working well however, for the life of me, I cannot figure out why when I click the "start new search" reset, I cannot get the results to hide without refreshing the entire page. I attached my approaches below, any help is appreciated.

I have tried a basic script like this:

jQuery(document).ready(function($) {
// 1. Initially hide the search results (div)
$("#cruise-search-results").hide();

// 2. Show results when "View Cruises" is clicked
$(".cruise-search").click(function() {
$("#cruise-search-results").show();
});

// 3. Hide results and reset fields when "Reset" is clicked
$(".cruise-reset").click(function() {
// Reset all select elements within the filter
$(".search-container select").each(function() {
$(this).prop('selectedIndex', 0); // Select the first option
});

// Hide the results immediately
$("#cruise-search-results").hide();

// Trigger Toolset filter refresh
if (typeof wpv_filter_submit === 'function') {
wpv_filter_submit();
}
});

// Use Toolset's 'wpv_parametric_search_results_updated' event with a slight delay
$(document).on('js_event_wpv_parametric_search_results_updated', function(event, data) {
// Check if it's the correct View (replace 54768 with your actual View ID)
if (data.view_id == 54768) {
// Introduce a very short delay before hiding the results
setTimeout(function() {
$("#cruise-search-results").hide();
}, 10); // 10 milliseconds (adjust as needed)
}
});
});

and then looking for a mutation to no avail:

jQuery(document).ready(function($) {
// 1. Initially hide the search results (div)
$("#cruise-search-results").hide();

// 2. Show results when "View Cruises" is clicked
$(".cruise-search").click(function() {
$("#cruise-search-results").show();
});

// 3. Hide results and reset fields when "Reset" is clicked
$(".cruise-reset").click(function() {
// Reset all select elements within the filter
$(".search-container select").each(function() {
$(this).prop('selectedIndex', 0); // Select the first option
});

// Hide the results immediately
$("#cruise-search-results").hide();

// Trigger Toolset filter refresh
if (typeof wpv_filter_submit === 'function') {
wpv_filter_submit();
}
});

// *** Mutation Observer to monitor and hide results ***
// Select the node that will be observed for mutations
const targetNode = document.getElementById('cruise-search-results');

// Options for the observer (which mutations to observe)
const config = { childList: true, subtree: true };

// Callback function to execute when mutations are observed
const callback = function(mutationsList, observer) {
for(const mutation of mutationsList) {
if (mutation.type === 'childList') {
// Hide any newly added child nodes
$(targetNode).children().hide();
}
}
};

// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);

// Start observing the target node for configured mutations
observer.observe(targetNode, config);
});

#2792404

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

What if you try to use the following custom JS code and add it to your view's custom JS box:

jQuery(document).ready(function($){
    jQuery("input[name='wpv_filter_reset']").on("click",function(){
        sessionStorage.setItem("reset_clicked", "1");
          
    });
   
});
 
jQuery( document ).on( 'js_event_wpv_parametric_search_results_updated', function( event, data ) {
    /**
    * data.view_unique_id (string) The View unique ID hash
    * data.layout (object) The jQuery object for the View layout wrapper
    */
   
  jQuery("input[name='wpv_filter_reset']").bind("click",function(){
        sessionStorage.setItem("reset_clicked", "1");
        //alert(sessionStorage.getItem("reset_clicked"));
          
    });
   
  if(sessionStorage.getItem("reset_clicked") == "1"){
        jQuery(".js-wpv-loop-wrapper").hide();
        sessionStorage.setItem("reset_clicked", "0");
  }else{
    sessionStorage.setItem("reset_clicked", "0");
    jQuery(".js-wpv-loop-wrapper").show();
  }
});

More info:
- https://toolset.com/documentation/legacy-features/views-plugin/adding-custom-javascript-views/

#2792464

Thank you! I tried the code both separately and combined them to no avail. Any other suggestions are appreciated. Clearly I am doing something wrong here. aghhhhh. LOL

jQuery(document).ready(function($) {
// 1. Initially hide the search results (div)
$("#cruise-search-results").hide();

// 2. Show results when "View Cruises" is clicked
$(".cruise-search").click(function() {
$("#cruise-search-results").show();
});

// 3. Hide results and reset fields when "Reset" is clicked
$(".cruise-reset").click(function() {
// Reset all select elements within the filter
$(".search-container select").each(function() {
$(this).prop('selectedIndex', 0); // Select the first option
});

// Hide the results immediately
$("#cruise-search-results").hide();

// Trigger Toolset filter refresh
if (typeof wpv_filter_submit === 'function') {
wpv_filter_submit();
}

// Set the flag in session storage to indicate the reset button was clicked
sessionStorage.setItem("reset_clicked", "1");
});

// Use Toolset's 'wpv_parametric_search_results_updated' event
$(document).on('js_event_wpv_parametric_search_results_updated', function(event, data) {
// Check if the reset button was clicked
if (sessionStorage.getItem("reset_clicked") == "1") {
// Find and hide the loop wrapper element within the current View's layout
$(".js-wpv-loop-wrapper", data.layout).hide();
sessionStorage.setItem("reset_clicked", "0"); // Reset the flag
}
});
});

And for fun I tried this and it did not work:

jQuery(document).ready(function($) {
// 1. Initially hide the search results (div)
$("#wpv-view-layout-54768").hide(); // Changed to target the wrapper div

// 2. Show results when "View Cruises" is clicked
$(".cruise-search").click(function() {
$("#wpv-view-layout-54768").show(); // Changed to target the wrapper div
});

// 3. Hide results and reset fields when "Reset" is clicked
$(".cruise-reset").click(function() {
// Reset all select elements within the filter
$(".search-container select").each(function() {
$(this).prop('selectedIndex', 0); // Select the first option
});

// Hide the results immediately
$("#wpv-view-layout-54768").hide(); // Changed to target the wrapper div

// Trigger Toolset filter refresh
if (typeof wpv_filter_submit === 'function') {
wpv_filter_submit();
}

// Set the flag in session storage to indicate the reset button was clicked
sessionStorage.setItem("reset_clicked", "1");
});

// Use Toolset's 'wpv_parametric_search_results_updated' event
$(document).on('js_event_wpv_parametric_search_results_updated', function(event, data) {
// Check if the reset button was clicked
if (sessionStorage.getItem("reset_clicked") == "1") {
// Hide the wrapper div again after the AJAX update is complete
$("#wpv-view-layout-54768").hide();
sessionStorage.setItem("reset_clicked", "0"); // Reset the flag
}
});
});

#2792466

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please send me admin access details and let me check what's goning wrong with your setup.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2792633

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

Actually - to use reset button is a problem here and that is why I've added the following button as reset button to your view's "Search and Pagination" section and commented out the Toolset reset button you can remove it later on.
=> hidden link

<input type="button" onclick="javascript:window.location.href=window.location.protocol + '//' + window.location.host + window.location.pathname;" class="cruise-reset" name="wpv_filter_reset" value="Start New Search">

I've adjusted the custom JS code as given under:

jQuery(document).ready(function($){

  jQuery("#cruise-search-results").addClass("hidden");
  
 
jQuery( document ).on( 'js_event_wpv_parametric_search_results_updated', function( event, data ) {
    /**
    * data.view_unique_id (string) The View unique ID hash
    * data.layout (object) The jQuery object for the View layout wrapper
    */
  jQuery("#cruise-search-results").removeClass("hidden");
  
    
  
});

});

Can you please confirm it works as expected.

#2792641

OK, I see. Two things we were hoping to preserve were:

1) the results only display when the view cruises button is clicked
2) clearing the results with no page reload.

Is this feasible?

Thank you!

#2793122

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

1) the results only display when the view cruises button is clicked
==>
Ok. I've changed the "Custom Search and Pagination" setting option to "AJAX results update when visitors click on the search button" and now results will be only displayed when you click on the button.

2) clearing the results with no page reload.
==>
No, there is no such optoin available and we already managed it using custom way and thats the only solutoin I have to offer at the moment.

#2793294

OK, thank you. I will take a look over the changes.