У меня есть список сайтов и сайтов на этих сайтах. Как создать дерево навигации с использованием элементов ul и li с учетом этого результата?
https://hosted.demo.ca
https://hosted.demo.ca/academic
https://hosted.demo.ca/academic/bm
https://hosted.demo.ca/academic/cmtte
https://hosted.demo.ca/academic/dm
https://hosted.demo.ca/academic/pm
https://hosted.demo.ca/archive
https://hosted.demo.ca/associations
https://hosted.demo.ca/associations/bm
https://hosted.demo.ca/associations/dm
https://hosted.demo.ca/associations/pm
https://hosted.demo.ca/cdn
https://hosted.demo.ca/cf_test
https://hosted.demo.ca/charity
https://hosted.demo.ca/charity/bod
https://hosted.demo.ca/charity/bod/boarddocs
https://hosted.demo.ca/charity/bod/mtgmaterial
https://hosted.demo.ca/clite
https://hosted.demo.ca/clite/admin
https://hosted.demo.ca/company
https://hosted.demo.ca/company/finance
https://hosted.demo.ca/company/hr
https://hosted.demo.ca/company/hr/b1
https://hosted.demo.ca/company/hr/b1/b2
https://hosted.demo.ca/company/hr/b1/b2/b3
https://hosted.demo.ca/company/mrkting
https://hosted.demo.ca/demo
Я хочу взять этот список и превратить его в нечто вроде этого:
<UL>
<li>Academic
<ul>
<li>BM</li>
<li>CMTTE</LI>
<li>DM</li>
<li>PM</li>
</ul>
</li>
</ul>
<ul>
<li>ARCHIVE</li>
</UL>
<ul>
<LI>ASSOCIATIONS
<ul>
<li>BM</li>
<li>DM</LI>
<li>PM</li>
</ul>
</LI>
</ul>
Кто-то предложил мне попробовать что-то вроде этого:
var map = {}; //init the map
var web = $(xData.responseXML).find("Web");
for (var i = 0; i < web.length; i++) {
//we create a index for our links based on the depth of them by '/'
var m = web[i].attributes['Url'].value.substring(23, web[i].attributes['Url'].value.length).split('/').length;
map[m] = map[m] || []; //make sure we leave alone the old values if there is none init with new array
map[m].push(web[i].attributes['Url'].value); //push new value to node
}
console.log(map);
Но возвращаемый им объект занял все сайты с одинаковым числом "/" и помещал их в один массив. Это не совсем то, что я ищу.
Посмотри на
var map = {}; //init the map
var web = $(xData.responseXML).find("Web")
.map(function () {
return $(this).attr('Url');
});
// create map for later use
for (var i = 0; i < web.length; i++) {
var item = web[i],
parts = item.split('/'),
domain = parts.splice(0, 3).join('/'),
current;
if (!map[domain]) map[domain] = {};
current = map[domain];
for (var index = 0, length = parts.length; index < length; index++) {
var part = parts[index];
if (!current[part]) {
current[part] = {};
}
current = current[part];
}
}
console.log(map);
// create DOM method
function traverseMap(obj, element) {
for (var index in parts) {
var li = $('<li>', {
text: item
}).appendTo(element);
if (!$.isEmptyObject(obj[item])) {
var ul = $('<ul>').appendTo(li);
traverseMap(obj[item], ul);
}
}
}
// invoke the DOM creating function
traverseMap(map, $('#root'));
Демо на http://jsfiddle.net/4gJ9T/
И с небольшими изменениями, чтобы удерживать/показывать полный URL-адрес (как ссылки)
Демо на http://jsfiddle.net/4gJ9T/2/
$('data')
на $(xData.responseXML)
или любую $(xData.responseXML)
переменную, в которой вы храните свои данные XML ..