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()所以要警惕你的链接函数调用