简体   繁体   English

循环显示分层数据

[英]Loop to display hierarchical data

I am creating an array out of essentially hierachical data, for example as below: 我从本质上是层级数据创建数组,例如,如下所示:

[
    {id: 1, title: 'hello', parent: 0, children: [
        {id: 3, title: 'hello', parent: 1, children: [
            {id: 4, title: 'hello', parent: 3, children: [
                {id: 5, title: 'hello', parent: 4},
                {id: 6, title: 'hello', parent: 4}
            ]},
            {id: 7, title: 'hello', parent: 3}
        ]}
    ]},
    {id: 2, title: 'hello', parent: 0, children: [
        {id: 8, title: 'hello', parent: 2}
    ]}
]

I am looking to loop through the array, but can't get my head around how to recursively loop down to create an unordered list where each child level is indented. 我正在寻找遍历数组的方法,但无法理解如何递归地遍历以创建一个无序列表,其中每个子级都缩进了。 Trying to do this in JavaScript, but need a push in the right direction for the construction of the loop to drill down until there are no more children, and then back up to the top array. 尝试使用JavaScript进行此操作,但需要按正确的方向推动循环的构造,以向下钻取直到不再有子级为止,然后返回到顶部数组。

Any help would be appreciated. 任何帮助,将不胜感激。

I answered a question about this before 我之前回答了一个问题

Here is demo for it: http://jsfiddle.net/zn2C7/7/ 这是它的演示: http : //jsfiddle.net/zn2C7/7/

var list = $("<ul>");
function populatedata() {
     $.each(data.FolderList, function (i, folder) {                
         if (folder.ParentFolderID == -1) {
            var item = $("<li>").html(folder.FolderName);

            list.append(item);
            var children = $('<ul>');
            item.append(children);
            checkChild(folder.FolderID, children);                    
        }

    });
    $('body').append(list);
}

function checkChild(parentid, parent) {
    $.each(data.FolderList, function (i, folder) {
        if (folder.ParentFolderID == parentid) {
            var item = $("<li>").html(folder.FolderName);                    
            var children = $('<ul>');
            parent.append(item);
            item.append(children);
            checkChild(folder.FolderID, children);                    
        }
        else {
            return ;
        }
    });
}

It was possible to build it using html variable, like you tried to do that, but it is much simpler to use DOM manipulation functions of jQuery ($('<ul>') and $('<li>') - create new element, .append() - append element to some other element) 可以使用html变量来构建它,就像您尝试做的那样,但是使用jQuery的DOM操作函数($('<ul>') and $('<li>') - create new element, .append() - append element to some other element)要简单得多($('<ul>') and $('<li>') - create new element, .append() - append element to some other element)

  function checkChild(parentid) {
        $.each(data.FolderList, function (i, folder) {

            if (folder.ParentFolderID == parentid) {
                html += '<li><ul>' + folder.FolderName;
                checkChild(folder.FolderID);
                html+=</ul></li>
                return html;
            }
            else {
                return ;
            }
        });
    }

Also, please note that in code above you are doing return html; 另外,请注意,在上面的代码中,您正在执行return html; from each function callback. 从每个函数回调。 Not sure what you wanted to get exactly, but in .each it may work like break in regular loop (if you will return false): 不确定要获取的内容是什么,但是在.each中,它可能像在常规循环中中断一样工作(如果返回false):

We can stop the loop from within the callback function by returning false. 我们可以通过返回false来停止回调函数中的循环。

That is from jquery api page. 那是从jQuery API页面。

Also, for such tasks I prefer to use debugger. 此外,对于此类任务,我更喜欢使用调试器。 At this moment there are a lot of powerful tools for HTML/CSS/JS debugging in browser. 目前,有很多功能强大的工具可用于在浏览器中调试HTML / CSS / JS。 Just press F12 in Chrome, IE or FF (for the last one you may need to install Firebug extension) and you will get a lot of helpful features along with simple JS debugging. 只需在Chrome,IE或FF中按F12(对于最后一个,您可能需要安装Firebug扩展程序),您将获得许多有用的功能以及简单的JS调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM