javascript - 使用 ngRepeat 的复杂嵌套行表

标签 javascript angularjs

我有一个看起来像这样的对象:

{
    "10": {},
    "12": {
        "20": {
            "value": 1,
            "id": 1,
        },
        "100": {
            "value": 12,
            "id": 1,
        }
    },
    "14": {
        "100": {
            "value": 14,
            "id": 2,
        }
    },
    "16": {},
    "18": {},
    "20": {
        "100": {
            "value": 23,
            "id": 1,
        },
        "150": {
            "value": 56,
            "id": 3,
        }
    },
    "22": {},
    "24": {},
    "26": {
        "50": {
...

我想根据这个对象创建一个表,应该是这样的:

enter image description here

不过,我不确定如何通过使用正确的重复组合来创建此表。我目前正在尝试:

<table>
    <thead>
        <tr>
            <th>type</th>
            <th>module</th>
            <th>value</th>
            <th>id</th>
        </tr>
    </thead>
    <tbody>

        <tr ng-repeat-start="(row1, value1) in TestData">
            <td rowspan="{{value1.length}}">{{row1}}</td>
            <td ng-repeat="(label2, value2) in value1">{{label2}}</td>
        </tr>
    </tbody>
</table>

最佳答案

乍一看,您可以通过插入 <tbody> 来做类似的事情(更多信息在这里:Angular.js ng-repeat across multiple tr's)

var myAppModule = angular.module('myApp', []).controller('MyController', MyController);

function MyController() {

  // https://stackoverflow.com/questions/1345939/how-do-i-count-a-javascript-objects-attributes
  this.objectCount = function(obj) {
    return Object.keys(obj).length;
  }

  this.data = {
    "10": {},
    "12": {
      "20": {
        "value": 1,
        "id": 1,
      },
      "100": {
        "value": 12,
        "id": 1,
      },
      "200": {
        "value": 120,
        "id": 10,
      }
    },
    "14": {
      "100": {
        "value": 14,
        "id": 2,
      }
    },
    "16": {},
    "18": {},
    "20": {
      "100": {
        "value": 23,
        "id": 1,
      },
      "150": {
        "value": 56,
        "id": 3,
      }
    },
    "22": {},
    "24": {}
  };

}
table,
th,
td {
  border: 1px solid black;
}
table {
  border-collapse: collapse;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>

<div ng-app="myApp" ng-controller="MyController as ctrl">
  <table>
    <thead>
      <tr>
        <th>type</th>
        <th>module</th>
        <th>value</th>
        <th>id</th>
      </tr>
    </thead>
    <tbody ng-repeat="(row1, value1) in ctrl.data" ng-switch="ctrl.objectCount(value1) === 0">
      <tr ng-switch-when="true">
        <td>{{row1}}</td>
        <td colspan="3"></td>
      </tr>
      <tr ng-switch-when="false" ng-repeat="(row2, value2) in value1">
        <td ng-if="$index === 0" rowspan="{{ctrl.objectCount(value1)}}">
          {{row1}}
        </td>
        <td>{{row2}}</td>
        <td>{{value2.value}}</td>
        <td>{{value2.id}}</td>
      </tr>
    </tbody>
  </table>
</div>

如果你需要更精细的合并行和列,你应该考虑构建一个更面向对象的 View 对象,这样模板更简单,你的业务逻辑在 JavaScript 中。

关于javascript - 使用 ngRepeat 的复杂嵌套行表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34592937/

相关文章:

javascript - AngularJS 用对象填充 ng-options

javascript - Angular 错误 : [$rootScope:inprog] $apply already in progress on document. click()

javascript - 如何使用 "AddEventListener"方法向 Canvas 元素添加双击事件?

angularjs - 在 ng-upgrade 中使用 Protractor

javascript - 在导航菜单中添加一个向下滑动的搜索框

具有固定长度、前导空格和数字的 JavaScript 正则表达式模式

javascript - IONIC 使用 JSON 格式建议

javascript - 如何使用 JavaScript 中的函数最好地从数组内部返回记录

javascript - 现有选项卡的样式看起来像单选按钮

javascript - Angular Validator 检查输入是否为数字