【问题标题】:Angular custom text search filter orderAngular 自定义文本搜索过滤器顺序
【发布时间】:2014-08-14 19:38:16
【问题描述】:

我想调整我的角文本搜索,使其不区分字序。 我必须搜索一个大的 json 对象,但是 基本上,如果文件中有{"name":"John Doe"},我希望searchText 'Doe John' 返回正值。 有什么建议么?谢谢

<input type="text" ng-model="searchText">
<ul>
    <li ng-repeat="item in items | filter:searchText">
       {{ item }}
    </li>
</ul>

编辑:

我得到了它的工作谢谢! http://jsfiddle.net/Lp02huqm/

【问题讨论】:

标签: javascript angularjs frontend


【解决方案1】:

我实际上为某些东西创建了一个类似的过滤器。

app.filter("searchText", function () {
    return function (items, filterBy) {
        var filterBy = filterBy.split(/\s+/);

        if (!filterBy.length) {
            return items;
        }
        return items.filter(function (item) {
            return filterBy.every(function (word) {
                return ~item.indexOf(word);
            });
        });
    };
});

请注意,我使用的是 every,因此“John Doe”需要使用“Doe John”。如果您希望“Doe”或“John”也匹配,您可以使用 .some

【讨论】:

  • 这太酷了!您介意在 html 中包含一个如何使用它的示例吗? ng-repeat="item in items | searchText:searchText" 会有点奇怪。
  • @runTarm just | searchText
  • 谢谢! Array.every 函数非常有用!这是一个工作示例jsfiddle.net/Lp02huqm
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 2018-11-30
  • 1970-01-01
  • 2012-09-02
  • 2017-10-14
  • 1970-01-01
相关资源
最近更新 更多