Hello Aimery, how are you?
So to make a group without an index to make the filter, eg we will group by
category (dogs, cats, horses) I made an example of code to use the way you
quoted above:
myApp.controller('MainController', function($scope) {
$scope.values = [1,2,3,4,5,6,7,8,9,10];
$scope.grouped = [];
var i,t, j = $scope.values.length, chunk = 4;
for ( i = 0; i < j; i+=chunk ) {
$scope.grouped.push($scope.values.slice(i,i+chunk));
}
});
And can display as follows:
<body ng-app="myApp" ng-controller="MainController">
<div class="group">
<span ng-repeat="(key,value) in grouped">
<div>
<span ng-repeat="(key,each) in value">
{{ each }}
</span>
</div>
</span>
</div>
</body>
But I suggest you return a server object already with the categories to
which they belong, for example:
$scope.animals = [
{name: 'Mia', category: 'cat'},
{name: 'Bugg', category: 'dog'},
{name: 'Kit', category: 'dog'},
{name: 'Kie', category: 'cat'},
{name: 'Mon', category: 'dog'}
];
With this, you can use an angular filter called groupBy, example:
<span ng-repeat="(key, value) in animals | groupBy: 'category'">
Category: {{ key }}
<li ng-repeat="animal in value">
Animal Name: {{ animal.name }}
</li>
</span>
2015-03-13 13:16 GMT-03:00 Aimery Marsily <[email protected]>:
> Hi !
>
> I need to iterate through an array of 50 values with ng-repeat but I need
> to iterate on every 4 values grouped. My aim is to have something like :
> <div>
> <span>1</span>
> <span>2</span>
> <span>3</span>
> <span>4</span>
> </div>
> <div>
> <span>5</span>
> <span>6</span>
> <span>7</span>
> <span>8</span>
> </div>
>
> So for that I need to nest 2 ng-repeats. In PHP I'll use foreach
> (array_chunk($array->datas, 4) as $i => $datas) and then an other foreach
> on the $datas. But in AngularJS I realy don't know how to do so. Any ideas ?
>
> Here is a basic plunker to work with :
> http://plnkr.co/edit/FfisnfVYhiahBxGARdgN?p=preview
>
> Thanks
>
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.