Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with ng-repeat angular js

I have an angular ng-repeat like bellow,

<div class="row" >
<div class="col-md-6" ng-repeat="(index,data) in mydata"> 
  <-- my content -->
</div> 
</div>

This will create output like below,

<div class="row" >
<div class="col-md-6" ng-repeat="(index,data) in mydata"> 
  <-- my content -->
</div> 
 <div class="col-md-6" ng-repeat="(index,data) in mydata"> 
  <-- my content -->
</div> 
 <div class="col-md-6" ng-repeat="(index,data) in mydata"> 
  <-- my content -->
</div> 
-----------------------
----------------- Etc.
</div>

But i need to repeat <div class="row" > also which contain two <div class="col-md-6" in each row.

This output needs like

<div class="row" >
<div class="col-md-6" ng-repeat="(index,data) in mydata"> 
  <-- my content -->
</div> 
 <div class="col-md-6" ng-repeat="(index,data) in mydata"> 
  <-- my content -->
</div> 
</div>
 <div class="row" >
<div class="col-md-6" ng-repeat="(index,data) in mydata"> 
  <-- my content -->
</div> 
 <div class="col-md-6" ng-repeat="(index,data) in mydata"> 
  <-- my content -->
</div> 
</div>
<div class="row" >
<div class="col-md-6" ng-repeat="(index,data) in mydata"> 
  <-- my content -->
</div> 
 <div class="col-md-6" ng-repeat="(index,data) in mydata"> 
  <-- my content -->
</div> 
</div>
------- Etc

Is it possible to do with this usingng-repeat?

like image 961
Shijin TR Avatar asked Jun 29 '15 11:06

Shijin TR


1 Answers

As mentioned in comment, if you want your row to repeat with each element in mydata, you would need to put ng-repeat on the <div> that contains row.

Its upto you to decide if you want to hardcode the inner <div> like this:

<div class="row" ng-repeat="data in mydata">
  <div class="col-xs-6"> {{data.index}} </div>
  <div class="col-xs-6"> {{data.value}} </div>
</div>

or use another ng-repeat on it.

<div class="row" ng-repeat="(index,data) in mydata">
  <div class="col-xs-6" ng-repeat="i in data"> {{i}} </div>
</div> 

Where mydata is an array of json with following structure:

$scope.mydata = [{index:0,value:'hello'},
                 {index:1,value:'hello1'},
                 {index:2,value:'hello2'}
                ]

here's the plunkr

UPDATE:

If you have data like following,

$scope.mydata = [{value:'hello0'},
                 {value:'hello1'},
                 {value:'hello2'},
                 {value:'hello3'}];

and you want to display it like

hello0 hello1

hello2 hello3

in the view, then you would need to make a check for elements occurring at even iterations and print elements at $index and $index+1. I used $even for the same. but you can also create a custom filter.

<div class="row" ng-repeat="data in mydata">
  <div ng-if="$even">
    <div class="col-xs-6" > {{mydata[$index].value}} </div>
    <div class="col-xs-6" > {{mydata[$index + 1].value}} </div>
  </div>
</div>    

Heres the updated plunker

like image 130
nalinc Avatar answered Sep 30 '22 14:09

nalinc