简体   繁体   English

附加 <ul> 和 <li> 在递归循环中

[英]Append <ul> and <li> in recursive loop

I have a site collection. 我有一个网站集。 I was told I need a recursive loop to do this. 有人告诉我我需要一个递归循环来做到这一点。

This is what I've tried: 这是我尝试过的:

When the site loads, call getSiteTree() which passes the top level website to my getSubSite() function. 网站加载后,调用getSiteTree(),它将顶级网站传递给我的getSubSite()函数。 From there I check if there are any subsites. 从那里,我检查是否有任何子站点。 I have a boolean but I'm not really using it for anything yet, I've just seen it used before for this type of work. 我有一个布尔值,但是我还没有真正使用它,我刚刚看过它用于这种类型的工作。 Anyways, from there I check if there are any sub sits, if not I log the end of the branch, if there are, I call the function again using the new url and repeat the process. 无论如何,我从那里检查是否有子位置,如果没有,则记录分支的末尾,如果有,我使用新的url再次调用该函数并重复该过程。 Looking at my console, it seems to work as intended. 查看我的控制台,它似乎可以正常工作。

function getSiteTree(){
  var tree = $('#treeviewList');
  var rootsite = window.location.protocol + "//" + window.location.hostname;
  var siteEnd = false;

  getSubSite(rootsite);  
}

function getSubSite(url){
    $().SPServices({
    operation: "GetWebCollection",
    webURL: url,
    async: true,
    completefunc: function(xData, Status) {
        var siteUrl; 
        var siteCount = $(xData.responseXML).find("Web").length;

        if(siteCount == 0){
            console.log("end of branch");
            siteEnd = true;
        }else{
            $(xData.responseXML).find("Web").each(function() {
                siteUrl = $(this).attr("Url");
                console.log(siteUrl);
                getSubSite(siteUrl);
            });
      }
    }
    });
}

My questions : now that I have my sites, I need to take those sites and create something like this but I'm not sure how to accomplish this. 我的问题 :现在我有了我的网站,我需要访问这些网站并创建类似的内容,但是我不确定如何做到这一点。

    <li>Site 1
        <ul>
            <li>sub 1.1</li>
            <li>sub 1.2</li>
            <li>sub 1.3</li>
                <ul>
                    <li>1.3.1</li>
                </ul>
            <li>sub 1.4</li>
            <li>sub 1.5</li>
        </ul>
    </li>
    <li>Site 2
        <ul>
            <li>sub 2.1</li>
            <li>sub 2.2</li>
            <li>sub 2.3</li>
                <ul>
                    <li>2.3.1</li>
                    <li>2.3.2</li>
                </ul>
        </ul>
    </li>
</ul>

I have this inital html: 我有这个初始的html:

      <div id="treeviewDiv" style="width:200px;height:150px;overflow:scroll">
        <ui id="treeviewList"></ui>
      </div>

This is the output I can get and what I'm trying to accomplish. 这是我可以获得的输出以及我要完成的工作。 Hopefully this clears things up a bit http://i.imgur.com/D5eHeHe.png : 希望这可以使事情更加清晰http://i.imgur.com/D5eHeHe.png

在此处输入图片说明

You'd want to do something like this assuming you have a nested object: 假设您有一个嵌套的对象,您想执行以下操作:

function buildTree($element, $object) {
    var $ul = $('<ul></ul>');
    var $li;
    $object.forEach(function (item) {
        $li = $('<li>' + item.Name + '</li>');
        $ul.append($li);
        if (item.SubCategories.length > 0) {
            buildTree($ul, item.SubCategories);
        }
        $element.append($ul);
    });
}

here's a fiddle: http://jsfiddle.net/snowburnt/uJbE8/1/ 这是一个小提琴: http : //jsfiddle.net/snowburnt/uJbE8/1/

For SharePoint, you would send the RootWeb of the site collection and instead of subcategories you'd send the item.webs property. 对于SharePoint,您将发送网站集的RootWeb,而不是子类别,而是发送item.webs属性。 All of this using a javascript or jquery object, of course. 当然,所有这些都使用javascript或jquery对象。 You could do it some other way, but if you want to recurse it, it helps to have an object that contains other objects of the same "class" 您可以通过其他方式进行操作,但是如果要递归操作,则可以使一个对象包含相同“类”的其他对象会有所帮助

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

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