Skip to content

How to make recursive output? #137

Description

@Qclanton

Hello.

I have some data to be displayed recursively. For example, multilevel menu:

var menu = [
    {
        title: "Some",
        link: "http://localhost/1",
        submenu: [
            {
                title: "Foo",
                link: "http://subbaz/17"
            },
            {
                title: "Bar",
                link: "http://subbaz/3"
            },
        ]
    }, 

    {
        title: "Other",
        link: "http://localhost/7",
        submenu: [
            {
                title: "Tra",
                link: "http://subkrya/ggg"
            },
            {
                title: "Bla",
                link: "http://subkrya/ssss",
                submenu: [
                    {
                        title: "We Need To Go Deeper",
                        link: "http://very/long/link"
                    }
                ]
            },
        ]
    }, 
];

In plain javascript I can make output in the following manner:
html:

<nav id="menu"></nav>

js:

function drawMenu(menu) {
    var html = '<ul>';

    menu.forEach(function(element) {
        html += '<li><a href="'+element.link+'">'+element.title+'</a></li>';

        if (typeof element.submenu === "object") {              
            html += drawMenu(element.submenu);
        }
    });

    html += '</ul>';

    return html;
}


var menu_html = drawMenu(menu);
document.getElementById('menu').innerHTML = menu_html;
}

jsfiddle: http://jsfiddle.net/n3908g7u/


How can I similarly bind data using transparency.js?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions