根据下拉列表筛选记录(Filtering records according to dropdown

2019-10-23 10:17发布

我需要根据dropdownlists选择过滤列表或记录。 我有一个需要筛选记录被动相互协作3个下拉菜单。 在一个下拉列表即值选择应该过滤通过其它下拉列表的值进行记录。

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;
};

上面的代码是做既过滤下拉列表和限制的记录数,从而得到无限滚动。

编辑文本基于搜索

这里是我的搜寻方块eventmap

"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));
     }
   }

这是IAM在做filteredAndLimitResults

if(!(!currentSearchTitle || currentSearchTitle == "")) {
      filtered = _.filter(filtered, function (item) {
              return item.title === currentSearchTitle ;
              console.log(item.title === currentSearchTitle);
      });
    }

当我键入搜索框的东西。 所有记录消失,当按下esc它回来,因为它是。 在console.log我可以看到,每次我按一个键就返回集合。

Answer 1:

您需要强制过滤器一前一后。 尝试这样的:

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;
};


文章来源: Filtering records according to dropdownlist