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

Answers:

Answer

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>
Answer

rowspan will do everything you need. Follow this link:

w3schools This is one of the best resources for web development.

Answer

Using div u can also do

<div ng-repeat = "user in users">
<div> {{user.id}} </div>
  <div ng-repeat = "user1 in users.subjects">
     {{user1.id}} : {{user1.name}}
  <div>
</div>
Answer

Donal's answer is good. But I edited to show beautiful table:

<table class="table table-bordered table-hover table-height m-b-xs">
<thead>
    <tr>
        <td>ID</td>
        <td>Subjects</td>
        <td>name</td>
    </tr>
</thead>
<tbody>
    <tr ng-repeat-start="user in users">
        <td rowspan="{{user.subjects.length}}">key {{user.id}}</td>
        <td>S{{ user.subjects[0].id }}</td>
        <td>{{ user.subjects[0].name }}</td>
    </tr>
    <tr ng-repeat-end ng-repeat="subject in user.subjects.slice(1)">
        <td>S{{subject.id}}</td>
        <td>{{subject.name}}</td>
    </tr>
</tbody>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.