如何创建(带有角度)动态表头,其中头部单元应该有一些副头部单元不等于头部单元?
我的表是:
var table = [{
header: "a",
subheaders: "a1", "a2"
}, {
header: "b",
subheaders: "b1", "b2", "b3"
}]
我尝试创建这样的表头(表数据将具有单独的控制器):
|a |b |
|a1|a2|b1|b2|b3|
我使用ng-repeat并且标题一切正常(当我做<tr ng-repeat="head in table"><th>{{head.header}}</th></tr>
时),但是当我尝试对子标题执行相同操作时,ng-repeat重复整个子标题数组并且我遗漏了一些伪表或smth的元素......
我得到了
|a |b |
|a1a2|b1b2b3|
答案 0 :(得分:2)
请参阅以下内容:
(function() {
var table = {
thead: [{
header: "A",
subheaders: [
"A1",
"A2"
]
}, {
header: "B",
subheaders: [
"B1",
"B2",
"B3"
]
}, {
header: "C",
subheaders: [
"C1",
"C2",
"C3"
]
}]
};
var app = angular.module("app", []);
app.controller("Controller", function() {
var vm = this;
vm.table = table;
vm.subArray = [];
vm.subs = function(table) {
angular.forEach(vm.table.thead, function(header) {
angular.forEach(header.subheaders, function(subheader) {
vm.subArray.push(subheader)
})
})
};
vm.subs();
});
})()
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<div ng-app="app">
<div ng-controller="Controller as ctrl">
<h1>Some table</h1>
<table class="table table-bordered">
<thead>
<tr>
<th ng-repeat="head in ctrl.table.thead" colspan="{{head.subheaders.length}}">{{head.header}}</th>
</tr>
<tr>
<th ng-repeat="sub in ctrl.subArray">{{sub}}</th>
</tr>
</thead>
<tbody>
<tr>
<td ng-repeat="i in [1, 2, 3, 4, 5, 6, 7, 8]">{{i}}</td>
</tr>
</tbody>
</table>
</div>
</div>