0

I am using angularjs, I have following set of data that I need to bind to a table using ng-repeat. I tried thus but I am not able to reach my output my data in sub array so I am not able to bind this in repeat, anyone help how to do this .

data

 $scope.SubData=[{
    Name: '1',
    Subject: [{
        tamil: 12,
        english: 21,
        social: 45
    }]
},
{
    Name: '2',
    Subject: [{
        tamil: 10,
        english: 20,
        social: 30,
        geo: 40
    }]
}]

Expected output should look like this

enter image description here

NOTE:Subject Names are Dynamically

5
  • Did you store it in $scope or using with var? :-p Commented Jun 12, 2017 at 11:08
  • @SaiUnique scope i update question Commented Jun 12, 2017 at 11:09
  • Show what you have tried so far. ie. HTML. So that we can correct your mistkes. Commented Jun 12, 2017 at 11:10
  • that subjects are dynamically i am not able to mention column names so i put it one one td whole subject Commented Jun 12, 2017 at 11:11
  • @jose i think you need to correct your data As $scope.SubData=[{Name:'1',Subject:[{SubjectName:'tamil',marks:12,}] something like this Commented Jun 12, 2017 at 11:23

4 Answers 4

2

You can do like this:

<table ng-repeat="stu in SubData">
  <tr>
     <td>
       {{stu.Name}}
     </td>
     <td ng-repeat="(key, val) in stu.Subject[0]">
        {{key}}:{{val}}
     </td>
  </tr>
</table> 

Here is the plunker

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

Try This, It's exactly what you need :

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<style>
    table,
    th,
    td {
        border: 1px solid black;
        border-collapse: collapse;
    }
    th,
    td {
        padding: 5px;
        text-align: left;
    }
    table#t01 tr:nth-child(even) {
        background-color: #eee;
    }
    table#t01 tr:nth-child(odd) {
        background-color: #fff;
    }
    table#t01 th {
        background-color: black;
        color: white;
    }
</style>

<body ng-app="myApp" ng-controller="myCtrl">

    <table border="1">
        <tr>
            <th>Name</th>
            <th>Subject</th>
        </tr>
        <tr ng-repeat="x in SubData">
            <td>{{ x.Name }}</td>
            <td><label ng-repeat="(key, value) in x.Subject"><label ng-repeat="(k,z) in value">{{k}} - {{z}}<label ng-show="!$last"> ,</label> <br></label></label>
            </td>
        </tr>
    </table>
    <script>
        var app = angular.module("myApp", []);
        app.controller("myCtrl", function($scope) {
            $scope.SubData = [{
                    Name: '1',
                    Subject: [{
                        tamil: 12,
                        english: 21,
                        social: 45
                    }]
                },
                {
                    Name: '2',
                    Subject: [{
                        tamil: 10,
                        english: 20,
                        social: 30,
                        geo: 40
                    }]
                }
            ]
        });
    </script>
</body>
</html>

Output :

Output

6 Comments

Subject Names are Dynamically @Sangwin Gawande
but u mention the subject names right y.taml y. english in dynamic means how should i mention that
Try above code, I have updated it, It will take dynamic subjects
i need to remove last comma how to do this like tamil: 10, english: 20, social: 30, geo: 40,?
Done, Check updated code, last comma removed.<label ng-show="!$last"> ,</label>
|
1

function ClickToEditCtrl($scope) {
  $scope.SubData=[{Name:'1',Subject:[{tamil:12,english:21,social:45}]},
              {Name:'2',Subject:[{tamil:10,english:20,social:30,geo:40}]}]
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<div ng-app>
  <div ng-controller="ClickToEditCtrl">
    <table style="border : 1px solid black">
    <thead>
    <td style="border: 1px solid black">Name</td>
    <td style="border: 1px solid black">Subject</td>
    </thead>
    <tbody>
      <tr  ng-repeat="stu in SubData">
     <td>
       {{stu.Name}}
     </td>
     <td ng-repeat="(sub, mark) in stu.Subject[0]">
        {{sub}} : {{mark}}
     </td>
  </tr>
    </tbody>   
</table>
  </div>
</div>

Comments

1

function ClickToEditCtrl($scope) {
  $scope.SubData=[{Name:'1',Subject:[{tamil:12,english:21,social:45}]},
              {Name:'2',Subject:[{tamil:10,english:20,social:30,geo:40}]}]
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
<div ng-app>
  <div ng-controller="ClickToEditCtrl">
    <table style="border : 1px solid black">
    <thead>
    <td>Name</td>
    <td>Subject</td>
    </thead>
    <tbody>
      <tr ng-repeat="stu in SubData">
     <td>
       {{stu.Name}}
     </td>
     <td ng-repeat="(sub, mark) in stu.Subject[0]">
        {{sub}} : {{mark}}
     </td>
  </tr>
    </tbody>   
</table>
  </div>
</div>

You can do double iteration . i mean 2 ng-repeat to achive it..

one for overall list,

<table ng-repeat="stu in SubData">
   <tr>
     <td>
       {{stu.Name}}
     </td>
     <td ng-repeat="(sub, mark) in stu.Subject[0]">
        {{sub}} : {{mark}}
     </td>
  </tr>
</table>   

4 Comments

i am also try in ng-repeat @Mohideen ibn
make me as a fiddle it can easily understood
check my fiddle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.