ü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
o düzen korunur bunlar gerekli mi? Ya da böyle olabilir mi?
bir
beş
iki
üç
dört