1

I have a Json given below

[{
    groupName: "Group 1",
    [{
        name: "Item 1"
    }, {
        name: "Item 2"
    }]
},
{
    groupName: "Group 2",
    [{
        name: "Item 3"
    }, {
        name: "Item 4"
    }]
}]

I need a table like shown here , how can this be achieved in angular js.

2
  • 2
    (+1 @Mistalis) Is your json in a variable accessible from a controller's scope ? Have a look at the ng-repeat directive (docs.angularjs.org/api/ng/directive/ngRepeat) Commented Feb 21, 2017 at 12:51
  • Like this? Also your Object is not valid. Commented Feb 21, 2017 at 13:02

3 Answers 3

2

I keep your JSON structure, but just gave name vals to the child array:

angular.module('app', []).controller('ctrl', function($scope) {
  $scope.data = [{
    groupName: "Group 1",
    vals: [{
      name: "Item 1"
    }, {
      name: "Item 2"
    }]
  }, {
    groupName: "Group 2",
    vals: [{
      name: "Item 3"
    }, {
      name: "Item 4"
    }]
  }];
});
table,
th,
td {
  border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<table ng-app="app" ng-controller="ctrl">
  <thead>
    <td><strong>Group</strong></td>
    <td><strong>Items</strong></td>
  </thead>
  <tbody ng-repeat="d in data">
    <tr>
      <td rowspan="{{d.vals.length}}">{{d.groupName}}</td>
      <td>{{d.vals[0].name}}</td>
    </tr>
    <tr ng-repeat="v in d.vals" ng-if="$index > 0">
      <td>{{v.name}}</td>
    </tr>
  </tbody>
</table>

Demo on JSFiddle

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

Comments

2

Here is my solution

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
    $scope.data = [{
      groupName: "Group 1",
      nestedData: [{
        name: "Item 1"
      }, {
        name: "Item 2"
      }]
    },
    {
      groupName: "Group 2",
      nestedData: [{
        name: "Item 3"
      }, {
        name: "Item 4"
      }]
    }
  ];
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
  <table class="table">
    <thead>
      <th>Name</th>
      <th>Nested name</th>
    </thead>
    <tbody ng-repeat="item in data">
          <tr ng-repeat="nestedItem in item.nestedData">
            <td rowspan="{{item.nestedData.length}}" ng-hide="$index == 1">{{item.groupName}}</td>
            <td>{{nestedItem.name}}</td>
          </tr>
    </tbody>
  </table>
</div>

Comments

2

You have to use ng-repeat directive in order to render information into table.

var myApp = angular.module('myApp', []);

function myCTRL($scope) {
    $scope.groups = [{
      groupName: "Group 1",
      sub: [{
        name: "Item 1"
      }, {
        name: "Item 2"
      }]
    },
    {
      groupName: "Group 2",
      sub: [{
        name: "Item 3"
      }, {
        name: "Item 4"
      }]
    }
  ];
}
td,th{
  border:1px solid grey;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCTRL">
   <table>
            <tr>
              <th>Group</th>
              <th>Items</th>
            </tr>
            <tr ng-repeat="group in groups" >
              <td>{{group.groupName}}</td>
              <td >
                 <table>
                    <tr ng-repeat="subgroup in group.sub">
                    <td>{{subgroup.name}}</td>
                   </tr>
                 </table>
              </td>
            </tr>
          </table> 
</div>

Comments

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.