【问题标题】:Filter angular-ui typeahead autocomplete for more than one field为多个字段过滤 angular-ui typeahead 自动完成
【发布时间】:2014-08-11 13:39:02
【问题描述】:

我有一个snippet in plunker。 我正在尝试过滤两个字段的结果(在 OR 条件下)

typeahead="state as state.name for state in statesWithFlags | filter:{name:$viewValue,country:$viewValue}"

而不是当前

typeahead="state as state.name for state in statesWithFlags | filter:{name:$viewValue}"

条件应用为 AND 术语,但我需要 OR 术语

【问题讨论】:

标签: javascript angularjs angular-ui


【解决方案1】:

您可以创建自定义过滤器:

angular.module('plunker')
  .filter('custom', function () {
    return function(inputArray, args) {
      var outputArray = [];
      // Filtering logic below:
      angular.forEach(inputArray, function (item) {
        if (item.name.indexOf(args.viewValue) !== -1 || item.country.indexOf(args.viewValue) !== -1) {
          outputArray.push(item);
        }
      });
      return outputArray;
    };
  });

在此处查看正在工作的 plunker: http://plnkr.co/edit/4s2ZxOokiXw9uMG5r75

您可以在此处找到用于创建自定义过滤器的文档:https://docs.angularjs.org/guide/filter#creating-custom-filters

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 2015-07-19
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    相关资源
    最近更新 更多