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)
我的XML非常简单(不是正确的值)
<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.
我使用过jQuery的.filter()和。 find()每个()没有任何成功,它没有给我很好的破折号来指示我在哪个级别,它只是吐出文件夹名称。
My code that traverses
我的代码遍历
$(data).find("FolderName").each(function(){
var folderName = $(this).text();
$("#folderLevels").append(folderName+"<br/>");
});
2 个解决方案
#1
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.
好的,所以从我的理解,我们需要建立一个菜单,具有一定数量的前面的破折号,取决于孩子的水平。幸运的是,jQuery让这很容易。
$(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
希望有所帮助。这里的主要帮助者是parents()方法。 http://api.jquery.com/parents
#2
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;
}
#1
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.
好的,所以从我的理解,我们需要建立一个菜单,具有一定数量的前面的破折号,取决于孩子的水平。幸运的是,jQuery让这很容易。
$(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
希望有所帮助。这里的主要帮助者是parents()方法。 http://api.jquery.com/parents
#2
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;
}