简体   繁体   中英

Show all divs on Unchecking the checkboxes

I have a code for product list in different divs based on different data attributes like data-brand,data-store and some more. I am filtering the records with checkbox selection which my friend helped me to develop.I need small change in this.Look at the code.

<html>
  <head>
    <meta charset="utf-8">
    <title>JS Bin</title>
  </head>
  <body>
    <div>
      <div class="content" 
           data-category="shoes" 
           data-price="4000" 
           data-size="38" 
           data-brand="Nike">
        <img src="http://placehold.it/120x80">
        <p>Nike 38<br>$4000</p>
      </div>
      <div class="content" 
           data-category="shirts" 
           data-price="6000" 
           data-size="20"
           data-brand="Nike">
        <img src="http://placehold.it/140x80">
        <p>Nike 20<br>$6000</p>
      </div>    
      <div class="content" 
           data-category="shoes" 
           data-price="500" 
           data-size="20"
           data-brand="Andrew">
        <img src="http://placehold.it/120x80">
        <p>Andrew 20<br>$500</p>
      </div>  
      <div class="content" 
           data-category="shoes" 
           data-price="780" 
           data-size="42"
           data-brand="Andrew">
        <img src="http://placehold.it/120x80">
        <p>Andrew 42<br>$780</p>
      </div>
      <div class="content" 
           data-category="shirts" 
           data-price="1200" 
           data-size="40"
           data-brand="Sunbaby">
        <img src="http://placehold.it/140x80">
        <p>Sunbaby 40<br>$1200</p>
      </div>
      <div class="content" 
           data-category="shoes" 
           data-price="2000" 
           data-size="70"
           data-brand="Andrew">
        <img src="http://placehold.it/120x80">
        <p>Andrew 70<br>$2000</p>
      </div>
      <div class="content" 
           data-category="shoes" 
           data-price="800" 
           data-size="50"
           data-brand="Sunbaby">
        <img src="http://placehold.it/120x80">
        <p>Sunbaby 50<br>$800</p>
      </div>
      <div class="content" 
           data-category="shirts" 
           data-price="1300"
           data-size="20"
           data-brand="Nike">
        <img src="http://placehold.it/140x80">
        <p>Nike 20<br>$1300</p>
      </div>
      <div class="content" 
           data-category="shirts" 
           data-price="800" 
           data-size="35"
           data-brand="Andrew">
        <img src="http://placehold.it/140x80">
        <p>Andrew 35<br>$800</p>
      </div>
    </div>
    <form id="filter">
      <div>
        <input type="checkbox" 
               name="brand" 
               value="Andrew" checked>               
               Andrew
        </input>
        <input type="checkbox" 
               name="brand" 
               value="Sunbaby">
               Sunbaby
        </input>
        <input type="checkbox" 
               name="brand" 
               value="Nike">
               Nike
        </input>
      </div>
      <div>
        <input type="checkbox" 
               name="category" 
               value="shoes" checked>               
               Shoes
        </input>
        <input type="checkbox" 
               name="category" 
               value="shirts">
               Shirts
        </input>
      </div>
      <div>
        <input type="radio" 
               name="price" 
               value="0-9000"         
               checked>
               All
        </input>
        <input type="radio" 
               name="price" 
               value="0-999">
               $0-$1000
        </input>
        <input type="radio" 
               name="price" 
               value="1000-2000">
               $1000-$2000
        </input>
      <div> 
        <div>
        <input type="radio" 
               name="size" 
               value="0-80"         
               checked>
               All
        </input>
        <input type="radio" 
               name="size" 
               value="0-25">
               Small
        </input>
        <input type="radio" 
               name="size" 
               value="26-45">
               Medium
        </input>
        <input type="radio" 
               name="size" 
               value="46-80">
               Big
        </input>
      <div> 
    </form>
  </body>
</html>

css

.hidden {display: none;}

.content {border-radius: 5px; border: 1px solid #bbb;padding: 5px; margin: 5px; float: left;}
#filter {clear: left;}

script

var filterContentForm = function(content, form){  
  var filter = function() {  
    var checkBoxGroups = {},
        radioGroups = {};
    var addRadioGroup = function(name){
      radioGroups[name] = {      
        el: $('input[name='+name+']:checked')
      };
      var n = radioGroups[name];
      n.el
      .each(function(){
        n.range = $(this).val().split('-');
        n.from = Number(n.range[0]);
        n.to = Number(n.range[1]);      
      });
    };      
    $('#filter input[type=radio]')
    .each(function(){
      addRadioGroup($(this).attr('name'));
    });      
    var addCheckBoxGroup = function(name){
      checkBoxGroups[name] = {
        el: $('input[name='+name+']:checked'),      
        ch: []
      };
      var n = checkBoxGroups[name];
      n.el.each(function(){
        n.ch.push($(this).val());
      });
    };
    $('#filter input[type=checkbox]')
    .each(function(){
      addCheckBoxGroup($(this).attr('name'));
    });
    var contents = $(content), all = 0;
    contents.removeClass('hidden')
    .each(function(){
      var $this = $(this),
          price = $this.data('price');
      for(var c in radioGroups){ 
        var n = radioGroups[c],
            d = Number($this.data(c));
        if(d < n.from || d > n.to){
          $this.addClass('hidden');
          all++;
          return;
        }
      }    
      var show = 0, i;
      for(var c in checkBoxGroups){   
        var n = checkBoxGroups[c], 
            d = $this.data(c);      
        for(i = 0; i < n.ch.length; i++){        
          if(d === n.ch[i]) {
            show++; break;
          }
        } 
      }
      var l = Object.keys(checkBoxGroups).length;
      if(show < l) {
        $this.addClass('hidden');
        all++;
      }
    });
    if(all > contents.length - 1) 
      contents.removeClass('hidden');
  };
  $(form+' input').change(filter);
  filter();
};
filterContentForm('.content', '#filter'); 
#filter {clear: left;}

The above code is working fine.I just need one small change in this. That is, on start two checkboxes are checked iefor brand ieNike and category ie shoes. I just want that on the start, these two checkboxes also need to be unchecked,all records visible,but when I am removing the 'checked' from Andrew and Shoes checkbox,Filtering doesnot happen.

Just guide me how to keep all checkboxes unchecked on start and then filtering should work after selecting the checkboxes. Thanks for help

Your filter code seems to be a little buggy. Make sure you are adding the jquery js in the header!

To toggle checked/unchecked state of your checkboxes and/or radio-buttons simply add/remove the checked attribute from the tag

<input type="checkbox" 
           name="category" 
           value="shoes" **checked**> 

Ok so this is the problem:

You are checking the following condition

if(show < l)

where show is the count of filterCategories [ in your case: brand and category ] that are checked. This is being compared against l which is count of total filterCategories present.

So, when only one of the categories is checked, this conditions becomes true, and following code

  `$this.addClass('hidden'); all++;` 

gets executed. This makes your all++ reach the value of contents.length hence your following code gets executed

  if(all > contents.length - 1) contents.removeClass('hidden'); 

which overrides the filters and shows all the items [ in your case the divs ]

To fix this see the following code. The activeFilterCount variable is the change that is required. Just replace your code from var show=0,i; to all++;} with the following code:

var show = 0, i, activeFilterCount=0; for(var c in checkBoxGroups){ var n = checkBoxGroups[c], d = $this.data(c); if(n.ch.length > 0){ activeFilterCount++; } for(i = 0; i < n.ch.length; i++){
if(d === n.ch[i]) { show++; break; } } } if(show < activeFilterCount) { $this.addClass('hidden'); all++; }

I know it got a little too lengthy, but I hope it helps you! Let me know if anything is not clear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM