2011-03-11 25 views
6

在JavaScript中,我有對象的數組,表示一個任意深列表...使用jQuery生成任意深度列表

data = 
[ 
{ title, depth }, 
{ title, depth }, 
{ title, depth }, 
{ title, depth }, 
] 

...其中深度是元素有多深名單。

我想將此數據轉換爲html。

例如:

[ 
{ title: "one", depth : 1 }, 
{ title: "two", depth : 1 }, 
{ title: "three", depth : 2 }, 
{ title: "four", depth : 3 }, 
{ title: "five", depth : 1 }, 
] 

成爲...

<ul> 
    <li><p>one</p></li> 
    <li> 
    <p>two</p> 
    <ul> 
     <li> 
     <p>three</p> 
     <ul> 
      <li><p>four</p></li> 
     </ul> 
     </li> 
    </ul> 
    </li> 
    <li><p>five</p></li> 
</ul> 

什麼是做到這一點的最簡單的方法,使用jQuery?

感謝

+0

是否有必要,訂單維持?或者它可以是這樣嗎?

  • 一個

2011-03-11 19:40:01

回答

7

這裏是你可以做的一種方式:

(function($) { 
    $.listify = function(items) { 
    var container = $('<div></div>'), root = container; 
    var depth = 0; 
    for (var i = 0; i < items.length; ++i) { 
     var item = items[i]; 
     if (item.depth > depth) { 
     while (item.depth > depth) { 
      var ul = $('<ul></ul>').appendTo(container); 
      var li = $('<li></li>').appendTo(ul); 
      container = li; 
      ++depth; 
     } 
     } else if (item.depth <= depth) { 
     while (item.depth < depth) { 
      container = container.parent().parent(); 
      --depth; 
     } 
     container = $('<li></li>').insertAfter(container); 
     } 
     container.append('<p>' + item.title + '</p>'); 
    } 
    return root.children(); 
    } 
})(jQuery); 

我承認,我只是想listify命名的東西。 Here is a jsFiddle.你會調用它像這樣:

$.listify([ 
    { title: "one", depth : 1 }, 
    { title: "two", depth : 1 }, 
    { title: "three", depth : 2 }, 
    { title: "four", depth : 3 }, 
    { title: "five", depth : 1 }, 
]).appendTo(document.body); 
+1

非常酷,謝謝。您將其命名爲'listify',您將獲得獎勵積分。 – trev 2011-03-11 20:56:23

1

這一個是動態的:

http://jsfiddle.net/wq8QY/

不管你多少級往下走它仍然會被創建的所有道路。

var json = [ 
{ title: "one", depth : 1 }, 
{ title: "two", depth : 10 }, 
{ title: "three", depth : 20 }, 
{ title: "four", depth : 25 }, 
{ title: "five", depth : 11 }, 
] 
    var dom = $('body') ; 
    dom.append($('<ul>')) 
    //console.log(json.length) 
    for(var i = 0; i < json.length; i++){ 
    //console.log(json[i]) 
     var appendTo = $('body') 
     for(var j = 1; j <= json[i].depth; j++){ 
      console.log(appendTo, json[i], json[i].depth, '1st') 
      if(appendTo.children('ul').length <= 0){ 
       console.log('no ul', j) 
       appendTo.append($('<ul>')) 
      } 
      appendTo = $(appendTo.children('ul')) 
      console.log(appendTo, json[i], '2nd') 
     } 
     console.log(appendTo, json[i], 'appending') 
     appendTo.append($('<li><p>'+json[i].title+'</p></li>')) 
    } 
0
$.fn.listify = function(list) { 
    var stack = [this]; 
    $.each(list, function() { 
     var item = $('<li><p>'+this.title+'</p></li>'); 
     stack[this.depth] = stack[this.depth] 
      ? item.insertAfter(stack[this.depth]) 
      : item.wrap('<ul>').appendTo(stack[this.depth - 1]); 
     stack.length = this.depth + 1; 
    }); 
} 
3

作品多層次,正確巢標籤:

(function ($) { 
    $.makelist = function (arr) { 
    var currentdepth = 1; 
    var root = $('<ul>'); 
    var el = root; 
    var idx = 0; 

    while (idx < arr.length) { 
     if (arr[idx].depth == currentdepth) { 
     el.append($('<li>').html($('<p>').text(arr[idx].title))); 
     idx++; 
     } 
     else if (arr[idx].depth > currentdepth) { 
     newel = $('<ul>'); 
     el.append(newel); 
     el = newel; 
     currentdepth++; 
     } 
     else { 
     el = el.parent('ul'); 
     currentdepth--; 
     } 
    } 
    return root; 
    } 
})(jQuery); 

     $(document).ready(function() { 
      arr = [ 
        { title: "one", depth: 1 }, 
        { title: "two", depth: 1 }, 
        { title: "three", depth: 2 }, 
        { title: "four", depth: 3 }, 
        { title: "five", depth: 1 }, 
        ]; 

      $('body').append($.makelist(arr)); 
     });