Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Row every after 2 item in Angular ng-repeat - Ionic Grid

I need to create a strcuture as below in my app through ng-repeat.

<div class="row">
  <div class="col-50">1</div>
  <div class="col-50">2</div>
</div>
<div class="row">
  <div class="col-50">3</div>
  <div class="col-50">4</div>
</div>

Right now my code is as below:

<div class="row">
  <label class="item item-radio col col-50" ng-repeat="a in question.answer">
  <input type="radio" name="answers"   class="a-{{ a.correct }}" value="{{ a.correct }}" ng-click='ansValue("{{ a.correct }}")'> 

  <div class="item-content">
     <img src="img/ans/{{ a.option }}" />
  </div>

  <i class="radio-icon ion-checkmark"></i>
  </label>
</div>

But in the above code, its just a single row tag that I have. I wan't to somehow get the row tag contain/wrap every 2 items in the loop. What is the best way to achieve this?

Ref: Ionic Grid Doc

like image 421
esafwan Avatar asked Nov 01 '14 09:11

esafwan


5 Answers

I managed to do it using $even.

<div ng-repeat="number in numbers">

    <div class="row" ng-if="$even">
        <div class="col col-50">{{numbers[$index]}}</div>
        <div class="col col-50">{{numbers[$index + 1]}}</div>
    </div>

</div>

Here's a working JSFiddle.

like image 105
Patrick Reck Avatar answered Nov 09 '22 05:11

Patrick Reck


The solution from @Patrick Reck is excellent, but it forces you to repeat your code twice,

I suggest this improvement:

    <div ng-repeat="number in numbers">    
        <div class="row" ng-if="$even">
            <div class="col col-50" 
                 ng-repeat="num in [numbers[$index],numbers[$index + 1]]">
                {{num}}
            </div>
        </div>
    </div>

this way you will write your code one time as if it is a normal ng-repeat

like image 24
Aladdin Mhemed Avatar answered Nov 09 '22 06:11

Aladdin Mhemed


You can add flex-wrap: wrap to class row

http://jsfiddle.net/0momap0n/99/

<div ng-controller="MyCtrl">
    <div class="row" style="flex-wrap: wrap">
    <div class="col col-50" ng-repeat="number in numbers">{{number}}</div>
</div>  

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.numbers = [1, 2, 3, 4, 5, 6];
}
like image 6
Zao Avatar answered Nov 09 '22 06:11

Zao


I would write it like this, you can increase the $index % 3 to match the number of columns that you would like, this one will create 3 columns ...

<div class="row">
   <div class="" ng-repeat="i in range(1, 9)">
     <div class="clearfix" ng-if="$index % 3 == 0"></div>

       <div class="col">  
         <h1>{{ i }}</h1>
       </div>

     </div>
</div>
like image 3
Ricky Levi Avatar answered Nov 09 '22 05:11

Ricky Levi


This solution is using flex-flow and solves this brilliantly:

CSS

 .container {
    display: flex;
    flex-flow: row wrap;
  }
  .item {
    width: 50%;
  }

HTML

    <div class="container">
   <div class="item" *ngFor="let number of numbers">
      {{number}}
  </div>
like image 2
sixten Avatar answered Nov 09 '22 05:11

sixten