代码之家  ›  专栏  ›  技术社区  ›  PotatoBox

使用AngularJS在表中动态生成列

  •  0
  • PotatoBox  · 技术社区  · 7 年前

    data : {
        'Person A': {
            'Week 1' : {
                'Task 1': True,
                'Task 2': False,
            },
            'Week 2' : {
                'Task 1': True,
                'Task 2': True,
            },
        },
        'Person B': {
            'Week 1' : {
                'Task 1': True,
                'Task 2': False,
            },
            'Week 2' : {
                'Task 1': False,
                'Task 2': False,
            },
        },
    }
    

    这种结构随着周数的变化而变化。我想让它动态生成,所以它看起来像这样:

    enter image description here

    我想到的第一件事就是创建列模板,然后对列表中的每个元素重复它。但这正是我遇到问题的地方——我就是做不到,每一个新列都被添加到上一列的下面。我的代码如下:

    <table>
      <thead>
        <th>Person ID</th>
        <th ng-repeat="week in weeks order by $index">
          <tr>
            <td>{{ week }}</td>
          </tr>
          <tr>
            <td>Task 1</td>
            <td>Task 2</td>
          </tr>
        </th>
      </thead>
      <tbody>
        <tr ng-repeat='person in data'>
          <td>{{ person }}</td>
          <td>{{ Task 1 }}</td>
          <td>{{ Task 2 }}</td>
        </tr>
      </tbody>
    </table>
    

    另外,我还想循环处理任务,这意味着我想为特定的人动态地创建和填充每个特定星期的数据列,但我不知道如何-我不能只是把 ng-repeat 在里面 <td>

    1 回复  |  直到 7 年前
        1
  •  1
  •   Slava Utesinov    7 年前

    angular.module('app', []).controller('ctrl', function($scope) {
      $scope.data = {
        'Person A': { 
          'Week 1': { 'Task 1': true, 'Task 2': false },
          'Week 2': { 'Task 1': true, 'Task 2': true }
        },
        'Person B': {
          'Week 1': { 'Task 1': true, 'Task 2': false },
          'Week 2': { 'Task 1': false, 'Task 2': false }
        }
      };
    
      $scope.weeks = [];
      for (var personName in $scope.data) {
        var person = $scope.data[personName];
        for (var weekName in person) {
          var week = person[weekName];
    
          var weekScope = {
            name: weekName,
            tasks: []
          };
          $scope.weeks.push(weekScope);
    
          for (var taskName in week)        
            weekScope.tasks.push(taskName)      
        }
        break;
      }
    
    })
    table,
    th,
    td {
      border: 1px solid black;
      border-collapse: collapse;
    }
    
    .center{
      text-align:center;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js">
    </script>
    
    <div ng-app='app' ng-controller='ctrl'>
      <table>
        <thead>
          <tr>
            <td rowspan='2'>Person ID</td>
            <td ng-repeat='week in weeks' class='center' colspan='{{week.tasks.length}}'>
              {{week.name}}
            </td>
          </tr>
          <tr>
            <td ng-repeat-start='week in weeks' hidden></td>
            <td ng-repeat-end ng-repeat='task in week.tasks'>{{task}}</td>
          </tr>
        </thead>
        <tbody>
          <tr ng-repeat='(personName, person) in data'>
            <td>{{personName}}</td>
            <td ng-repeat-start='week in weeks' hidden></td>
            <td ng-repeat-end ng-repeat='task in week.tasks'>
              {{person[week.name][task]}}
            </td>
          </tr>
        </tbody>
      </table>
    </div>