0

I am going nuts trying to figure this out, I am trying to build a simple menu tree with multi levels

RootFolder
- First Sub Folder
-- First Sub Sub Folder
--- First Sub Sub Sub Folder
--- First Sub Sub Sub Sub Folder
- Second Sub Folder
- Second Sub Sub Folder

My XML is very simple (not the right values)

<OrgFolderDetails>
    <FolderName>Main Folder</FolderName>
    <TheChildren>
        <OrgFolderDetails>
            <FolderName>First Sub Folder</FolderName>
            <TheChildren>
                <OrgFolderDetails>
                    <FolderName>First Sub Sub Folder</FolderName>
                    <TheChildren>
                        <OrgFolderDetails>
                            <FolderName>First Sub Siub Sub Folder</FolderName>
                            <TheChildren>
                                <OrgFolderDetails>
                                    <FolderName>First Sub Sub Sub Sub Folder</FolderName>
                                    <TheChildren/>
                                </OrgFolderDetails>
                            </TheChildren>
                        </OrgFolderDetails>
                    </TheChildren>
                </OrgFolderDetails>
            </TheChildren>
        </OrgFolderDetails>
        <OrgFolderDetails>
            <FolderName>Second Sub Folder</FolderName>
            <TheChildren>
                <OrgFolderDetails>
                    <FolderName>Second Sub Sub Folder</FolderName>
                    <TheChildren/>
                </OrgFolderDetails>
            </TheChildren>
        </OrgFolderDetails>
    </TheChildren>
</OrgFolderDetails>

I have used jQuery's .filter() and. find() each() without any success, it doesn't give me the nice dashes to indicate which level I am in, it just spits out the folder names.

My code that traverses

 $(data).find("FolderName").each(function(){
             var folderName = $(this).text();

            $("#folderLevels").append(folderName+"<br/>");
         });
2
  • Can you post your jQuery also, please? Commented May 23, 2012 at 2:54
  • ahren, thanks, I forgot to add my initial snippet. My concept/approach was trying to locate the position and counting the nested position and adding the # of dashes... No success there. Commented May 23, 2012 at 3:03

2 Answers 2

1

Okay, so from what I understand, we need to build a menu, with a certain number of preceding dashes, dependent on the level of the child. Lucky, jQuery makes this kinda easy.

$(data).find("FolderName").each(function(){ 
    var levels = $(this).parents("OrgFolderDetails").size();
    var text = $(this).text();
    var html = "";

    for(var j=0; j < levels; j++){
        html += "-";    
    }
    html += " "+text+"</br>"; //add in that space after the dashes, and the <br>

    $("#folderLevels").append(html);
});

Hope that helps. The main helper in this is the parents() method. http://api.jquery.com/parents

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

1 Comment

OMG, are you serious... I see your approach, that works well, thank you very much... your code teaches me a lot, much appreciated!
0

I think you will want to use a recursive function for this.

var s = getNestedString($(data), 0);

function getNestedString(d, level) {
    // get the name of current node
    var s = d.children("FolderName").text() + "<br/>";

    // prepend with dashes for inner levels
    for (var i=0 ; i<level ; i++) {
        s = "-" + s;
    }

    // get children and call this function recursively (incrementing level)
    var ch = d.children("TheChildren").children("OrgFolderDetails");
    for (var i=0 ; i < ch.length ; i++) {
        s = s + getNestedString(ch[i], level+1);
    }

    // return string for current and nested nodes
    return s;
}

2 Comments

I will check this out too, thanks for the quick response, I was ripping my hair out!
@GuMingPeng I don't think mine works actually, plus ahren's answer is better (simpler). I thought you could use children since it exists in JQuery, but apparently not.

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.