Question

I'm using Angular and ng-repeat as well as Semantic UI Grid layout.

I'm trying to display 5 columns in a row. When the fifth item is reached create a new row. The below creates a new row and column for each item in the data. I can see there is an index property with ng-repeat but not sure how to basically say if mod 5 == 0 output a new row element else just output 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

Was it helpful?

Solution

The cleanest and most maintainable way would be in my opinion to write a directive. In your HTML you simply have:

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

And in your JS you break your data into the correct structure within the controller and output it using the template:

    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
      };
  });

OTHER TIPS

Here's one way to approach this that uses a range filter.

We calculate how many rows you'll need (data.results.length/5) and then iterate over that number of rows using ng-repeat with the range filter.

Then within each row we slice out the columns needed for that row and create those columns with another 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>

and the associated range filter (from: 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;
};

Here's a fiddle of this: http://jsfiddle.net/BMrNs/1/

Try using ng-switch in combination with Array.slice() It's not the most efficient method but if you don't have any way to meddle with data representation, 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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top