0

I am trying to find a way to display the following JSON data in the form of list (UL, LI) using the ng-repeat and ng-if. But as a newbie to the Angular JS it looks complicated because of the JSON data structure below (schemaJsonData in the controller):

JSON data structure is :

Connections[
  { /* For each connection we have multiple databases*/
  databases[
    { /* For each Database we have tables, view, storedProcedures adn functions*/
    tables[ 
       table[ /* For each table group of columns, indexes, etc */
            columns[], indexes[], foreignKeys[], triggers[]
         ] /*end of table*/   ,
    ],/*end of tables*/ 
    views [
    ], /*end of views*/  
    storedProcedures [
    ], /*end of storedProcedures*/   
    functions [
    ], /*end of functions*/   
   } 
  ] /* end of databases */ 
 }
] /* end of connections */ 

My code is as follows:

<!DOCTYPE html>
<html>

<body>

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

    <ul ng-repeat="(key,value) in schemaData">
        <div ng-if="isArray(value)">

        </div>
        <div ng-if="!isArray(value)">
            <ul> {{value}} </ul>
        </div> 

        <li > 
          <ul ng-if="isArray(value)">
                {{value}}              
          </ul>
          <ul ng-if="!isArray(value)">
                {{value}}              
          </ul>
        </li>


    </ul>
</div>
<script src="lib/angular/angular.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {

  var schemaJsonData = {
       "connectionName":"connection",
       "connections":[
          {
             "name":"connection",
             "databases":[
                {
                   "name":"information_schema",
                   "tables":[

                   ],
                   "views":[

                   ],
                   "storedProcedures":[

                   ],
                   "functions":[

                   ]
                },
                {
                   "name":"mysql",
                   "tables":[

                   ],
                   "views":[

                   ],
                   "storedProcedures":[

                   ],
                   "functions":[

                   ]
                },
                {
                   "name":"performance_schema",
                   "tables":[

                   ],
                   "views":[

                   ],
                   "storedProcedures":[

                   ],
                   "functions":[

                   ]
                },
                {
                   "name":"timetrack",
                   "tables":[
                      {
                         "name":"employee",
                         "columns":[
                            {
                               "name":"idemployee"
                            },
                            {
                               "name":"name"
                            },
                            {
                               "name":"salary"
                            },
                            {
                               "name":"age"
                            }
                         ],
                         "indexes":[

                         ],
                         "foreignKeys":[

                         ],
                         "triggers":[

                         ]
                      },
                      {
                         "name":"work",
                         "columns":[
                            {
                               "name":"id"
                            },
                            {
                               "name":"hours"
                            },
                            {
                               "name":"date"
                            },
                            {
                               "name":"archived"
                            },
                            {
                               "name":"description"
                            }
                         ],
                         "indexes":[

                         ],
                         "foreignKeys":[

                         ],
                         "triggers":[

                         ]
                      }
                   ],
                   "views":[

                   ],
                   "storedProcedures":[

                   ],
                   "functions":[

                   ]
                }
             ]
          }
       ]
    };
  $scope.schemaData = schemaJsonData;
  $scope.isArray = angular.isArray;
});
</script>
</body>
</html>

Problems are:

1) I can use the ng-repeat and ng-if and i am able to check is it array or not

2) But as the structure can change, I want to write a simple code to loop thru the JSON data and if it is Array, then loop thru array and same again (I mean, in the loop, if we get array again, then loop again).

user3278897
  • 984
  • 10
  • 23

1 Answers1

1

I searched for a while to answer my question, honestly Angular JS is making the things complicated and interesting at the same time. At last, i found few solutions to problem, I am updating as answer so, it can be helpful for others like me:

First of all thanks to Poyraz Yilmaz and also, Muli Yulzary (He is interested how to solve this problem :) )

Solution:

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

app.controller('MainCtrl', function($scope) {
  var schemaJsonData =[{
      "name": "myConnection",
      "databases": [{
        "name": "informationDB",
        "tables": [{
          "name": "info",
        }, {
          "name": "table2",
        }]
      }]
    },{
      "name": "yourConnection",
      "databases": [{
        "name": "empDB",
        "tables": [ {
          "name": "employee",
          "columns": [
            {"name": "empsalary"},
            {"name": "abc"},
            {"name": "def"},
          ]
        }]
      }]
    }];        
  $scope.schemaData = schemaJsonData;
  $scope.isString = angular.isString;
  $scope.isNumber = angular.isNumber;
  $scope.isArray = angular.isArray;  
  $scope.isObject =  angular.isObject;
  $scope.searchKeyword = [];
});
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <!-- <link rel="stylesheet" href="style.css" /> -->
    <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.8/angular.js" data-semver="1.4.8"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <input type="text" placeholder="Search" ng-model="searchKeyword">
    <ul>
     <li ng-repeat="(key, item) in schemaData" ng-include="'list.html'">
      </li>
    </ul>
  </body>
  
  <script type="text/ng-template" id="list.html">
    <div ng-if="isObject(item)">
        <div ng-repeat="(key1, item1) in item">       
          <div ng-if="key1 == 'name'">
              <li>{{item1}}</li>
          </div>
          <div ng-if="isArray(item1)">
            <ul ng-repeat="(key, item) in item1" ng-include="'list.html'">
            </ul>
          </div>                 
        </div>  
    </div>
    
    <div ng-if="!isArray(item)">
    </div>
        
</script>

</html>

Guys this another good link to know the Angular JS recursive.

Community
  • 1
  • 1
user3278897
  • 984
  • 10
  • 23