带AngularJs的行跨度
我需要一些关于angularJs的ng repeat的帮助。我试图使用带有以下json的colspan呈现一个表:带AngularJs的行跨度,angularjs,html-table,angularjs-ng-repeat,Angularjs,Html Table,Angularjs Ng Repeat,我需要一些关于angularJs的ng repeat的帮助。我试图使用带有以下json的colspan呈现一个表: var json = { "actorActress":[ { "name":"Angelina Jolie", "age":45, "rowspan":2 }, { "name":"Brad Pitt", "age":48, "ro
var json = {
"actorActress":[
{
"name":"Angelina Jolie",
"age":45,
"rowspan":2
},
{
"name":"Brad Pitt",
"age":48,
"rowspan":3
}
],
"Films":[
{
"film":"Mr & Mrs Smith",
"other_info":"info1"
},
{
"film":"Tomb Raider",
"other_info":"info2"
},
{
"film":"Troy",
"other_info":"info1"
},
{
"film":"Mr & Mrs Smith",
"other_info":"info2"
},
{
"film":"Fight Club",
"other_info":"info1"
}
]
}
这是我试图呈现的表格:
我怎样才能做到这一点
提前谢谢
更新-原始阵列
这是我从数据库收到的原始数组
var OriginallyJson = [
{"name": "Angelina Jolie", "age": 45, "film": "Mr & Mrs Smith", "other_info": "info1"},
{"name": "Angelina Jolie", "age": 45, "film": "Tomb Raider", "other_info": "info1"},
{"name": "Brad Pitt", "age": 48, "film": "Mr & Mrs Smith", "other_info": "info2"}, ... ,]
它可以通过使用
ng repeat
和limit
过滤器的正确组合来实现
HTML
<table border="1px solid red">
<thead>
<tr>
<th width="25%">Name</th>
<th width="25%">Age</th>
<th width="25%">Films</th>
<th width="25%">Other Information</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="person in json.actorActress">
<td width="25%">{{person.name}}</td>
<td width="25%">{{person.age}}</td>
<td width="50%" colspan="2">
<table width="100%" border="1px">
<tr width="100%" ng-repeat="f in json.Films | limitTo: person.rowspan: calculateInitialIndex($index)">
<td width="50%">
{{f.film}}
</td>
<td width="50%">
{{f.other_info}}
</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
能否更改阵列结构?也许使用map创建一个新数组,而不是修改原来的数组。是的,我改成了这种结构,因为我认为它更容易渲染表。最初,json变量是一个包含重复数据的数组。我用原来的数组结构更新了我的问题,这是一个很好的答案,但是你认为不使用嵌套表就可以呈现表吗?@Nands这很难。。然后必须重新格式化JSON结构,我建议这样做,这样解决方案也会变得更简单:)
function calculate(startIndex, index, result){
if(startIndex == 0 && index == 0) return result;
result = result + $scope.json.actorActress[--index].rowspan
if(index == 0) return result;
return calculate(startIndex, index, result)
}
$scope.calculateInitialIndex = function(currentIndex) {
var result = 0;
return calculate(currentIndex, currentIndex, result);
}