AngularJS 选择 - 匹配第一个字符

标签 angularjs search select

https://github.com/angular-ui/ui-select

此搜索插件使用'LIKE %word%'查找匹配项。可以将其更改为'LIKE word%' 示例:如果我在搜索州时输入字母“T”,当前它会返回以下内容:
H
D
L
N
一个
M
R
...

我希望它返回:
D
L
N
一个

最佳答案

您可以通过使用 Angular 的自定义过滤器来实现此功能。 像这样的东西:

app.filter('propsFilter', function() {
  return function(items, props) {
    var out = [];
    if (angular.isArray(items)) {
      items.forEach(function(item) {
        var keys = Object.keys(props);
        var prop = keys[0];
        var text = props[prop].toLowerCase();
        if (item[prop].toString().toLowerCase().indexOf(text) === 0) {
          out.push(item);
        }
      });
    } else {
      out = items;
    }
    return out;
  };
});

并将此过滤器传递到 select2 声明中:

<ui-select ng-model="person.selected" theme="select2" class="form-control" title="Choose a person">
            <ui-select-match placeholder="Select or search a person in the list...">{{$select.selected.name}}</ui-select-match>
            <ui-select-choices repeat="item in people | propsFilter: {name:$select.search}">
              <div ng-bind-html="item.name | highlight: $select.search"></div>
              <small ng-bind-html="item.email | highlight: $select.search"></small>
            </ui-select-choices>
          </ui-select>

Here是您可以引用的 plunkr 链接:

关于AngularJS 选择 - 匹配第一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31850457/

相关文章:

mysql:子查询返回多于1行

angularjs - 错误 : [$resource:badcfg] Error in resource configuration for action `query` . 预期响应包含一个数组但得到一个对象

javascript - 指令需要在元素呈现后更新数据

angularjs - Angular 全能自动完成

node.js - Algolia 从搜索结果中删除 _highlightResult

jQuery 选择/取消选择所有同级

php - MySQL 动态选择框

javascript - Angular 错误 : [$injector:unpr] while adding ui. Bootstrap

angularjs - 是否可以在没有 ui-sref 的情况下使用 ui-sref-active ?

php - PostgreSQL - 树组织