繁体   English   中英

如果所选值='all',则从过滤器中排除属性

[英]Exclude attribute from filter if selected value = 'all'

下面的功能允许用户按数据属性过滤产品,并通过创建所选值的数组同时容纳多个值的过滤。 我需要显示与任何过滤器匹配的所有结果,因此我已使用OR语句构造了函数。 我如何修改它对checked.filter(".all")响应方式,以使其不显示所有产品,而只是不按该数据属性进行过滤?

我在此处发布了带有功能示例的小提琴: http : //jsfiddle.net/chayacooper/hSwcr/1/如果选中了“无袖” +“红色”,则显示所有为“无袖”或“红色”的项目。 如果用户然后选中了“所有颜色”,则我想忽略颜色过滤器data-color="All" ),而仅应用样式过滤器data-style="Sleeveless" -这样它将显示2个项目所有项目中。

    var selected = { color: [], style: [] };   
    $('.filterOptions').on("change", ":checkbox", function (e) {
        var type = $(e.delegateTarget).data("type");
        var $this = $(this);
        var attrValue = $this.data(type);
        if ($this.parent().hasClass("active")) {
            $this.parent().removeClass("active");
            selected[type].splice(selected[type].indexOf(attrValue),1);
        }
        else {
            $this.parent().addClass("active");
            selected[type].push(attrValue);
        }            
        var checked = $(":checked", ".filterOptions");            
        // show all of the available options if...
        if (checked.length == 0 || checked.filter(".all").length > 0) {
            $('#content').find('*').show();
        } 
        else {
            $("#content").find("*").hide();
            for (var key in selected) {
                $.each(selected[key], function(index,item) {
                    $('#content').find('[data-' + key + ' *="' + item + '"]').show();
                });
            }
        }
    }); 

如何更改if逻辑:

        if (checked.length == 0 // ...no boxes are checked
            || // ...or...
            checked.filter(".all").length > 0)

像这样:

        if (checked.not(".all").length == 0)

这允许各个过滤器覆盖“所有”逻辑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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