2011-03-11 13 views
6

üretmek için, ben derinlik unsuru ne kadar derin listesinde bir keyfi derin listesini temsil nesneler dizisi ...kullanın jquery bir keyfi derin listesine javascript olarak

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

... var .

Bu verileri html'ye dönüştürmek istiyorum. Örneğin

:

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

olur ...

<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> 

Bunu yapmanın en kolay yolu jQuery kullanarak, nedir

? Sadece listify bir isim istedik

(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); 

itiraf:

Teşekkür

+0

o düzen korunur bunlar gerekli mi? Ya da böyle olabilir mi?

  • bir

  • beş

  • iki

    • üç

      • dört

cevap

7

Burada yapabileceğini bir yoludur. Here is a jsFiddle. Öyle gibi çağırır:

$.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

Çok güzel, teşekkürler. 'Listify' isimlendirmek için bonus puanları hak ediyorsunuz. – trev

1

Bu seferki dinamiktir:

http://jsfiddle.net/wq8QY/

olursa olsun tüm yol aşağı hala oluşturulacak gitmek kaç seviye. doğru birden çok düzeyde ve birlikte

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

Works etiketlerini nests:

(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)); 
     });