Tuesday, 27 January 2015

Populating nested array data into table using ng-repeat.

Today working on nested json data I thought of trying to populate data into table using ng-repeat, something kind of nested ng-repeat, I am not finding any proper word for it. So it happened like this. I have created one array having to fields Name and Children, where children consists of another child array having gain 2 fields, Name and Age. So I have use 2 nested type ng-repeat, outer one parsing the parent array while inner ng-repeat parsing inner child array. So here is the code and I have not put it into separate script and included everything into one single HTML page.

<!DOCTYPE html>
<html data-ng-app="app">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Transclude example</title>
    <style>

        li 
       {
        list-style-type:none;   

        }
    </style>
    <script src="https://code.angularjs.org/1.0.8/angular.js"></script>
    <script>

      var app = angular.module('app', []);
      app.controller('MainCtrl', function($scope) {
      $scope.mydata = [
        {Name:'Smith',Children:[
        {Name:'Max',Age:'5'},
        {Name:'Mini',Age:'3'},
        {Name:'Jango',Age:'4'}]
        },
        {Name:'Jacko',Children:[
        {Name:'Alia',Age:'4'},
        {Name:'Alexa',Age:'3'},
        {Name:'Pinto',Age:'7'}]
        },
        {Name:'Patrick',Children: [
        {Name:'Blanc',Age:'9'},
        {Name:'Minova',Age:'13'},
        {Name:'Jude',Age:'14'}]
        },
        {Name:'Julia',Children:[
        {Name:'Mama',Age:'2'},
        {Name:'Maggi',Age:'11'},
        {Name:'Troy',Age:'12'}]
        }
        ];
    });
   </script>
  </head>

  <body data-ng-controller="MainCtrl">
    <table border=1>
    <tr ng-repeat="data in mydata">
    <th>{{data.Name}}</th>
    <td ng-repeat="dt in data.Children">{{dt.Name}}</td><td>{{dt.Age}}</td>     
    </tr>
    </table>
  </body>
</html>

AngularJS Transclude example
{{data.Name}} {{dt.Name}}{{dt.Age}}

No comments: