7

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?

Shijin TR
  • 7,516
  • 10
  • 55
  • 122
  • Its obvious that if you want your `row` to repeat with each element in `mydata`, you would need to put `ng-repeat` on the `
    ` that contains `row`. Choice of writing the inner `
    ` is upto you(weather to hardcode it or use another `ng-repeat`). Please mention full structure of `mydata` and the way you want to display it
    – nalinc Jun 29 '15 at 11:24
  • mydata contain large data, – Shijin TR Jun 29 '15 at 11:25
  • @Patrick Is my question is unclear? – Shijin TR Jun 29 '15 at 11:48
  • @Patrick All the answers are similar ,And that was the answer i expected.I spend one full day for this – Shijin TR Jun 29 '15 at 11:50

4 Answers4

4

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

nalinc
  • 7,375
  • 24
  • 33
  • How can i show data ?I don't want to create elements with dummy data – Shijin TR Jun 29 '15 at 11:46
  • What sort of data do you have ? I am using the following values for **mydata** in plunkr. `$scope.mydata = [{index:0,value:'hello'},{index:1,value:'hello1'},{index:2,value:'hello2'}]`. Can you give the structure of `mydata` (if possible, with 1-2 items) ? – nalinc Jun 29 '15 at 11:54
  • $scope.mydata = [{value:'hello'},{value:'hello1'},{value:'hello2'},{value:'hello3'}]; then hello - hello1 next row hello2-hello3 etc – Shijin TR Jun 29 '15 at 11:57
  • @Shijin: is [this](http://plnkr.co/edit/Yx89OC7q41YvuMh8ZMIl?p=preview) what you are looking for ? – nalinc Jun 29 '15 at 12:34
0

Create a directive:

angular.module(....)
.directive('myRows',function(){
  return {
    restrict : 'A',
    template : '<div class="row"><div class="col-md-6" ng-repeat="(index,data) in mydata"><-- my content --></div></div>'
  };
});    

assumption here is that mydata is defined on the scope of the surrounding controller. Use it in your html:

<div data-my-rows><div>
Jan Peter
  • 380
  • 6
  • 16
0

So, you now have two answers that correctly address your question and will produce the results you requested, but I wanted to give you some advice (albeit unsolicited) about your Bootstrap structure.

In Bootstrap, it is perfectly acceptable to not repeat your rows for every 12 grid units. In fact, if you want to use multiple responsive column classes together, such as using col-lg-4 and col-md-6 to produce 3 columns on large viewports and 2 on medium viewports, you cannot insert rows between your repeated content.

Since col classes use float and percentages, you don't have to worry about "terminating" the row. Elements will simply float next to one another up to 12 grid units and then start a new natural horizontal "row." That is how Bootstrap's responsive grid works.

You should use rows for two purposes:

  1. To nest columns inside of columns. See my answer here for a visual explanation of this topic. or
  2. To create horizontal groups of columns. This has the purpose of grouping elements together and clearing column floats for subsequent columns.

What I'm saying is that your initial ng-repeat structure was better and will provide you with a more flexible outcome.

Community
  • 1
  • 1
jme11
  • 17,134
  • 2
  • 38
  • 48
0

Yes ng-repeat is possible to do that.

Here is example code from my project

<tbody>
<tr ng-repeat="proj in projects>
<td>
{{proj.projectID}}
</td>
<td>
 {{proj.projectName}}
</td>
<td>
{{proj.project.start date  | date:'mediumDate'}}
 </td>
  <td>
  {{proj.projectStatus}}
  </td>
 </tr>
  </tbody>

I hope this would help

vijay kani
  • 140
  • 8