[英]Issue on multi level filtering DOM based on different data attributes
I am working on this demo . 我正在做这个演示 。 How can I enrich my code to have multi level filtering system based on different existing and potential upcoming data attributes?
如何基于不同的现有和潜在的即将来临的数据属性来丰富我的代码,使其具有多层过滤系统?
As you can see I am trying to filter the .box
based on two data attributes shape
and color
but my code is filtering the DOM separately. 如您所见,我正在尝试根据两个数据属性的
shape
和color
来过滤.box
,但是我的代码分别过滤了DOM。 How can I fix this? 我怎样才能解决这个问题?
$('input:checkbox[name=shape]').on('change', function() {
if ( $('input:checkbox[name=shape]:checked').length > 0)
{
$(".box").each(function(){
$(this).removeClass('fadeInLeft').addClass('fadeOutLeft').css('display','none').css('display','none');
});
let data = [];
$('input:checkbox[name=shape]:checked').each(function() {
data.push($(this).data('shape'));
});
console.log(data);
$.each(data, function(index, value){
$('.box[data-shape="'+value+'"]').removeClass('fadeOutLeft').addClass('fadeInLeft').css('display','block');
});
}
else
{
$(".box").each(function(){
$(this).removeClass('fadeOutLeft').addClass('fadeInLeft').css('display','block');
});
}
});
$('input:checkbox[name=color]').on('change', function() {
if ( $('input:checkbox[name=color]:checked').length > 0)
{
$(".box").each(function(){
$(this).removeClass('fadeInLeft').addClass('fadeOutLeft').css('display','none').css('display','none');
});
let data = [];
$('input:checkbox[name=color]:checked').each(function() {
data.push($(this).data('color'));
});
$.each(data, function(index, value){
$('.box[data-color="'+value+'"]').removeClass('fadeOutLeft').addClass('fadeInLeft').css('display','block');
});
}
else
{
$(".box").each(function(){
$(this).removeClass('fadeOutLeft').addClass('fadeInLeft').css('display','block');
});
}
});
You can save the checked condition as an array of objects and iterate all the .box
. 您可以将检查的条件另存为对象数组,然后迭代所有
.box
。
$("input:checkbox").on('change', function() { let condition = []; // save all the conditions that being checked $.each($("input:checkbox:checked"), function(ele, index) { let $this = $(this); let name = $this.attr('name'); let data = $this.data(name); condition.push({ name, data }); }) // hide all box $(".box").removeClass('fadeInLeft').addClass('fadeOutLeft').css('display','none'); // iterate all box to check if matches any of the conditions if (condition.length != 0) { $('.box').each(function() { let $box = $(this); let checked = condition.find(x=> $box.data(x.name) == x.data ) != null; if(checked){ $box.removeClass('fadeOutLeft').addClass('fadeInLeft').css('display','block'); } }) } })
body { padding: 40px; } .box { float: left; margin: 10px; } .red { background: red; } .green { background: green; } .yellow { background: yellow; } .diamond { height: 60px; text-align: center; transform: rotate(45deg); width: 60px; } .circle { height: 60px; border-radius: 50%; width: 60px; } .square { height: 60px; width: 60px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="js-shape"> <input type="checkbox" name="shape" data-shape="square">square <input type="checkbox" name="shape" data-shape="circle">circle <input type="checkbox" name="shape" data-shape="diamond">diamond </div> <div class="js-color"> <input type="checkbox" name="color" data-color="red">Red <input type="checkbox" name="color" data-color="yellow">yellow <input type="checkbox" name="color" data-color="green">green </div> <div class="row"> <div class="col-md-2 animated box red square" data-shape="square" data-color="red" data-size="70">70</div> <div class="col-md-2 animated box green circle" data-shape="circle" data-color="green" data-size="71">71</div> <div class="col-md-2 animated box yellow diamond" data-shape="diamond" data-color="yellow" data-size="70">70</div> <div class="col-md-2 animated box red diamond" data-shape="diamond" data-color="red" data-size="70">70</div> <div class="col-md-2 animated box green circle" data-shape="circle" data-color="green" data-size="71">71</div> <div class="col-md-2 animated box yellow square" data-shape="square" data-color="yellow" data-size="70">70</div> <div class="col-md-2 animated box red diamond" data-shape="diamond" data-color="red" data-size="70">70</div> <div class="col-md-2 animated box green circle" data-shape="circle" data-color="green" data-size="71">71</div> <div class="col-md-2 animated box yellow circle" data-shape="circle" data-color="yellow" data-size="70">70</div> <div class="col-md-2 animated box red diamond" data-shape="diamond" data-color="red" data-size="70">70</div> <div class="col-md-2 animated box green diamond" data-shape="diamond" data-color="green" data-size="71">71</div> <div class="col-md-2 animated box yellow square" data-shape="square" data-color="yellow" data-size="70">70</div> <div class="col-md-2 animated box red diamond" data-shape="diamond" data-color="red" data-size="70">70</div> <div class="col-md-2 animated box green diamond" data-shape="diamond" data-color="green" data-size="71">71</div> <div class="col-md-2 animated box yellow diamond" data-shape="diamond" data-color="yellow" data-size="70">70</div> <div class="col-md-2 animated box red square" data-shape="square" data-color="red" data-size="70">70</div> <div class="col-md-2 animated box green square" data-shape="square" data-color="green" data-size="71">71</div> <div class="col-md-2 animated box yellow diamond" data-shape="diamond" data-color="yellow" data-size="70">70</div> <div class="col-md-2 animated box red diamond" data-shape="diamond" data-color="red" data-size="70">70</div> <div class="col-md-2 animated box green square" data-shape="square" data-color="green" data-size="71">71</div> <div class="col-md-2 animated box yellow diamond" data-shape="diamond" data-color="yellow" data-size="70">70</div> <div class="col-md-2 animated box red circle" data-shape="circle" data-color="red" data-size="70">70</div> <div class="col-md-2 animated box green square" data-shape="square" data-color="green" data-size="71">71</div> <div class="col-md-2 animated box yellow diamond" data-shape="diamond" data-color="yellow" data-size="70">70</div> </div>
BTW, IMO, I prefer using class
instead of using data-*
. 顺便说一句,IMO,我更喜欢使用
class
而不是data-*
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.