0

I have written this HTML to display my data in tree format, but its not working as expected. What is the issue here.

I know I can use custom directive to do that, but I just want it to show in very basic format. Also writing directive for this will be very tricky.

<div data-ng-cloak data-ng-show="rootNode.children.totalAvailable > 0">
    <table border=1>
        <tr>
            <th>Tree Form</th>
        </tr>


        <tr data-ng-repeat="parent in rootNode.children.items">
            <tr>
                <td>
                    {{parent.displayName}}
                </td>
            </tr>

            <tr data-ng-repeat="firstChild in parent.children.items">
                <tr>
                    <td>
                        {{firstChild.displayName}}
                    </td>
                </tr>
                <tr data-ng-repeat="secondChild in firstChild.children.items">
                    <tr>
                        <td>
                            {{secondChild.displayName}}
                        </td>
                    </tr>
                </tr> 
            </tr>       
        </tr>
    </table>
</div>

Tree structure I want to display is as follows

Parent 1
    Child 1.0
        SecondChild 1.0.1
        SecondChild 1.0.2
        SecondChild 1.0.3
    Child 1.1
        SecondChild 1.1.1
        SecondChild 1.1.2
        SecondChild 1.1.3

Parent 2
    Child 2.0
        SecondChild 2.0.1
        SecondChild 2.0.2
        SecondChild 2.0.3
    Child 2.1
        SecondChild 2.1.1
        SecondChild 2.1.2
        SecondChild 2.1.3

Thanks in advance.

2 Answers 2

1

Pretty sure that Angular knows the correct pluralization. (firstChild -> firstChildren, if not try firstChilds). Also does it need to be in a table.

<div data-ng-cloak data-ng-model="items" data-ng-show="rootNode.children.totalAvailable > 0">
    <table border=1>
        <tr>
            <th>Tree Form</th>
        </tr>
        <tr data-ng-repeat="parent in items">
            <tr>
                <td>
                    {{parent.displayName}}
                </td>
            </tr>
            <tr data-ng-repeat="firstChild in parent.firstChildren">
                <tr>
                    <td>
                        {{firstChild.displayName}}
                    </td>
                </tr>
                <tr data-ng-repeat="secondChild in firstChild.secondChildren">
                    <tr>
                        <td>
                            {{secondChild.displayName}}
                        </td>
                    </tr>
                </tr>
            </tr>
        </tr>
    </table>
</div>

Or not in a table.

<div data-ng-cloak data-ng-controller="NameController" data-ng-show="rootNode.children.totalAvailable > 0">
    <ol data-ng-model="items">
        <li data-ng-repeat="parent in items">
            <span data-ng-bind="parent.displayName"></span>
            <ol>
                <li data-ng-repeat="firstChild in parent.firstChildren">
                    <span data-ng-bind="firstChild.displayName"></span>
                    <ol>
                        <li data-ng-repeat="secondChild in firstChild.secondChildren">
                            <span data-ng-bind="secondChild.displayName"></span>
                        </li>
                    </ol>
                </li>
            </ol>
        </li>
    </ol>
</div>

Let me know if this is enough or if you want me to make a Plunker or something.

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

Comments

0

Maybe this is a silly suggestion but why don't you use a CSS text-indent depending on child level? The whole idea of tr in tr seems to be overcomplicated and poorly maintainable

4 Comments

Main issue here is I am not able to use ng-repeat in nested fashion
your ng-repeats looks fine, but the output html will be invalid
Why my HTML will be invalid. Please be specific. Is there any way to achieve this.
for a starter as far as i remember valid elements in tr are td and th

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.