AngularJS如何使用table标签实现无限层级递归模板渲染
AngularJS中table标签递归渲染嵌套数据问题
我正在尝试研究如何在AngularJS中使用table标签实现递归渲染,现有如下数据结构,其递归嵌套层级无上限:
$scope.report = [{ 'Name': 'Moo', 'Value': 'MooV', 'Children': [{ 'Name': 'Moo2', 'Value': 'Moo2V', 'Children': [{ 'Name': 'Moo3', 'Value': 'Moo3V' }] }] }];
我需要将上述嵌套数据渲染为视觉上扁平的简单表格,目标表格结构如下:
<table> <tr> <td>Moo</td> <td>MooV</td> </tr> <tr> <td>Moo2</td> <td>Moo2V</td> </tr> <tr> <td>Moo3</td> <td>Moo3v</td> </tr> <table>
这种实现可以在对用户呈现扁平表格效果的同时,支持对各层级节点做定制化处理,但我目前编写的代码无法正常运行,当前代码如下:
<div ng-app="app" ng-controller='AppCtrl'> <script type="text/ng-template" id="rloop"> <td>{{data.Name}}</td> <td>{{data.Value}}</td> <tr ng-repeat="data in data.Children" ng-include src="'rloop'"></tr> </script> <table class="table table-condensed"> <thead> <tr> <th>Name</th> <th>Value</th> </tr> </thead> <tbody> <tr ng-repeat-start="data in report" ng-include="'rloop'"></tr> <tr ng-repeat-end></tr> </tbody> </table> </div>
该代码失效的原因是递归逻辑会生成tr标签嵌套tr的非法HTML结构,我曾尝试将ng-repeat移动到tbody等标签上调整实现,但受HTML表格标签的嵌套规则限制(例如tr标签内不允许嵌套其他tr)始终无法得到正确效果。问题复现地址:JSfiddle
内容的提问来源于stack exchange,提问作者David Hubner
相关产品推荐
相关产品推荐

