1

I developed the component based on the https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Adding_decorations_to_the_backend tutorial, using the default search filter function to put it in the foreground of the website,

<?php echo JLayoutHelper::render('joomla.searchtools.default',array('view' => $this));?>

but when the search filter is filtered, it leaves the page like management background search filter and retains the filter conditions and Value, I want to automatically clear the retained value after leaving the current page, how do I do it?

1
  • Welcome to JSE, enqoo. Please take the tour while you wait for support. – mickmackusa Dec 30 '20 at 10:19
2

This is a bit interesting question because most of the users want to keep the search filter state as it is for a while and if they do not they would just click on the filter clear button manually. However it happened with me too few times that I've forgotten clearing the search filter and I have been looking for items which had been filtered out already.

So, since searchtools in Joomla is a jQuery plugin, the simplest way of handling the auto-clearing of the search filter could be with a short jQuery script according to me. You want to clear the search filter not when someone is leaving the items list page but when someone arrives back to the items list page and the search filter is not empty. That would be a simpler and more sensible, easier approach in my opinion. So the basic script would be for this:

<script type="text/javascript">

jQuery(document).ready(function($) {
  let urlReferrer = document.referrer;
  if (window.location.href != urlReferrer) {        
    let btn = $('.js-stools-btn-clear');        
    btn.click();
  }
});

</script>

Now the above script does not do too much yet. It only checks if you arrive from another URL other than the current page's url (so if you just refresh the list page it does not do anything). And if you arrive from other pages then it will trigger the clear-button click, thus ignites the searchtools clear() method (which submits the form again with a clear filter state).

But the above script must be extended with few other conditions if we really want to get a useful function:

<script type="text/javascript">

jQuery(document).ready(function($) {
  let searchField = $('input[name="filter[search]"]');
  let searchVal = searchField.val();    
  let urlReferrer = document.referrer;  
  let urlTest = urlReferrer.match(/view=article&layout=edit/g);

  if (window.location.href != urlReferrer && urlTest == null && searchVal != '') {
    let btn = $('.js-stools-btn-clear');        
    btn.click();
  }
});

</script>

So the script is extended with the followings:

  1. It checks whether you arrived from other pages and not just refreshing the page.
  2. It checks if you are not an editor of the pages and you are not just arriving back from the item's edit page where you are editing specific items you wanted to filter for editing, so you want to keep the filtered items.
  3. It checks if the search filter is already empty thus we do not have to clear that. This way the arriving new users will also not trigger this function at all.

(note: at the urlTest variable, the value of the match() method must be changed to your single item edit view name! I just wanted to test this in articles/article edit view). I also deliberately used more variables than needed just for the better overview of the script here. It could be few lines shorter of course.

The script should be placed to the bottom of the list view's layout file of your items in tmpl folder. For exmaple:

/administrator/components/com_mycomponent/views/items/tmpl/default.php

or if it is front-end:

/components/com_mycomponent/views/items/tmpl/default.php

We want to use this in list views of course. There could be other ways of clearing the state of the search filter (or model) but this is I considered as the simplest approach for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.