Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add multiple rows in one column using angular ng-repeat

I want to generate table contents using json object. I am using ng-repeat to insert multiple rows in a table. But I have to generate table like the following.

    --------------
    ID | subjects 
    --------------
       | S1  
    1  | S2  
       | S3  
    --------------
       | S4  
    2  | S5  
       | S6
    --------------


my angular code is: 
 <tr ng-repeat = "user in users">
    <td>{{user.id}}</td>
    <td>{{user.subject}}</td>
</tr>

my json object is :

user:[
  {id:1 , 
   subjects:[
       {id:1 , name:"eng"} 
       {id:2 , name:"phy"}
   ]
  },
  {id:2 , 
   subjects:[
       {id:1 , name:"eng"} 
       {id:3 , name:"math"}
   ]
  }
]

I want to generate html table like this

 <table>
  <tr>
     <td >ID</td>
     <td>Sub</td>
  </tr>
  <tr>
    <td rowspan="3">1</td>
    <td>S1</td>
  </tr>
  <tr>
    <td>S2</td>
  </tr>
  <tr>
    <td>S3</td>
   </tr> 

how to insert multiple rows in one column using angular

like image 362
Amina Yousuf Avatar asked Jan 09 '23 03:01

Amina Yousuf


1 Answers

Use ng-repeat-start and ng-repeat-end. For example:

<tr ng-repeat-start="user in users">
    <td rowspan="{{user.subjects.length+1}}">{{user.id}}</td>                    
</tr>
<tr ng-repeat-end ng-repeat="subject in user.subjects">
    <td>S{{subject.id}}</td>
</tr>

Here is a full example:

var app = angular.module('MyApp', []);
app.controller('MyController', function ($scope) {

    var users = [{
        id: 1,
        subjects: [{
            id: 1,
            name: "eng"
        }, {
            id: 2,
            name: "phy"
        }]
    }, {
        id: 2,
        subjects: [{
            id: 1,
            name: "eng"
        }, {
            id: 3,
            name: "math"
        }, {
            id: 4,
            name: "hist"
        }, 
        {
            id: 5,
            name: "geog"
        }]
    }];

    $scope.users = users;
});
table { border-collapse: collapse; }
td { border: 1px solid Black; }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<table ng-app="MyApp" ng-controller="MyController">
    <thead>
        <tr>
            <td>ID</td>
            <td>Subjects</td>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat-start="user in users">
            <td rowspan="{{user.subjects.length+1}}">{{user.id}}</td>                    
        </tr>
        <tr ng-repeat-end ng-repeat="subject in user.subjects">
            <td>S{{subject.id}}</td>
        </tr>
    </tbody>
</table>

Also, working fiddle here: http://jsfiddle.net/donal/r51d5fw5/17/

An alternative version, using nested ng-repeat, can be implemented using a div to display the nested subject information:

<tr ng-repeat="user in users">
    <td>{{user.id}}</td>
    <td>
        <div ng-repeat="subject in user.subjects">S{{subject.id}}</div>
    </td>
</tr>
like image 80
Donal Avatar answered Jan 10 '23 19:01

Donal