简体   繁体   English

筛选多个类别的产品

[英]Filter products on several categories

I'm building a product filter for a webshop.我正在为网上商店构建产品过滤器。 Currently there are 3 categories (product properties) but the quantity of categories can vary.目前有 3 个类别(产品属性),但类别的数量可能会有所不同。 I'm already a day in to this seamingly easy task but no solution that fits my need 100%.我已经完成了这项极其简单的任务,但没有 100% 满足我需要的解决方案。 Searched SO but couldn't find an exact match for my problem.搜索了 SO,但找不到与我的问题完全匹配的内容。

Right now I've got some code working that only shows products that have the exact properties.现在我有一些代码工作,只显示具有确切属性的产品。 The problem is that this is too limiting.问题是这太局限了。

This is the code I have now to filter products, the filter doesn't need to get data from a server, all products are available and have the class of the filter values:这是我现在过滤产品的代码,过滤器不需要从服务器获取数据,所有产品都可用并且具有过滤器值的 class:

JS JS

 $('.choice_form').change(function(evt){

        var filter = $(':input:checked,select').map(function(index, el) {
            return "." + el.value;
        }).toArray().join("");

        $(".vis-products").hide().filter(filter).show();

    });

HTML: HTML:

<div class="categories default-text">
  <!-- choice help -->
  <div class="choicehelp-wrapper">
    <b class="black">Basisdeel</b><br><label class="noselect choicehelp_text"><input type="checkbox" name="choice_option[]" value="ft12" class="choice_form js" data-group="product" data-action="choicehelp" data-value="12" data-id="1" style="margin:6px 0px 6px 0px">&nbsp;&nbsp;Kunststof, zwart</label><br>
    <label class="noselect choicehelp_text"><input type="checkbox" name="choice_option[]" value="ft13" class="choice_form js" data-group="product" data-action="choicehelp" data-value="13" data-id="1" style="margin:6px 0px 6px 0px">&nbsp;&nbsp;Messing, nikkel</label><br><br><b class="black">Insteek</b><br>
    <label class="noselect choicehelp_text"><input type="checkbox" name="choice_option[]" value="ft11" class="choice_form js" data-group="product" data-action="choicehelp" data-value="11" data-id="2" style="margin:6px 0px 6px 0px">&nbsp;&nbsp;Knie insteek koppeling</label><br><label class="noselect choicehelp_text"><input type="checkbox" name="choice_option[]" value="ft9" class="choice_form js" data-group="product" data-action="choicehelp" data-value="9" data-id="2" style="margin:6px 0px 6px 0px">&nbsp;&nbsp;Rechte inschroef koppeling</label><br>
    <label class="noselect choicehelp_text"><input type="checkbox" name="choice_option[]" value="ft10" class="choice_form js" data-group="product" data-action="choicehelp" data-value="10" data-id="2" style="margin:6px 0px 6px 0px">&nbsp;&nbsp;T-inschroef koppeling</label><br><br><b class="black">Inschroef</b><br>
    <label class="noselect choicehelp_text"><input type="checkbox" name="choice_option[]" value="ft7" class="choice_form js" data-group="product" data-action="choicehelp" data-value="7" data-id="3" style="margin:6px 0px 6px 0px">&nbsp;&nbsp;M4 Diameter</label><br><label class="noselect choicehelp_text"><input type="checkbox" name="choice_option[]" value="ft4" class="choice_form js" data-group="product" data-action="choicehelp" data-value="4" data-id="3" style="margin:6px 0px 6px 0px">&nbsp;&nbsp;M5 Diameter</label><br>
    <label class="noselect choicehelp_text"><input type="checkbox" name="choice_option[]" value="ft6" class="choice_form js" data-group="product" data-action="choicehelp" data-value="6" data-id="3" style="margin:6px 0px 6px 0px">&nbsp;&nbsp;M8 Diameter</label><br><label class="noselect choicehelp_text"><input type="checkbox" name="choice_option[]" value="ft8" class="choice_form js" data-group="product" data-action="choicehelp" data-value="8" data-id="3" style="margin:6px 0px 6px 0px">&nbsp;&nbsp;M9 Diameter</label> </div>
</div>

And the product html:以及产品html:

<div class="visnav3 vis-products vis-catalog js ft11 ft12 filterOn" data-group="product" data-action="goto-details" data-id="111111" data-value="DEMO-2" style="">....</div>

The ftxx classess correspond with the checkbox values. ftxx 类对应于复选框值。

A bit too much too include, you can view it here: https://www.technicomponents.nl/nl/p/pneumatiek/insteekkoppelingen/knie-koppelingen/有点太多了,你可以在这里查看: https://www.technicomponents.nl/nl/p/pneumatiek/insteekkoppelingen/knie-koppelingen/

It should do this:它应该这样做:

Case 1. If you just click 1 filter, all products with that filter as a classname should be visible案例 1. 如果您只单击 1 个过滤器,则所有具有该过滤器作为类名的产品都应该可见

Case 2. If you select for example 2 or more filters from 1 category, all products that have the selected filter as a class should be visible.案例 2. 如果您 select 例如 2 个或更多来自 1 个类别的过滤器,则所有具有选定过滤器为 class 的产品都应该可见。

Case 3. If you select one or multiple values from multiple categories it should be something like:案例 3. 如果您 select 来自多个类别的一个或多个值,它应该类似于:

WHERE在哪里

Category 1:第一类:
- Filter x OR - 过滤器 x 或
- Filter y OR - 过滤 y 或
- Filter z OR - 过滤器 z 或

AND

Category 2:第 2 类:
- Filter x OR - 过滤器 x 或
- filter z OR - 过滤 z 或

AND

Category 3:第三类:
- Filter y OR - 过滤 y 或
- Filter z OR - 过滤器 z 或

The current code only works if you select 1 value (from one or multiple categories).当前代码仅适用于 select 1 值(来自一个或多个类别)。

You would need a way to know which group an input belongs to, so you know when to apply the AND-logic, and when the OR-logic.您需要一种方法来了解input属于哪个组,以便您知道何时应用 AND 逻辑以及何时应用 OR 逻辑。 It looks to me like you have such information in the data-id attribute.在我看来,您在data-id属性中有这样的信息。 I will assume this attribute has an equal value for inputs that belong to the same group, and that it's different when the inputs are in a different group.我将假设该属性对于属于同一组的输入具有相同的值,并且当输入在不同的组中时它是不同的。 (In case this is not right, just choose something that does make this distinction). (如果这不正确,只需选择能够做出这种区分的东西)。

You can perform the OR-operation with the CSS comma.您可以使用 CSS 逗号执行 OR 操作。 The AND operation could then be executed as consecutive calls of jQuery's filter method.然后,AND 操作可以作为 jQuery filter方法的连续调用来执行。

Here is a very much simplified demo of this idea:这是这个想法的一个非常简化的演示:

 $("input").change(function () { var filters = {}; // key/value per group $(':input:checked,select').each(function(index, el) { var group = $(el).data("id"); // <-- something that identifies the group filters[group] = (filters[group] || []).concat("." + el.value); }); var $filtered = $(".vis-products").hide(); // Apply each filter on the result of the previous one: for (var group in filters) $filtered = $filtered.filter(filters[group].join(",")); $filtered.show(); });
 .filter { width: 100px; float: left; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script> <div class="filter"> Cat 1:<br> <input type="checkbox" data-id="1" value="a">a<br> <input type="checkbox" data-id="1" value="b">b<br> Cat 2:<br> <input type="checkbox" data-id="2" value="i">i<br> <input type="checkbox" data-id="2" value="j">j<br> <input type="checkbox" data-id="2" value="k">k<br> Cat 1:<br> <input type="checkbox" data-id="3" value="x">x<br> <input type="checkbox" data-id="3" value="y">y<br> </div> Data:<br> <div class="vis-products aix">ai x</div> <div class="vis-products aiy">ai y</div> <div class="vis-products ajx">aj x</div> <div class="vis-products ajy">aj y</div> <div class="vis-products akx">ak x</div> <div class="vis-products aky">ak y</div> <div class="vis-products bix">bi x</div> <div class="vis-products biy">bi y</div> <div class="vis-products bjx">bj x</div> <div class="vis-products bjy">bj y</div> <div class="vis-products bkx">bk x</div> <div class="vis-products bky">bk y</div>

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

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