Hierarchical Filters: Select all children when selecting parent

Forums › Forums › Search & Filter Pro › Hierarchical Filters: Select all children when selecting parent

Tagged: 

Viewing 6 posts – 1 through 6 (of 6 total)
  • Anonymous
    #46485

    Hello!

    We have a site running with S&F and results are great!

    We would like to reproduce the following behaviour with a hierarchical taxonomy field:

    1. Select also all children when selecting a parent
    2. Unselect parent when unselecting a child

    Expected result after first click on parent category “PASTA”

    Expected result after first click

    Expected filters in url after first click: _sft_recipe_cat=pasta,pasta-rellena,pasta-larga,pasta-corta

    Expected result after second click on child category “PASTA LARGA”

    Expected result after second click

    Expected filters in url after second click: _sft_recipe_cat=pasta-rellena,pasta-corta

    Is this possible?

    Trevor
    #46489

    Not that I would have a clue how to go about this, but I suspect this could only be achieved in js.

    Anonymous
    #46643

    Thank you Trevor.
    Here is the approach we came with

    
    $(document).ready(function() {	
    
    	var form = $('form.searchandfilter');
    	
    	if(typeof form === 'undefined') return;
    	
    	if(form.data('auto-update') == 1) return;
    
        form.parent().on('click', function (evt){ // bind click outside form in order to work with ajax
    
        	// the clicked form item
        	var _item = $(evt.target).closest('li');
        	
        	// styling stuff
        	if(_item.hasClass('sf-level-0')){
    			_item.toggleClass('sf-option-active');
    		}
    
    		// check/uncheck item and all children as well
    		// chose checkbox as form field options in plugin ui.
    		var _checked = _item.find('.sf-input-checkbox').prop('checked') == true ? false : true;
    		_item.find('.sf-input-checkbox').prop('checked', _checked);
    
    		// uncheck parent when unchecking child
    		if(!_checked){
    			var _parent_li = _item.parent().closest('li');
    			_parent_li.removeClass('sf-option-active'); // styling stuff
    			_parent_li.find(' > .sf-input-checkbox').prop('checked', false);
    		}
    
    		form.submit();
    
        	evt.stopPropagation();
    	evt.preventDefault();
        });
    });
    
    Anonymous
    #46644

    (which works)

    Trevor
    #46645

    Wow. Thanks for sharing!!!! Can I close this thread?

    Anonymous
    #46646

    Yes please!

Viewing 6 posts – 1 through 6 (of 6 total)
Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.