【发布时间】:2015-01-22 02:35:30
【问题描述】:
情况:
我有一个 Angular 应用程序,它使用 angular ui-select 从数据库中搜索和选择人员。
它工作正常,除了一件事。 用户应该能够使用两个条件在人员中进行过滤:姓名和电子邮件。
使用普通的角度过滤器,我只能过滤其中一个。
如果我尝试过滤这两个字段,它就不再起作用了。
一个字段的工作示例:
<ui-select multiple ng-model="database_people.selectedPeople" theme="select2" ng-disabled="disabled" style="width:100%">
<ui-select-match placeholder="Select person...">{{$item.name}} < {{$item.email}} ></ui-select-match>
<ui-select-choices repeat="person2 in list_people | filter: {name: $select.search, db_data_type_id: 5}">
<div ng-bind-html="person2.name | highlight: $select.search"></div>
<small>
email: <span ng-bind-html="''+person2.email | highlight: $select.search"></span>
</small>
</ui-select-choices>
</ui-select>
过滤器中有两个字段的示例无效:
<ui-select multiple ng-model="database_people.selectedPeople" theme="select2" ng-disabled="disabled" style="width:100%">
<ui-select-match placeholder="Select person...">{{$item.name}} < {{$item.email}} ></ui-select-match>
<ui-select-choices repeat="person2 in list_people | filter: {name: $select.search, email: $select.search, db_data_type_id: 5}">
<div ng-bind-html="person2.name | highlight: $select.search"></div>
<small>
email: <span ng-bind-html="''+person2.email | highlight: $select.search"></span>
</small>
</ui-select-choices>
</ui-select>
奇怪的是它实际上只对第一个字符有效。 当我键入第一个字符时,它会在名称和电子邮件这两个字段中突出显示它。 但是当我输入第二个字符时它不再起作用了 (我在控制台中没有错误)。
尝试使用来自 ANGULAR SAMPLES 的 PROPSFILTER:
<ui-select multiple ng-model="database_people.selectedPeople" theme="select2" ng-disabled="disabled" style="width:100%">
<ui-select-match placeholder="Select person...">{{$item.name}} < {{$item.email}} ></ui-select-match>
<ui-select-choices repeat="person2 in list_people | propsFilter: {name: $select.search, email: $select.search, db_data_type_id: 5}">
<div ng-bind-html="person2.name | highlight: $select.search"></div>
<small>
email: <span ng-bind-html="''+person2.email | highlight: $select.search"></span>
</small>
</ui-select-choices>
</ui-select>
在这种情况下,它完全崩溃了,select2 中没有数据了,我在控制台中遇到了一些错误:
Cannot read property 'toString' of null
Cannot read property 'length' of undefined
问题:
如何在多个字段中进行筛选? 我可以使用普通过滤器做到这一点吗? 或者我必须使用自定义过滤器? 但是在这种情况下,为什么不能正常工作?
非常感谢!
【问题讨论】:
-
@hanu 只是想知道,您是否为 propsFilter 实现了自定义 javascript 函数?它不会单独工作 (see line 83)。我还没有尝试使用它,但似乎很多人不知道他们必须包含 javascript 代码。
-
是的,它必须包含在指令中。谢谢指出。
标签: javascript angularjs filter angular-ui