Logical task, algorithm search


I develop javascript filtering of some results and have some difficulties.
Here is the problem ...
Suppose we have some criteria

  • Manufacturer (Trusardi, Calvin Kane, Armani ...)
  • Color (red, blue, black ...)
  • OtherFeatures (goretex, blah, blah ...)

Each function is displayed as a flag ...
The problem is that I want to turn off the checkboxes if its selection does not lead to any results. For example, Armani products can only have blue color, so the armani check should turn off black and red, but other manufacturers should not be turned off ... as a check they should provide the result ...
Here is the code so far

results = $("#products li");
results.hide();
var filtersGroup = $("#filters li.filtersGroup");

$("#filters li.filtersGroup a").removeClass("disabled");
filtersGroup.each(function(index) {
    var classes = "";

    $(this).find("a.checked").each(function(index) {
        classes = classes + "." + $(this).attr("id") + ",";
    });
    if (classes == "") return true;

    results = results.filter(classes.substr(0, classes.length - 1));
    //periorismos
    filtersGroup.not($(this)).each(function(index) {
        $(this).find("a").each(function(index) {
            if (results.filter("." + $(this).attr("id")).length <= 0) {
                $(this).removeClass("checked").addClass("disabled");
            }
        });
    });
});

, . , , , , , .
, , , . ( 16 4 12 ..
, ... ?

+3
4

, .., . , -, :

results['Trusardi'] = 5 
results['Armani' = 0

.. :

$("#filters li.filtersGroup a").addClass("disabled");
foreach (m in manufacturers) {
    if (manufacturers[m] > 0) {
       $("#filters li.filtersGroup a." + m).removeClass("disabled");
   }
}

.

+1

, . , ... , , .. ....

 results=$("#products li");
            results.hide();
            var groupClasses=[];
            var groupsChecked=0;

        var filtersGroup=$("#filters li.filtersGroup");

        $("#filters li.filtersGroup a").removeClass("disabled");
        filtersGroup.each(function(index) {
            var classes="";

            $(this).find("a.checked").each(function(index) {
                classes=classes+ "." + $(this).attr("id") +",";
            });

            groupClasses[groupClasses.length]=classes;
            if(classes=="") return true;
            groupsChecked++;
            results=results.filter(classes.substr(0,classes.length-1));

        });
        //disable
        var gi=0;
        filtersGroup.each(function(index) {
            if( ! (groupsChecked<=1 && groupClasses[gi]!=""))
                {
                    $(this).find("a").not(".checked").each(function(){
                         if (results.filter("." + $(this).attr("id")).length <= 0) {
                             $(this).removeClass("checked").addClass("disabled");
                         }
                    });
                }

            gi++;
        });

, . , , ..

0

, . ( jQuery, , ...)

0
source

Source: https://habr.com/ru/post/1772750/


All Articles