【发布时间】:2015-10-17 20:52:03
【问题描述】:
如何在 Json 下绑定 Place 和 Activity?我确实使用表解析 Event 和 AccountShop 并且它运行。我认为是因为 Place 和 Activiy 中的分隔符?任何人都可以在这里提供帮助或有建议或建议将我从代码中删除。
table.htm
<!DOCTYPE html>
<html ng-app="myTable">
<head>
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.0/angular.min.js"></script>
<script type="text/javascript">
var myTable=angular.module('myTable',[]);
myTable.controller('tableCtrl',function($scope,$http){
$http.get("Table.json").success(function(response){
$scope.members=response.events;
});
});
</script>
<style type="text/css">
table, th, td{
border: 2px sol grey;
border-collapse: collapse;
padding: 4px;
font-family: arial;
}
table tr:nth-child(odd){
background-color: #00BFFF;
}
table tr:nth-child(even){
background-color: #808080;
}
</style>
</head>
<body ng-controller="tableCtrl">
<table border="1">
<tr>
<th>Event</th>
<th>Account Shop</th>
<th>Place</th>
<th>Activity</th>
</tr>
<tr ng-repeat="member in members">
<td>{{member.Event.id}}<br>
{{member.Event.account_id}}<br>
{{member.Event.shop_id}}<br>
<td ng-repeat="member in members">
{{member.AccountShop.id}}<br>
{{member.AccountShop.name}}<br>
{{member.AccountShop.short_code}}<br>
</td>
<tr ng-repeat="member in members">
{{member.Place.id}}
{{member.Place.name}}
{{member.Place.lk_country_code}}
</table>
</body>
</html>
Table.json
{
"events": [
{
"Event": {
"id": "59",
"account_id": "12",
"shop_id": "12",
},
"AccountShop": {
"id": "1",
"name": "Sample",
"short_code": "SampleL"
},
"Places": [
{
"id": "537",
"name": "Sample",
"lk_country_code": "MY"
}
],
"Activities": [
{
"id": "4011",
"short_description": "Sample\r\n"
},
{
"id": "106",
"short_description": "Sample\r\n \r\n"
},
{
"id": "1027",
"short_description": "Sample\r\n"
}
]
}
]
}
【问题讨论】:
-
你是否从后端控制数据结构?如果是这样,可以通过在每个项目内移动像
Event这样的属性并制作一个平面对象数组来更好地优化结构......使用像type:'Event'这样的公共属性 -
你能尝试运行它吗 :),它毁了我,就像我在 3 小时前玩代码解决了 dis lol,然后我搜索其他代码。我认为问题出在 html 文件上,
-
将此代码放入plunker的演示中。如果需要,会在那里修改一下
-
@Jerald,你想用内联网格(行扩展网格)显示网格吗??
-
@Vijay 是的,类似这样 :) 行扩展网格
标签: angularjs json parsing html-table