0

你好我正在搜索的骨幹集合,當前字符串我正在尋找的過濾集合

項目編號

這應返回2個結果但它永遠只能顯示1因此,我可以添加搜索參數時,不明白爲什麼,下面是我使用的代碼,

的代碼的運行,

updateSearchFilter: function(e) { 
    var self = this; 
    this.collection.each(this.filterHide, this); 
    if($(e.currentTarget).val() === "") 
    { 
     this.collection.each(this.filterShow, this); 
    } 

    var activeLabels = $(".labels").find(".inactive"); 
    $.each(activeLabels, function(key, index){ 
     switch($(index).attr("id")) 
     { 
      case "pending": 
       status = "7"; 
       self.filterDetails.states["pending"] = false; 
      break; 

      case "completed": 
       status = "5"; 
       self.filterDetails.states["complete"] = false; 
      break; 

      case "archived": 
       status = "3"; 
       self.filterDetails.states["archived"] = false; 
      break; 

      case "active": 
       status = "1"; 
       self.filterDetails.states["active"] = false; 
      break; 
     } 
    }); 

    var visible = this.collection.search($(e.currentTarget).val(), this.filterDetails.states); 
    if (visible !== undefined) { 
     visible.each(this.filterShow, this); 
    } 
}, 

因此,上面的代碼隱藏了第一次按鍵中的單獨結果,然後循環遍歷jquery對象數組並重新賦值給一個對象 - 這樣做可以確定我們需要哪些過濾器通過搜索。

然後,我們運行我們的搜索代碼,

ProjectCollection.prototype.search = function(searchTerm, filters) { 

    var pattern; 
    console.log(filters); 
    pattern = new RegExp(searchTerm, "gi"); 

    if (filters.active && filters.archived) { 
    if (searchTerm === "") { 
     return this; 
    } 
    return _(this.filter(function(data) { 
     return pattern.test(data.get("project_name") + data.get("client_name")); 
    })); 


    } else if (filters.active) { 
    if (searchTerm === "") { 
     return this.active(); 
    } 
    return _(this.active().filter(function(data) { 
     return pattern.test(data.get("project_name") + data.get("client_name")); 
    })); 


    } else if (filters.pending) { 
    console.log("hello"); 
    if (searchTerm === "") { 
     return this.pending(); 
    } 
    return _(this.pending().filter(function(data) { 
     return pattern.test(data.get("project_name") + data.get("client_name")); 
    })); 


    } else if (filters.archived) { 
    if (searchTerm === "") { 
     return this.archived(); 
    } 
    return _(this.archived().filter(function(data) { 
     return pattern.test(data.get("project_name") + data.get("client_name")); 
    })); 


    } else if (filters.complete) { 
    if (searchTerm === "") { 
     return this.complete(); 
    } 
    return _(this.complete().filter(function(data) { 
     return pattern.test(data.get("project_name") + data.get("client_name")); 
    })); 
    } 
}; 

ProjectCollection.prototype.archived = function() { 
    return new ProjectCollection(this.where({ 
    status: '3' 
    })); 
}; 

ProjectCollection.prototype.active = function() { 
    return new ProjectCollection(this.where({ 
    status: '1' 
    })); 
}; 

ProjectCollection.prototype.pending = function() { 
    return new ProjectCollection(this.where({ 
    status: '7' 
    })) 
}; 

ProjectCollection.prototype.complete = function() { 
    return new ProjectCollection(this.where({ 
    status: '5' 
    })); 
} 

現在我應該得到的回覆是2分的結果,

項目編號1 &項目編號2

不過我只有每一個搜索項得到一個結果,項目編號1具有「存檔」狀態,項目編號2具有「待定」狀態。然而,我似乎從未進入邏輯的未決部分(上面),即使filters.pending = true;

我怎樣才能確保我得到所有匹配的每個狀態返回?

+0

如果你從來沒有在控制檯看到「hello」消息,那麼顯然'filters.active'必須評估爲'true'。 (「filters.archived」的值與是否執行待定案例無關。)由於您正在向控制檯回送'filters'對象,因此請查看「filters.active」的含義。 –

回答

0

您可以將所有過濾器狀態鍵保存在一個數組中,而不是單獨設置過濾器的狀態。例如,用戶要篩選activecompletepending,所以你將有['1','5','7']

數組所以,當您篩選相關的狀態,你可以通過

// filterModes is ['1', '5', '7']; 
collection.filter(function(model){ 
    return filterModes.indexOf(model.status) !== -1; 
} 

完成剩下的應該是簡單你現在

注意collection.filter()collection.where()回報陣列和陣列可能會或可能不會有filter()絕對沒有where()所以要警惕你的鏈接函數調用