简体   繁体   English

角度搜索多个输入值

[英]Angular Search multiple input values

I have a result set populated via ng-repeat. 我有一个通过ng-repeat填充的结果集。 Now I need to do a sort of filtering. 现在,我需要进行某种过滤。 I tried using ng-filter to filter out search results. 我尝试使用ng-filter过滤掉搜索结果。 Use Case : User can enter multiple merchantIds each comma separated (or any other preffered machanism). 用例:用户可以输入每个逗号分隔的多个商人ID (或任何其他使用的机制)。 And the result set populated corresponds to the merchantIds the user entered. 并且填充的结果集对应于用户输入的商人ID。 How currently works : Search works well when searching single merchantId. 当前工作方式:搜索单个商人ID时,搜索效果很好。 For example when user types merchantId, this list downs all merchants that starts with merchantId . 例如,当用户键入merchantId时,此列表将列出所有以商人id开头的商人 When the input value is merchantId_1 , the result is only related to merchantId_1. 当输入值为merchantId_1时 ,结果仅与merchantId_1相关。 What I need is the user should be given the capability of typing merchantId_1 , merchantId_2 (each comma seperated) and list down the result corresponds to both merchantIds. 我需要的是,应该给用户键入商人ID_1商人ID_2 (每个逗号分隔)的能力,并列出与两个商人ID对应的结果。

What is the feasible way of achieving this using Angular. 使用Angular实现此目的的可行方法是什么。

I made a plunker that achieves what you were aiming for. 我做了一个实现您目标的pl子。

https://plnkr.co/edit/T9Z6GCFHzwH9xCKxkxJ7?p=preview https://plnkr.co/edit/T9Z6GCFHzwH9xCKxkxJ7?p=preview

HTML: HTML:

<input ng-model="searchText" />
<div ng-repeat="merchant in merchants | merchantFilter:searchText">
  {{merchant}}
</div>

JS: JS:

angular.module('app', [])
.controller('testController', function($scope) {
  $scope.merchants = ['merchant_1', 'merchant_2', 'merchant_3']
})
.filter('merchantFilter', function() {
  return function(merchants, searchText) {
    // If there's no input, return full list
    if (!searchText) return merchants;
    // Split and trim the search string
    var searchTerms = searchText.split(',');
    searchTerms = searchTerms.map(function(term) {
      return term.trim();
    })
    // Filter and return merchants matching any search term
    var returnedArray = [];
    _.forEach(merchants, function(merchant) {
      if (_.find(searchTerms, function(term) {
        return merchant.indexOf(term) > -1;
      })) {
        returnedArray.push(merchant)
      }
    })
    return returnedArray;
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM