代码之家  ›  专栏  ›  技术社区  ›  0xtuytuy

角度ng重复表:对象的动态列

  •  0
  • 0xtuytuy  · 技术社区  · 6 年前

    我正在尝试创建一个如下所示的表:

    20       23        25       26
    734.53   279.20    936.95   584.50
    

    基于此对象:

    {
        "20": 734.5339864003384,
        "23": 279.20378246766563,
        "25": 936.9526667106079,
        "26": 584.5060233468447,
        "27": 279.20378246766563,
        "28": 2055.970661511549,
        "29": 1405.0981690224412,
        "30": 549.4917661928141,
        "31": 2329.1464674575695,
        "32": 147.8822594632703,
        "33": 698.0104349592335
    }
    

    显然,根据JSON的键,列的标题需要是动态的,并且值应该对应。

    我有以下代码,但我不确定我是否在正确的轨道上:

    JS

     $scope.modal = {};
        $scope.modal.keys = [];
        $scope.modal.data = [];
        for (var key in assets.total) {
          if (assets.total.hasOwnProperty(key)) {
            $scope.modal.keys.push(key);
            $scope.modal.data.push(assets.total[key]);
          }
        }
    

    HTML

    <table class="table table-striped">
      <thead>
        <tr>
          <th ng-repeat="th in modal.key">{{th}}</th>
        </tr>
      </thead>
      <tbody></tbody>
        <tr ng-repeat="x in modal.data">
          <td ng-repeat="th in modal.key">{{x[th]}}</td>
        </tr>
      </tbody>
    </table>
    

    它只是不起作用,我只使用一列就得到了一个奇怪的结果。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Krishna Rathore    6 年前

    ng-repeat="(key, value) in Array"

    Working Demo on Stackblitz

    for (var key in assets.total)

    <table class="table table-striped">
        <thead>
            <tr>
            <th ng-repeat="(key, value) in data"> {{key}} </th>
            </tr>
        <tr>
            <td ng-repeat="(key, value) in data"> {{ value| number: 2 }} </td>
            </tr>
        </tbody>
    </table>
    

    $scope.data = {
          "20": 734.5339864003384,
          "23": 279.20378246766563,
          "25": 936.9526667106079,
          "26": 584.5060233468447,
          "27": 279.20378246766563,
          "28": 2055.970661511549,
          "29": 1405.0981690224412,
          "30": 549.4917661928141,
          "31": 2329.1464674575695,
          "32": 147.8822594632703,
          "33": 698.0104349592335
    }