简体   繁体   English

AngularJS ng-repeat过滤器特定的嵌套属性

[英]AngularJS ng-repeat filter specific nested property

I am trying to filter an ngRepeat by a specific array attribute. 我正在尝试通过特定的数组属性过滤ngRepeat。

js: js:

app.controller('UserIndexController', function ($scope, $http, $window) {

    $scope.search = {};

    $http({ method: 'GET', url: '/api/v2/group' })
        .success(function(data, status, headers, config) {

            $scope.groups = data;

        })
        .error(function(data, status, headers, config) {

        });

    $http({ method: 'GET', url: '/api/v2/user' })
        .success(function(data, status, headers, config) {

            $scope.users = data;

        })
        .error(function(data, status, headers, config) {

        });
});

html: 的HTML:

<div data-ng-controller="UserIndexController">
<select 
    ng-model="search.group" 
    ng-options="group.name as group.name for group in groups" 
    class="form-control">
    <option value="">Filter Users by Group</option>
</select>

<table class="table table-responsive table-striped table-hover">
        <thead>
    <tr>
            <th>User Type</th>
            <th>Email</th>
        <th>Name</th>
            </tr>
        </thead>
        <tbody>
        <tr data-ng-repeat="user in users | filter:search.group">
        <td><% user.groups[0].name %></td>
        <td><% user.last_name %>, <% user.first_name %></td>
        <td><% user.email %></td>
    </tr>
    </tbody>
    </table>
</div>

The above code searches through the entire user object. 上面的代码搜索整个用户对象。 This behaviour is not wanted because if any other user data attributes match the string in the selected option, then this row will also be included in the filter. 不需要此行为,因为如果任何其他user数据属性与所选选项中的字符串匹配,则此行也将包含在过滤器中。

How do I ensure that the filter only works on the user.groups[0].name column? 如何确保过滤器仅在user.groups[0].name列上起作用?

<tr data-ng-repeat="user in users | filter:{user.groups[0].name: search.group}"> results in the following error: Syntax Error: Token '[' is unexpected . <tr data-ng-repeat="user in users | filter:{user.groups[0].name: search.group}">导致以下错误: Syntax Error: Token '[' is unexpected

As such, I need something to either put user.groups[0].name directly into the user object so that the filter doesn't have to access it via the array, or a way of filtering on the array object. 因此,我需要将user.groups[0].name直接放入用户对象中,以便过滤器不必通过数组访问它,也不需要对数组对象进行过滤的方法。

You can use a custom filter function: 您可以使用自定义过滤器功能:

$scope.groupMatcher = function(groupFilter) {
  return function(user) {
    return user.groups[0].name === groupFilter;
  }
};

And use it: 并使用它:

<ul>
  <li ng-repeat="user in users | filter:groupMatcher('2')">
    {{user.name}}
  </li>
</ul>

If you need this more often define it as a custom filter in your module 如果您更需要此功能,请将其定义为模块中的自定义过滤器

First of all: The error is selfspeaking. 首先:错误是自言自语。 Filter expects an object to check the items passed to the filter against. 过滤器要求对象检查传递给过滤器的项目。 You cant use [] in object property names. 您不能在对象属性名称中使用[]

About your problem: So you want to show all users, that belong to a specific group. 关于您的问题:因此,您想显示属于特定组的所有用户。 Maybe with your own filter ? 也许使用您自己的过滤器?

yourApp.filter("UserByGroupNameFilter", function ()
{                
    return function (arrUsers, groupName)
    {
        var resultUsers = [];
        for(var i in arrUsers){
          var user = arrUsers[i];                
            /* Loop for all columns
               for(var j in user.groups){
                 if(user.groups[j].name == groupName){
                     resultUsers.push(user);
                     break;
                }
            } 
            */
          if(user.groups[0].name == groupName){
               resultUsers.push(user);
          }
        }     
        return resultUsers;
    };
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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