gpt4 book ai didi

javascript - Angular JS ng-repeat 具有复杂的 JSON 数据(数组、子数组、子对象)

转载 作者:行者123 更新时间:2023-12-03 07:57:00 26 4
gpt4 key购买 nike

我正在尝试找到一种使用 ng-repeat 和 ng-if 以列表(UL、LI)形式显示以下 JSON 数据的方法。但作为 Angular JS 的新手,由于下面的 JSON 数据结构( Controller 中的 schemaJsonData),它看起来很复杂:

JSON 数据结构为:

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 */

我的代码如下:

<!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>

问题是:

1)我可以使用 ng-repeat 和 ng-if 并且我可以检查它是否是数组

2)但是由于结构可以改变,我想编写一个简单的代码来循环遍历JSON数据,如果它是数组,那么循环遍历数组并再次相同(我的意思是,在循环中,如果我们得到数组再次,然后再次循环)。

最佳答案

我搜索了一段时间来回答我的问题,说实话,Angular JS 让事情变得复杂而有趣。最后,我找到了一些问题的解决方案,我正在更新答案,这样,它对像我这样的其他人可能会有所帮助:

首先感谢 Poyraz Yilmaz 和 Muli Yulzary(他对如何解决这个问题感兴趣:))

解决方案:

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>

伙计们,这又是一件好事 link了解 Angular JS 递归。

关于javascript - Angular JS ng-repeat 具有复杂的 JSON 数据(数组、子数组、子对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34756013/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com