Isotope: combination filtering + multiple selection

前端 未结 2 2084
轻奢々
轻奢々 2021-02-11 04:48

To all jQuery heros out there, I need help! (again)

I found similar questions to mine on the internet but there were no answers so far :-/

I want to filter a bu

2条回答
  •  北荒
    北荒 (楼主)
    2021-02-11 05:39

    have a look here

    http://codepen.io/desandro/pen/btFfG

    it's the same answer by DrewT but from the Isotope team.

    all you have to do in order to filter is put every group under same object

    var filters = {}; // should be outside the scope of the filtering function
     
    //filtering function
    $a.click(function() {       
        var group = $optionSet.attr('data-filter-group');
        var filterGroup = filters[group];
        if (!filterGroup) {
            filterGroup = filters[group] = [];
        }
        filters[group].push($this.attr('data-filter-value'));
    })
           
    

    now all you have to do is call getComboFilter function (you don't have to understand the code inside getComboFilter, think of it as a part of isotope)

    var comboFilter = getComboFilter( filters );
    $container.isotope({ filter: comboFilter});
     
     function getComboFilter( filters ) {
       var i = 0;
       var comboFilters = [];
       var message = [];
     
       for ( var prop in filters ) {
         message.push( filters[ prop ].join(' ') );
         var filterGroup = filters[ prop ];
         // skip to next filter group if it doesn't have any values
         if ( !filterGroup.length ) {
           continue;
         }
         if ( i === 0 ) {
           // copy to new array
           comboFilters = filterGroup.slice(0);
         } else {
           var filterSelectors = [];
           // copy to fresh array
           var groupCombo = comboFilters.slice(0); // [ A, B ]
           // merge filter Groups
           for (var k=0, len3 = filterGroup.length; k < len3; k++) {
             for (var j=0, len2 = groupCombo.length; j < len2; j++) {
               filterSelectors.push( groupCombo[j] + filterGroup[k] ); // [ 1, 2 ]
             }
     
           }
           // apply filter selectors to combo filters for next group
           comboFilters = filterSelectors;
         }
         i++;
       }
     
       var comboFilter = comboFilters.join(', ');
       return comboFilter;
     }
    

提交回复
热议问题