我有一个数组中的对象列表
var myLevel = ['sub', 'topic', 'type']
var myCollection = new Array();
myCollection.push({sub: 'Book 1', topic: 'topic 1', type: 'mcq'});
myCollection.push({sub: 'Book 1', topic: 'topic 1', type: 'mcq'});
myCollection.push({sub: 'Book 1', type: 'fib', topic: 'topic 2'});
myCollection.push({sub: 'Book 1', topic: 'topic 1', type: 'mtf'});
myCollection.push({sub: 'Book 1', topic: 'topic 1', type: 'mcq'});
myCollection.push({sub: 'Book 2', type: 'mcq', topic: 'topic 1'});
myCollection.push({sub: 'Book 2', topic: 'topic 1', type: 'mcq'});
myCollection.push({sub: 'Book 2', topic: 'topic 1', type: 'mcq'});
我想用ul和li将这些内容转换为正确的列表,列表的级别也依赖于myLevel变量。
每个部分都有一个独特的孩子,它不能有相同的。
我尝试将元素创建到myCollection的循环中
for(var i=0; i<myCollection.length; i++)
{
for(var j=0; j<myLevel.length; j++)
{
createMyTree(myCollection[i][myLevel[j]);
}
}
function createMyTree(str)
{
//?????????????
}
请帮助我如何在所需级别创建元素。
答案 0 :(得分:1)
好的,所以我重新编写它并且除非出现一个问题,否则它会起作用:主题1得分为两个主题1,因为主题2在它们之间插入。
var myLevel = ['sub', 'topic', 'type'];
var myCollection = new Array();
myCollection.push({sub: 'Book 1', topic: 'topic 1', type: 'mcq'});
myCollection.push({sub: 'Book 1', topic: 'topic 1', type: 'mcq'});
myCollection.push({sub: 'Book 1', type: 'fib', topic: 'topic 2'});
myCollection.push({sub: 'Book 1', topic: 'topic 1', type: 'mtf'});
myCollection.push({sub: 'Book 1', topic: 'topic 1', type: 'mcq'});
myCollection.push({sub: 'Book 2', type: 'mcq', topic: 'topic 1'});
myCollection.push({sub: 'Book 2', topic: 'topic 1', type: 'mcq'});
myCollection.push({sub: 'Book 2', topic: 'topic 1', type: 'mcq'});
var dom="<ul>";
var used = new Array();
var currentItems = new Array(myLevel.length);
var lastJ = -1;
for(var i=0; i<myCollection.length; i++)
{
var con = false;
for(var k=0; k<used.length; k++){
if(compareObjects(used[k], myCollection[i]))
con = true;
}
if(!con){
for(var j=0; j<myLevel.length; j++){
if(currentItems[j] !== myCollection[i][myLevel[j]]){
if(lastJ !== -1){
for(var l=0; l<lastJ-j; l++){
dom+="</ul></li>";
}
}
for(var l=j+1; l<currentItems.length; l++)
currentItems[l] = "";
currentItems[j] = myCollection[i][myLevel[j]];
dom+="<li>"+currentItems[j]+(j<myLevel.length-1?"<ul>":"");
lastJ = j;
}
}
used.push(myCollection[i]);
}
}
dom+="</ul>";
$('body').html(dom);
function compareObjects(obj1, obj2){
if(obj1.length != obj2.length)
return false;
for(var el in obj1){
if(obj2[el] === undefined)
return false;
if(obj1[el] !== obj2[el])
return false;
}
return true;
}
答案 1 :(得分:0)
经过大量的努力后我似乎已经得到了一些技巧。
var $out;
$(document).ready(function()
{
for(var i=0; i<myCollection.length; i++)
{
$out = $('#out');
for(var j=0; j<levelTree.length; j++)
{
var itemName = myCollection[i][levelTree[j]];
var level = j;
/**********************************************************/
var $con;
if($out.children().eq(0).length == 0)
{
$con = $('<ul></ul>');
$con.append('<li>'+ itemName + '</li>');
$out.append($con);
}
else
{
var flag = $out.children().eq(0).children().filter(function(i, e)
{
return (this.childNodes[0].textContent == itemName);
});
console.log(flag.length);
if(flag.length == 0)
{
$out.children().eq(0).append('<li>' + itemName + '</li>');
}
}
var ele = $out.children(0).eq(0).children().filter(function(i, e)
{
return (this.childNodes[0].textContent == itemName);
}).get();
$out = $(ele);
/**********************************************************/
}
}
});
通过递归可以提高效率。但它起作用了。 :d