Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular.js ng-repeat for creating grid

I'm trying to create a grid using bootstrap 3 and angularjs.

The grid I'm trying to create is this, repeated using ng-repeat.

<div class="row">  <div class="col-md-4">item</div>  <div class="col-md-4">item</div>  <div class="col-md-4">item</div> </div> 

I've tried using ng-if with ($index % 3 == 0) to add the rows, but this doesn't seem to be working right. Any suggestions would be great!

Thank you!

EDIT: Here's the code I ended up going with that worked:

<div ng-repeat="item in items">   <div ng-class="row|($index % 3 == 0)">     <ng-include class="col-sm-4" src="'views/items/item'"></ng-include>    </div> </div> 
like image 219
dzm Avatar asked Nov 22 '13 05:11

dzm


People also ask

Does ng-repeat create a new scope?

Directives that Create Scopes In most cases, directives and scopes interact but do not create new instances of scope. However, some directives, such as ng-controller and ng-repeat, create new child scopes and attach the child scope to the corresponding DOM element.

How do you use NG-repeat in a table?

Definition and UsageThe ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.

How do I create an index in NG-repeat?

Note: The $index variable is used to get the Index of the Row created by ng-repeat directive. Each row of the HTML Table consists of a Button which has been assigned ng-click directive. The $index variable is passed as parameter to the GetRowIndex function.

What is difference between data ng-repeat and Ng repeat?

The data-ng-repeat allows the HTML to be validated through validators that do not understand Angular. The documentation is here with directives. This is from the docs: Angular normalizes an element's tag and attribute name to determine which elements match which directives.


1 Answers

The accepted answer is the obvious solution however presentation logic should remain in view and not in controllers or models. Also I wasn't able to get the OP's solution to work.

Here are two ways to do create grid system when you have a flat list(array) of items. Say our item list is a alphabet:

Plunker here

$scope.alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',                     'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; 

Method 1:

This is a pure angular solution.

<div class="row" ng-repeat="letter in alphabet track by $index" ng-if="$index % 4 == 0">   <div class="col-xs-3 letter-box"         ng-repeat="i in [$index, $index + 1, $index + 2, $index + 3]"         ng-if="alphabet[i] != null">     <div>Letter {{i + 1}} is: <b> {{alphabet[i]}}</b></div>   </div> </div> 

The outer loop execute after every 4 iterations and creates a row. For each run of the outer loop the inner loop iterates 4 times and creates columns. Since the inner loop runs 4 times regardless of whether we have elements in array or not, the ng-if makes sure that no extraneous cols are created if the array ends before inner loop completes.

Method 2:

This is much simpler solution but requires angular-filter library.

<div class="row" ng-repeat="letters in alphabet | chunkBy:4">   <div class="col-xs-3 letter-box" ng-repeat="letter in letters" >     <div>Letter {{$index + 1}} is: <b> {{letter}}</b></div>   </div> </div> 

The outer loop creates groups of 4 letters, corresponding to our 'row'

[['A', 'B', 'C', 'D'], ['E', 'F', 'G', 'H'], ... ] 

The inner loop iterates over the group and creates columns.

Note: Method 2 might require evaluation of filter for each iteration of outer loop, hence method 2 may not scale very well for huge data sets.

like image 128
CodeExpress Avatar answered Sep 27 '22 22:09

CodeExpress