你好我正在搜索的骨干集合,当前字符串我正在寻找的过滤集合
项目编号
这应返回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
;
我怎样才能确保我得到所有匹配的每个状态返回?
如果你从来没有在控制台看到“hello”消息,那么显然'filters.active'必须评估为'true'。 (“filters.archived”的值与是否执行待定案例无关。)由于您正在向控制台回送'filters'对象,因此请查看“filters.active”的含义。 –