3

I'm trying to acheive the same behavior as the spring code below:

<c:forEach items="${data}" var="data" varStatus="contador">
    <c:if test="${(contador.count-1)%3==0}">
        <div class="conjunto-${conjunto} row"> <!-- Show opening div -->
    </c:if>

        <!-- Some HTML goes here -->

     <c:if test="${((contador.count-1)%3==2)}">
         </div>
    </c:if>
</c:forEach>

Explaining: I want a new div from class row only after 3 other HTML elements have been added. I have tried this with ng-if, like this:

<div ng-repeat="data in DATA">
    <div class="conjunto-{{$index/3}} row" ng-show="$index % 3 == 0" ng-include="'html.html'">
    </div>

    <div ng-show="$index % 3 != 0" ng-include="'html.html'">
    </div>
</div>

But it obviously doesnt work because only one element with be inside de div.row.

Is there an if-clause with which I could add only the opening div and then close it later?

Thanks in advance.

1 Answers1

0

Ok the best way to do this IMO is 2 fold, in your controller have a method similar to this

$scope.createDataChunks = function() {
    var a = $scope.DATA,
        retArr = [];
    while(a.length) {
       retArr.push(a.splice(0,3));
    }

    return retArr;
}

This would give you an array of arrays, each containing 3 (or less) items in it, you then have this as your markup

<div ng-repeat="data in createDataChunks()">
    <div class="conjunto-{{$index/3}} row" ng-show="$index % 3 == 0" ng-include="'html.html'">
        <!-- put custom HTML here -->
    </div>
</div>

I think that's roughly what you are after?

DrogoNevets
  • 1,456
  • 3
  • 18
  • 34
  • Hummmm... Thanks for the answer, Drogo. I will try it. But there's a problem that I didnt say, sorry. The `html.html` is the "custom HTML" you said there. And it has some data to be filled depending on the ng-repeat iteration. Do you think it will still work? Thanks a lot – Oliver John Nov 21 '14 at 12:11
  • just ignore the custom HTML bit, it should work using your external html file – DrogoNevets Nov 25 '14 at 09:30