5

I've done a bit of research around and I cannot seem to find the best pattern to use ng-repeat recursively to create a deep table. There is some noteworthy mentions here by user mgdelmonte regarding the subject, but alas, no solution.

The HTML and template:

<body ng-app="myApp">
    <div ng-controller="myAppController">

<script type="text/ng-template"  id="tree_item.html">
    <tr style="width:100%">        
        <td>{{data.name}}</td>
    </tr>
    <div ng-repeat="data in data.nodes" ng-include="'tree_item.html'">

    </div>
</script>

<table class="table table-striped">
     <thead>
        <tr>
            <th style="width:30px;">Test</th>
        </tr>
    </thead>
    <tbody ng-repeat="data in treeData" ng-include="'tree_item.html'">

    </tbody>
</table>
</div>
</body>

The JS:

angular.module('myApp', []);

function myAppController($scope) {
    $scope.treeData = [{
        "name": "Root",
        "nodes": [{
            "name": "Level 1",
            "nodes": [{
                "name": "Level 2"
            }]
        }]
    }];
}

Here is my jsfiddle: http://jsfiddle.net/8f3rL/86/

This is the absolute farthest I got with it. It traverses the tree fine, but I'm losing the <tr> tags when it recurses down. Instead it's rendering <span>s.

My intuition is telling me that when the ng-repeat runs on that <div>, it is creating a separate scope for those trs and tds, which I guess are illegal (since they have to be bound to a table element).

Changing the div to a table or tbody or tr is no-op as well (not that I'd even want to nest a table in like that).

2
  • Maybe this is a dumb question, but you're doing ng-repeat with a <tbody> element. Is that legal? I expect a <table> to have exactly one <tbody>, but maybe I'm dumb :) Another silly question, what is a "deep table"? Commented Jul 23, 2014 at 6:50
  • 1
    Not dumb at all. The tbody and div inside a table are the problem. Commented Jul 23, 2014 at 6:58

1 Answer 1

6

The browser doesn't like the multiple tbodys and the div tags between the rows. Try this...

<body ng-app="myApp">
    <div ng-controller="myAppController">

<script type="text/ng-template"  id="tree_item.html">
    <td>{{data.name}}
        <table style="margin-left: 20px">
            <tr ng-repeat="data in data.nodes" ng-include="'tree_item.html'"></tr>
        </table>
    </td>
</script>

<table class="table table-striped">
     <thead>
        <tr>
            <th style="width:30px;">Test</th>
        </tr>
    </thead>
    <tbody>
        <tr style="width:100%" ng-repeat="data in treeData" ng-include="'tree_item.html'"></tr>
    </tbody>
</table>
</div>

Live Demo - Fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, good call on the <div> being inserted outside of the <td> ... and for being nice enough to provide a solution.
Thanks for the reply. However, this is still a bit unfortunate because we are nesting table elements. Consider what would happen if we placed a border on the tables... jsfiddle.net/V9a2Z/1
Hi @Anthony I know it's beent a while but how would you do with a table with multiple <th>? Where would you place the <table> that contains the ng-include?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.