以下是html中select字段的代码:
<ui-select ng-model="group.selected" theme="selectize" ng-click="searchDisabled(3)" ng-disabled="disabledGroup">
<ui-select-match placeholder="Choose a group">
{{$select.selected.name}}
</ui-select-match>
<ui-select-choices repeat="group in groups | filter: $select.search">
<span ng-bind-html="group.name | highlight: $select.search"></span>
<small ng-bind-html="group.code | highlight: $select.search"></small>
</ui-select-choices>
</ui-select>
然后是筛选表列表的重要字段:
<tr ng-repeat="item in filteredNames = (nameslist | filter:search | selectGroup:group.selected)">
<td>{{ item.lname }}</td>
<td>{{ item.fname }}</td>
<td>{{ item.maxAge }}</td>
</tr>
我使用自定义筛选器selectGroup中的ui-select标记中的ngModel值作为筛选器的参数。
名称列表值来自数据库。这里是QR_Group DB表的GET请求的JSON结果:
{
"QR_Name":[
{ "Id":31, "lname":"Bricks", "fname":"Johnny", "maxAge":24, "QR_GroupId":6 },
{ "Id":4, "lname":"Schon", "fname":"Toni", "maxAge":54, "QR_GroupId":6 },
{ "Id":56, "lname":"Houston", "fname":"Monica", "maxAge":29, "QR_GroupId":6 },
],
"Id":6,
"name":"South America",
"code":"SA"
}
这个JSON格式是我从WebAPI方法GetGroup获得的。如何使用自定义筛选器在表列表上显示QR_Name数组?
这是我的第一个想法:
testApp.filter('selectGroup', ['$log', function ($log) {
return function (nameslist, group) {
group = group || '';
$log.info('fondslist:', fondslist);
$log.info('Filter for group:', group);
var selectList = [];
angular.forEach(fondslist, function (input) {
if (group == input.QR_Name['QR_GroupId']) {
selectList.push(input);
}
});
}
}]);
但是过滤器不能正常工作。
angular ui select在0.12中添加了对自定义过滤器的支持,并具有group filter属性-https://github.com/angular-ui/ui-select/pull/836