1

I got json object from mongoDb like below

JsonData = [
        Number: {
                 name1 : 4,
                 name2 : 5,
                 name3 : 8,
                 name4 : 5
                },
        Sum : {
                name1 : 52,
                name2 : 55,
                name3 : 82,
                name4 : 55
               },
        Ratio : {
                name1 : 1.5,
                name2 : 5.2,
                name3 : 0.5,
                name4 : 4.2
               }
            ]

In the above json data only the numbers will change and remaining all static and the array will not change except the numbers.

I am trying to bind this jsonData to grid as shown below

NameTypes Number  Sum  Ratio 
+--------+------+----+-----
  name1     4     52   1.5
  name2     5     55   5.2
  name3     8     82   0.5
  name4     5     55   4.2

How to bind this in angular.js

Thanks for Help

12
  • Are you trying to use this JSON in ng-grid? Commented May 30, 2017 at 10:00
  • 4
    Have you tried anything? Commented May 30, 2017 at 10:00
  • @DavidR I have tried with normal table not ng-grid using ng-repeat Commented May 30, 2017 at 10:01
  • @Rajesh I have used ng-repeat and binded before for normal grids where column names are from data Commented May 30, 2017 at 10:03
  • So the real issue is how to loop over keys of object. Right? Commented May 30, 2017 at 10:03

1 Answer 1

3

It should be like this

var app = angular.module('anApp', []);
app.controller('ctrl', function($scope) {
  $scope.JsonData = {
    Number: {
      name1: 4,
      name2: 5,
      name3: 8,
      name4: 5
    },
    Sum: {
      name1: 52,
      name2: 55,
      name3: 82,
      name4: 55
    },
    Ratio: {
      name1: 1.5,
      name2: 5.2,
      name3: 0.5,
      name4: 4.2
    }
  };      
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.min.js"></script>

<div ng-app="anApp" ng-controller="ctrl">

  <table>
    <thead>
      <tr>
        <th>Type</th>
        <th ng-repeat="(key,value) in JsonData">{{key}}</th>
      </tr>
    </thead>
    <tbody ng-repeat="(key,value) in JsonData.Number">
      <tr>
        <td>{{key}}</td>
        <td ng-repeat="(key,value) in JsonData">{{value[$parent.key]}}</td>
      </tr>
    </tbody>
  </table>

</div>

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

10 Comments

Well tried. but I don't know why you hardcode name with index?
*1 for your effort :)
@RameshRajendran +1 for your help and now we can remove this I think $scope.arraySize = new Array(4);
@jose ! yup. not needed. but should be your every array having the same object length :) good luck !
@RameshRajendran Thanks! for this this is perfect. out of curiocity i am asking if Sum array has another name5 : 52 and remaing arrays dont have how it will be show
|

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.