【问题标题】:Creating a matrix report by ng-repeat通过 ng-repeat 创建矩阵报告
【发布时间】:2016-09-08 06:18:04
【问题描述】:

我正在尝试使用 ng-repeat 创建一个矩阵表。我通过使用 pivot PIVOT 以我希望的方式获得了 sql 查询输出,并且现在在 Sql 服务器上拥有它

+-------------+----+----+---+---+---+---+-----+---+---+-----+---+-----+---+-----+---+
| displayname | a  | b  | c | d | e | f | g h | i | j | k l | m | n o | p | r s | t |
+-------------+----+----+---+---+---+---+-----+---+---+-----+---+-----+---+-----+---+
| row1        |  2 |  0 | 0 | 0 | 1 | 0 |   1 | 0 | 0 |   0 | 1 |   0 | 0 |   0 | 0 |
| row2        |  1 |  0 | 0 | 0 | 0 | 0 |   0 | 0 | 0 |   0 | 0 |   0 | 0 |   1 | 0 |
| row3        | 57 | 28 | 3 | 6 | 6 | 4 |   3 | 0 | 0 |   1 | 1 |   1 | 1 |   2 | 1 |
| row4        |  5 |  4 | 2 | 1 | 0 | 0 |   0 | 0 | 1 |   2 | 0 |   0 | 0 |   0 | 1 |
| row5        |  0 |  2 | 5 | 2 | 0 | 0 |   1 | 1 | 0 |   0 | 0 |   1 | 0 |   0 | 0 |
+-------------+----+----+---+---+---+---+-----+---+---+-----+---+-----+---+-----+---+

我正在尝试使用 html、数据表和 Angular js 复制此输出,但我被卡住了。

首先我不知道如何使第一列 (row1,..5) 保持不变,其次我不知道在查询中旋转表是否正确。

我尝试先对每个 <tr> 的 <td> 进行硬编码,然后在其余的 <td> 元素上使用 ng-repeat,但它没有用。另外,我怎样才能达到 ng-repeat 中的项目索引,如 item[0].a ?

【问题讨论】:

    标签: html angularjs datatables


    【解决方案1】:

    您可以尝试以下代码和附加的 plunker 示例

    JS

    var app = angular.module('plunker', []);
    
    app.controller('MainCtrl', function($scope) {
      $scope.dataSet = [{
        'col1': '1',
        'col2': '2',
        'col3': '3',
        'col4': '4',
        'col5': '5'
      }, {
        'col1': 'a1',
        'col2': 'a2',
        'col3': 'a3',
        'col4': 'a4',
        'col5': 'a5'
      }];
      $scope.cols = Object.keys($scope.dataSet[0]);
      console.log($scope.cols);
      $scope.name = 'World';
    });
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <!DOCTYPE html>
    <html ng-app="plunker">
    
    <head>
      <meta charset="utf-8" />
      <title>AngularJS Plunker</title>
      <script>
        document.write('<base href="' + document.location + '" />');
      </script>
      <link rel="stylesheet" href="style.css" />
      <script data-require="angular.js@1.0.x" src="https://code.angularjs.org/1.0.8/angular.js" data-semver="1.0.8"></script>
      <script src="app.js"></script>
    </head>
    
    <body ng-controller="MainCtrl">
      <table class="table">
        <tr>
          <th>Display Name</th>
          <th ng-repeat="key in cols">{{key}}</th>
        </tr>
        <tr ng-repeat="data in dataSet">
          <td>Row {{$index+1}}</td>
          <td>{{data.col1}}</td>
          <td>{{data.col2}}</td>
          <td>{{data.col3}}</td>
          <td>{{data.col4}}</td>
          <td>{{data.col5}}</td>
    
        </tr>
      </table>
    </body>
    
    
    </html>

    【讨论】:

      猜你喜欢
      • 2022-11-02
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      相关资源
      最近更新 更多