如何在AngularJS中为ui-select模块定义自定义过滤器?

时间:2015-07-01 23:19:35

标签: javascript json angularjs angularjs-filter ui-select

以下是html中select-field的代码:

<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-tag中的ngModel值作为过滤器的参数。

名单列表值来自数据库。这里是QR_Group DB-Table的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"
}

我从WebAPI方法GetGroup获取此JSON格式。如何使用自定义过滤器在表列表中显示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);
            }
        });
    }
}]);

但是过滤器无法正常工作。

1 个答案:

答案 0 :(得分:0)

angular ui-select添加了对0.12中自定义过滤器的支持,其中包含了group-filter属性 - https://github.com/angular-ui/ui-select/pull/836