我需要根据下拉列表中的选择过滤列表或记录。 我有三个下拉列表需要相互协作过滤掉记录。即,一个下拉列表中的值选择应过滤受其他下拉列表值影响的记录。
var filterAndLimitResults = function (cursor) {
if (!cursor) {
return [];
}
var raw = cursor.fetch();
var currentChosenCategory = chosenCategory.get();
var currentChosenCity = chosenCity.get();
var currentJtype = chosenJtype.get();
console.log(currentChosenCategory);
console.log(currentChosenCity);
// filter category
var filtered = [];
if (!currentChosenCategory || currentChosenCategory == "" && !currentChosenCity || currentChosenCity == "" && !currentJtype || currentJtype == "")
{
filtered = raw;
// console.log(filtered);
}
else {
filtered = _.filter(raw, function (item) {
if(currentChosenCategory){
return item.ccategory === currentChosenCategory ;
}
if(currentChosenCity){
return item.city === currentChosenCity ;
console.log(item.city === currentChosenCity);
}
});
}
var currentLimit =limit.get();
//enforce the limit
if (currentLimit ) {
filtered = _.first(filtered, currentLimit );
}
return filtered;
};
上面的代码正在对下拉列表进行过滤并限制记录数量,以便进行无限滚动。
编辑基于文本的搜索
这是我的搜索框的事件图
"keyup #search-title":function(e,t){
if(e.which === 27){
searchTitle.set("");
}
else {
var text = $(e.target.val);
searchTitle.set(text)
console.log(searchTitle.set(text));
}
}
这就是我在filteredAndLimitResults
if(!(!currentSearchTitle || currentSearchTitle == "")) {
filtered = _.filter(filtered, function (item) {
return item.title === currentSearchTitle ;
console.log(item.title === currentSearchTitle);
});
}
当我在搜索框中输入内容时。所有记录都消失了,当按下esc
时它就会恢复原状。在console.log
我可以看到每次按一个键它都会返回该集合。
答案 0 :(得分:0)
您需要一个接一个地强制执行过滤器。试试这样:
var filterAndLimitResults = function (cursor) {
if (!cursor) {
return [];
}
var raw = cursor.fetch();
var currentChosenCategory = chosenCategory.get();
var currentChosenCity = chosenCity.get();
var currentJtype = chosenJtype.get();
console.log(currentChosenCategory);
console.log(currentChosenCity);
// filter category
var filtered = [];
if (!currentChosenCategory || currentChosenCategory == "" || currentChosenCategory === "All categories")
{
filtered = raw;
// console.log(filtered);
}
else {
filtered = _.filter(raw, function (item) {
if(currentChosenCategory){
return item.ccategory === currentChosenCategory ;
}
});
}
// filter city
if (!(!currentChosenCity || currentChosenCity == "" || currentChosenCity === "All cities"))
{
filtered = _.filter(filtered, function (item) {
if(currentChosenCity){
return item.city === currentChosenCity ;
console.log(item.city === currentChosenCity);
}
});
}
// filter JType
if (!(!currentJtype || currentJtype == "" || currentJtype === "All Jtypes"))
{
filtered = _.filter(filtered, function (item) {
if(currentJtype){
//update the item.ccategory with the right field
return item.ccategory === currentJtype ;
}
});
}
var currentLimit =limit.get();
//enforce the limit
if (currentLimit ) {
filtered = _.first(filtered, currentLimit );
}
return filtered;
};