javascript - Filtering by two different categories -



javascript - Filtering by two different categories -

i have list of people , activities.

i have them filtered 2 categories, name , activity. made 2 select's , filled 1 people's names , 1 activities.

<select class="filter people"> <option value="0">all people</option <option value="1">john</option> <option value="2">mary</option> <option value="3">dan</option> etc. </select> <select class="filter activities"> <option value="0">all activities</option <option value="1">football</option> <option value="2">basketball</option> <option value="3">painting</option> <option value="4">racing</option> etc. </select>

sample of list structure.

<div id="wrap"> <div class="item people3 activity2">dan:basketball</div> <div class="item people3 activity1">dan:football</div> <div class="item people1 activity2">john:basketball</div> </div>

what want show items include selected filter.

for example, selecting basketball under category dropdown, hide every item except dan:basketball , john:basketball, selecting basketball under category, , john under people, hide every item except john:basketball. item value 0, shows all items selected filter.

here tried far. works 1 filter, combine work (n) filters, in case two, people , activities.

// $('.filter').change(function(){ $('.people').change(function(){ if($(this).val()!=0) { if(current != '') { $('div.item').not('.'+current).show(); } current = $(this).val(); $('div.item').not('.'+current).hide(); } else $('.item').show(); });

why not creating seperate function apply filtering?

see fiddle

$('.people').change(function(){ applyfilter(); }); $('.activities').change(function(){ applyfilter(); }); function applyfilter() { // selected people , activity var people = $('.people').val(); var activity = $('.activities').val(); // add together people , activity filter array var classfilter = []; if (people !=0) { classfilter.push('people'+people); } if (activity != 0) { classfilter.push('activity' + activity); } // show if filter array empty or apply filter if (classfilter.length == 0) { $("div.item").show(); } else { $("div.item").hide(); $("div.item." + classfilter.join(".")).show(); } }

you can create more generic here back upwards additional filtering.

javascript jquery filter filtering

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -