2016-07-28 86 views
-1

我試圖用多個複選框進行過濾,並且在此示例中使用了來自答案的代碼:Jquery filtering with multiple Checkboxes它工作得很好,但我需要結果來顯示覆選框的組合。目前它只能與複選框過濾結果完全匹配。我希望它顯示所有結果,如果對象包含任何數組1,而不是數組。我不能用同位素我的具體項目,但它會是這樣的同位素例如:使用多個組合複選框的Jquery過濾

<a href="http://codepen.io/desandro/pen/btFfG"> 

編輯:這是該位的代碼inparticular我知道需要改變。目前它遍歷數組並顯示內容,如果該元素完全匹配複選框類別。我也需要遍歷數組和展示的內容,如果任一類別匹配任何數組變量:

 // create a collection containing all of the filterable elements 
     var $filteredResults = $('.card'); 

     // loop over the selected filter name -> (array) values pairs 
     $.each(selectedFilters, function(name, filterValues) { 

      // filter each .card element 
      $filteredResults = $filteredResults.filter(function() { 

       var matched = false; 
       var currentFilterValues = $(this).data('category').split(' '); 

       // loop over each category value in the current .card's data-category 
       $.each(currentFilterValues, function(_, currentFilterValue) { 

        // if the current category exists in the selected filters array 
        // set matched to true, and stop looping. as we're ORing in each 
        // set of filters, we only need to match once 
        if ($.inArray(currentFilterValue, filterValues) != -1) { 
         matched = true; 
         return false; 
        } 
       }); 

       // if matched is true the current .card element is returned 
       return matched; 

      }); 
     }); 
+0

有什麼特別的關於您無法理解的代碼?當你使用在線示例中的代碼時,就像你在這裏所做的那樣,試着瞭解正在做什麼,爲什麼以及如何實現這一點很重要。這樣你就不會爲討厭的錯誤感到驚訝,或者至少知道在哪裏看。事實上,隨機互聯網代碼並沒有完全按照你的意願去做,而你正在尋求某人爲你解決這個問題。這將是很好的表明你已經試圖理解,並堅持,[用一個簡短的例子](http://meta.stackexchange.com/q/22754/218143)開始 – tmpearce

+0

謝謝,我只是編輯我的答案。我知道我需要改變的部分代碼,我明白那裏有什麼。我想我幾乎在那裏,我只是想讓我的頭腦圍繞所需的邏輯和功能。我上面編輯了我的問題,希望能更好地解釋它... – Julz

回答

0

在外環:

$.each(selectedFilters, function(name, filterValues) {... 

您遍歷每個所選濾波器會逐漸消除那些與濾波器不匹配的濾波器。

$filteredResults = $filteredResults.filter(function() {... 

,使其通過滿足所有過濾器(即你正在做邏輯與操作)的唯一項目。相反,您需要過濾.card項目一次,如果該項目的任何$(this).data('category')值與任何選定的過濾器匹配,則返回true。這樣你就可以做一個合乎邏輯的OR操作。