pelican_george
pelican_george

Reputation: 971

Filtering in Angular JS by map keys

Consider the following JSON structure: UPDATED:

[
    {
        "game0001": {
            "-JfuVKIsUBH27DMJfWmL": {
                "action": "STARTFIRSTHALF",
                "team": "HOME"
            },
            "-JfuVLJGMgclLZ0Maduh": {
                "action": "GOAL",
                "team": "AWAY"
            }
        },
        "$id": "events",
        "$priority": null
    },
    {
        "game0001": {
            "gameRunning": false,
            "startDate": "17/01/2015 17:27:42 PM"
        },
        "game0002": {
            "gameRunning": true,
            "startDate": "17/01/2015 19:45:59 PM"
        },
        "game0003": {
            "gameRunning": false,
            "scheduledDate": "17/01/2014 12:30:00 PM"
        },
        "$id": "games",
        "$priority": null
    }
]

How can I achieve filtering in AngularJS in HTML? In a very basic way, what I'm trying to achieve is the following:

<div ng-repeat="game in games">
  <div ng-repeat="event in events | filter:game">
    {{event.name}} - {{game.name}}
  </div>
</div>

I have 2 maps games and events which share the same keys, e.g (game0001, game0002)

While repeating the games, I would like to have a inner repeater for events and filter only the ones sharing the same key/id.

Upvotes: 0

Views: 100

Answers (1)

Peter Ashwell
Peter Ashwell

Reputation: 4302

Here's a working plunkr, I made assumptions about the data you wanted to fetch:

http://plnkr.co/edit/DVwQaRZeZiagGEzY4lCy?p=preview

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

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.games = {
    "games": {
      "game0001": {
        "first": "a",
        "second": "b"
      },
      "game0002": {
        "first": "c",
        "second": "d"
      }
    }
  }
  $scope.gamesKeys = Object.keys($scope.games['games']);

  $scope.events = {
    "events": {
      "game0001": {
        "event": {
          "key": "a"
        },
        "event": {
          "key": "b"
        },
        "event": {
          "key": "c"
        }
      },
      "game0002": {
        "event": {
          "key": "a"
        },
        "event": {
          "key": "b"
        },
        "event": {
          "key": "c"
        }
      }
    }
  }
  $scope.eventsKeys = Object.keys($scope.events['events']);
});

The important part is the ng-repeat here:

  <body ng-controller="MainCtrl">
    <div ng-repeat="gameKey in gamesKeys">

      <div ng-repeat="eventKey in eventsKeys">
        event: {{events['events'][eventKey].event.key}} - game: {{games['games'][gameKey].first}}
      </div>
    </div>
  </body>

Upvotes: 1

Related Questions