【问题标题】:Dynamic table header with subheaders带有子标题的动态表头
【发布时间】:2015-01-24 12:17:58
【问题描述】:

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

我的桌子是:

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 会重复整个子标题数组,并且我缺少一些伪元素表或...

我明白了

|a   |b     |
|a1a2|b1b2b3|

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

【问题讨论】:

    标签: angularjs html-table


    【解决方案1】:

    请看下面:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多