简体   繁体   中英

How to define custom filter for ui-select module in AngularJS?

Here is the code from select-field in html:

<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>

Then the field who's important to filter the table list:

<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>

I'm using the ngModel value from ui-select-tag in the custom filter selectGroup as parameter for the filter.

The nameslist values are coming from the database. Here the JSON result of a GET Request by QR_Group DB-Table:

{
  "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"
}

This JSON format I'm getting from the WebAPI method GetGroup. How can I display the QR_Name array on the table list with the custom filter?

This was my first thought:

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);
            }
        });
    }
}]);

But the filter doesn't work correctly.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM