Nadav Shabtai
Nadav Shabtai

Reputation: 687

nested ng-repeat with one index

I have this code, and I want to have one index for all the items in the second ng-repeat Right now the indexes looks like this:

0 0
0 1
0 2
1 0
1 1
1 2

And I want them to look like this:

0 0
0 1
0 2
1 3
1 4
1 5

And if I searh "qwe" I want it to look like this

0 0
0 1 
0 2

How can I achieve that (Please notice the ng-if that filters the text)?

var app = angular.module('github', []);
app.controller('githubRepos', ['$scope','$http', function($scope, $http){  
  $scope.text = '';
   $scope.groups = {
     1: ['asd', 'asd1', 'asd2'],
     2: ['qwe', 'qwe1', 'qwe2']
   }
  
  $scope.show = function(text) {
    return !$scope.text || text.indexOf($scope.text) !== -1;
  }
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="github">
  <div ng-controller="githubRepos">
    <input type="text" ng-model="text" />
    <div ng-repeat="group in groups track by $index" ng-init="parentIndex = $index">
      <div ng-repeat="item in group track by $index" ng-if="show(item)">
        {{parentIndex}} {{$index}} {{item}}
      </div>
    </div>
  </div>
</div>

Upvotes: 1

Views: 79

Answers (2)

Farinaz
Farinaz

Reputation: 375

you can use this:

{{parentIndex}} {{$index + (group.length * parentIndex)}} {{item}}

Upvotes: 4

Biswa Bandhu Bhandary
Biswa Bandhu Bhandary

Reputation: 59

    <div ng-app="github">
  <div ng-controller="githubRepos">
    <input type="text" ng-model="text" />
    <div ng-repeat="group in groups track by $index" ng-init="parentIndex = $index">
      <div ng-repeat="item in group track by $index" ng-if="show(item)">
        {{parentIndex}} {{$index + group.length * parentIndex}} {{item}}
      </div>
    </div>
  </div>
</div>

If you have any kind of problem with it comment on it

Upvotes: 2

Related Questions