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/