Angular filter - group by object (or several fields)

I expanded Angular's filter group as follows: transforming a command into an object.

     var app = angular.module('myApp',['angular.filter']);

app.controller('MainController', ['$scope', function($scope){
  $scope.players = [
  {
    name: 'Gene', 
    team: {
      'id' : '1',
      'name' : 'alpha'
      
    }
  },
  {
    name: 'George', 
    team: {
      'id' : '2',
      'name' : 'beta'
    }
  },
  {
    name: 'Steve', 
    team: {
      'id' : '3',
      'name' : 'gamma'
    }
  },
  {
    name: 'Paula', 
    team: {
      'id' : '2',
      'name' : 'beta'
    }
  },
  {
    name: 'Scruath', 
    team: {
      'id' : '3',
      'name' : 'gamma'
    }
  }
];
}]);


  
<!DOCTYPE html>
<html>

  <head>
    <script data-require="angularjs@1.6.2" data-semver="1.6.2" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.js"></script>
    <script data-require="angular-filter@*" data-semver="0.5.7" src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.7/angular-filter.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-app="myApp">
    <div ng-controller="MainController">
      <ul>
        <li ng-repeat="(team, players) in players | groupBy: 'team.name'">
          <a href="#I need the team ID">Group name: {{ team }}</a>
          <ul>
            <li ng-repeat="player in players">
              player: {{ player.name }}
            </li>
          </ul>
        </li>
      </ul>
    </div>
  </body>

</html>
Run codeHide result

But what if I need a team identifier in a group? What can I do?

<a href="#I need the team ID">Group name: {{ team }}</a>

I tried to group the command object and use team.nameand team.id, but that didn't work. In addition, I did not know how to create a group using several fields(team.id, team.name)

Here is the working plnkr

+4
source share
2 answers

I have a simple solution:

I am grouped by team.id

<li ng-repeat="(teamid, players) in players | groupBy: 'team.id'">

Then I used: players[0].team.nameinside the group

<li ng-repeat="(teamid, players) in players | groupBy: 'team.id'">
    <a href="#Here I can use the group teamid">Group name: {{ players[0].team.name }}</a>
      <ul>
        <li ng-repeat="player in players">
          player: {{ player.name }}
        </li>
     </ul>
</li>

players , , , players[0], players[1] .. .

     var app = angular.module('myApp',['angular.filter']);

app.controller('MainController', ['$scope', function($scope){
  $scope.players = [
  {
    name: 'Gene', 
    team: {
      'id' : '1',
      'name' : 'alpha'
      
    }
  },
  {
    name: 'George', 
    team: {
      'id' : '2',
      'name' : 'beta'
    }
  },
  {
    name: 'Steve', 
    team: {
      'id' : '3',
      'name' : 'gamma'
    }
  },
  {
    name: 'Paula', 
    team: {
      'id' : '2',
      'name' : 'beta'
    }
  },
  {
    name: 'Scruath', 
    team: {
      'id' : '3',
      'name' : 'gamma'
    }
  }
];
}]);


  
<!DOCTYPE html>
<html>

  <head>
    <script data-require="angularjs@1.6.2" data-semver="1.6.2" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.js"></script>
    <script data-require="angular-filter@*" data-semver="0.5.7" src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.7/angular-filter.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-app="myApp">
    <div ng-controller="MainController">
      <ul>
        <li ng-repeat="(teamid, players) in players | groupBy: 'team.id'">
          <a href="#Here I can use the group teamid">Group name: {{ players[0].team.name }}</a>
          <ul>
            <li ng-repeat="player in players">
              player: {{ player.name }}
            </li>
          </ul>
        </li>
      </ul>
    </div>
  </body>

</html>
Hide result
+4

team.id, . , . , . , team.id -

<body ng-app="myApp">
<div ng-controller="MainController">
    <ul>
        <li ng-repeat="(team, players) in players | groupBy: 'team.id'">
            <a href="{{ team }} ">Group name: {{ teams[team].name }}</a>
            <ul>
                <li ng-repeat="player in players">
                    player: {{ player.name }}
                </li>
            </ul>
        </li>
    </ul>
</div>
</body>

AngularJS

var app = angular.module('myApp',['angular.filter']);

app.controller('MainController', ['$scope', function($scope){

    $scope.players = [
        {
            name: 'Gene',
            team: {
                'id' : '1',
                'name' : 'alpha'

            }
        }, {
            name: 'George',
            team: {
                'id' : '2',
                'name' : 'beta'
            }
        }, {
            name: 'Steve',
            team: {
                'id' : '3',
                'name' : 'gamma'
            }
        }, {
            name: 'Paula',
            team: {
                'id' : '4',
                'name' : 'beta'
            }
        }, {
            name: 'Scruath',
            team: {
                'id' : '5',
                'name' : 'gamma'
            }
        }];

    $scope.teams = {};

    $scope.players.forEach(function (player) {
        if (angular.isUndefined($scope.teams[player.team.id])) {
            $scope.teams[player.team.id] = player.team;
        }
    });
}]);
+1

Source: https://habr.com/ru/post/1673858/


All Articles