带有子标题的动态表头



如何创建(带有角度)动态表格标题,其中标题单元格应具有一些不等于标题单元格的副标题单元格?

我的表是:

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|

这是我的代码:http://plnkr.co/edit/pSwjxi2vMN61tevArW4F?p=preview

请看下面:

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

最新更新