Using ng-repeat index to display different html

I use Angular and ng-repeat, as well as a semantic user interface layout .

I am trying to display 5 columns in a row. When the fifth element is created, create a new line. Below, a new row and column is created for each data item. I see that there is an index property with ng-repeat, but not sure how to basically say the if mod 5 == 0output of a new row element that just displays a new column.

<div class="ui grid">
    <div class="row" ng-repeat="item in data.results">
        <div class="column">
            <div class="ui segment">
                {{item.original_title}}
            </div>
        </div>
    </div>
</div>

thanks for the help

+4
source share
3 answers

, , . HTML :

<grid source="data.results" break="5" />

JS :

    angular.module('yourApp', [])
    .directive('grid', function() {
      return {
          restrict: 'E',
          scope: {
            break: '=break',
            source: '=source'
          },
          controller: function($scope) {
            var total = Math.ceil($scope.source.length / $scope.break);
            $scope.data = new Array(total);
            for (var i = 0; i < total; ++i) {
              $scope.data[i] = $scope.source.slice(i * $scope.break, (i + 1) * $scope.break);
            }
          },
          template:
            '<div class="ui grid">' +
            '  <div class="row" ng-repeat="row in data">' +
            '    <div class="column" ng-repeat="item in row">' +
            '      <div class="ui segment">' +
            '        {{item.original_title}}' +
            '      </div>' +
            '    </div>' +
            '  </div>' +
            '</div>',
          replace: true
      };
  });
+4

, .

, (data.results.length/5), , ng-repeat .

we slice , , ng-repeat.

<div class="row" ng-repeat="n in [] | range:(data.results.length/5)+1">
    <span ng-repeat='item in data.results.slice($index*5,($index*5+5))'>
        <div class="column">
           <div class="ui segment">
              {{item.original_title}}
           </div>
        </div>
    </span>
</div>

(: http://www.yearofmoo.com/2012/10/more-angularjs-magic-to-supercharge-your-webapp.html#more-about-loops):

app.filter('range', function () {
  return function (input, total) {
    total = parseInt(total);
    for (var i = 0; i < total; i++) {
        input.push(i);
    }
    return input;
};

: http://jsfiddle.net/BMrNs/1/

+3

Try using ng-switch in conjunction with Array.slice()This is not the most efficient method, but if you have no way to interfere with the presentation of the data, this is the best I can think of.

<div class="ui grid">
    <div ng-repeat="item in data.results">
        <div ng-switch on="$index % 5">
            <div ng-switch-when="0" class="row">
                <div ng-repeat="e in data.results.slice($index, $index + 5)">
                      <div class="column">
                          <div class="ui segment">
                              {{e.original_title}}
                          </div>
                      </div>
                </div>
            </div>
        </div>
    </div>
</div>
+2
source

All Articles