1

I have a list of divs and I want to achieve the following html example:

<div class="groups">
  <div class="group">Whatever 1</div>
  <div class="group">Whatever 2</div>
  <div class="group">Whatever 3</div>
</div>
<div class="groups">
  <div class="group">Whatever 4</div>
  <div class="group">Whatever 5</div>
  <div class="group">Whatever 6</div>
</div>

I have the following:

<div class="group" ng_repeat="group in groups">
    <div>{{group.name}}</div>
</div>

How can I encapsulate "group" divs inside a "groups" for every three elements? Also I want to add elements dynamically and have the same behavior.

2 Answers 2

2

EDIT: using a filter will not work do to infinite loop. Here is a solution with the filtering logic in the controller's watch function. It could be refactored and placed inside a service. But for the sake of simplicity it is not done here.

Here is a working Plunker: http://plnkr.co/edit/tINqbztypUqv674hvDbo

I would go with the solution proposed by @Mik378 augmented and manipulate data initially in your controller:

<div ng-controller="yourCtrl">

    <div class="groups" ng-repeat="groups in groupedArray">
        <div class="group" ng-repeat="group in groups">
            <div>{{group.name}}</div>
        </div>
    </div>
</div>

The controller:

angular.module('yourApp')
    .controller('yourCtrl', function ($scope) {
        $scope.tobeGroupedArray = [
            {name: 'group1'},
            {name: 'group2'},
            {name: 'group3'},
            {name: 'group4'},
            {name: 'group5'},
            {name: 'group6'},
            {name: 'group7'},
            {name: 'group8'},
            {name: 'group9'}
        ];

        $scope.$watch(
            'tobeGroupedArray',
            function (newVal) {
                if (!newVal) {
                    return;
                }
                $scope.groupedArray = [];
                newVal.forEach(function (group, idx) {
                    if (idx % 3 === 0) {
                        $scope.groupedArray.push([]);
                    }
                    $scope.groupedArray[$scope.groupedArray.length - 1].push(group);
                });
            },
            true
        );
    });
Sign up to request clarification or add additional context in comments.

7 Comments

Yes this one works but it is a bit too specific for the groups, I was hoping for a more global answer but it is ok for now.
You can imagine doing much more. You can wrap that code inside a directive and choose how much items are grouped together etc. Let me know if I can help
I'll try to do it with directive but since I am still new to angular I would like to see it, if it isn't too much of a trouble :)
Your solution works for this specific simple thing, but if I have something else inside the div (for example an input field) it creates a really bad user experience because it unfocuses due to watch.
Please share more code to illustrate this behaviour.
|
0

I presume you would have an array of groups, and each "groups" being an array. Why not the following:

<div class="groups" ng-repeat="groups in groupsArray">
  <div class="group" ng-repeat="group in groups">
      <div>{{group.name}}</div>
  </div>
</div>

5 Comments

I have an array of group objects inside the groups. This one doesn't include three per groups tag.
Why not manipulating your groups in your controller initially?
Can you give me an example?
Can you give me the kind of specific groups you have initially? (the javascript object)
Please see my answer and its working plunker: plnkr.co/edit/tINqbztypUqv674hvDbo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.