Re build ul and li tag with JQuery
I am trying to find a way to split the ul tag, showing only 10 li tags per ul.
Suppose I have li 30 items. The script will be rebuilt to 3 ul and each ul has a 10 li tag.
How can i do this?
suppose the original is:
<ul id="ul1" style="">
<li><a href="#"><span>Adidas</span></a></li>
<li><a href="#"><span>jason markk</span></a></li>
<li>... 28 mores </li>
</ul>
Jquery will return ul to 3 ul (10 li per ul):
<ul id="ul1" style="">
<li>10 times</li>
</ul>
<ul id="ul2" style="">
<li>10 times</li>
</ul>
<ul id="ul3" style="">
<li>10 times</li>
</ul>
Please Thanks
+3
source to share
9 replies
My attempt:
var no=$("#ul1").children().size();
var count=0,tmpstr='';
for(var j=0;j<no;j++)
{
if(count<10)
{
tmpstr=tmpstr+"<li>"+$("#ul1 li:nth-of-type("+(j+1)+")").html()+"</li>";
count++;
}
if(count==10 || j==(no-1))
{
$(".uj").append("<ul>"+tmpstr+"</ul>");
tmpstr='';
count=0;
}
}
0
source to share
Good question.
var parent = $(".container");
var items = parent.find("li");
var ul = $("<ul/>");
var counter = 0;
for (; counter < items.length; counter++) {
if (counter > 0 && counter % 10 === 0) {
parent.append(ul);
ul = $("<ul/>"); // new list
}
ul.append(items[counter].detach());
}
0
source to share
You can use to repeat every 10th element. .nth-child(10n)
$("#ul1 li:nth-child(10n)").each(function(){
$(this).prevAll('li').andSelf().wrapAll('<ul/>')
});
Edit :
$("#ul1 li:nth-child(10n)").each(function(i){
$(this).prevAll('li').andSelf().wrapAll($('<ul/>',{ id: 'ul'+(i+1)}))
});
0
source to share
thks for your answer :).
I also just got a solution here, what I am doing:
<script>
$(document).ready(function(){
var itemindex = 0;
var Jlistobj = null;
$('#list li').each(function()
{
if (itemindex % 10 == 0)
{
Jlistobj = $("<ul></ul>");
}
Jlistobj.append($(this));
$('#out_div').append(Jlistobj);
itemindex++;
});
});
</script>
0
source to share
I would create separate functions to split the array and display objects, for example:
Array.prototype.chunk = function(chunkSize) {
var array=this;
return [].concat.apply([],
array.map(function(elem,i) {
return i%chunkSize ? [] : [array.slice(i,i+chunkSize)];
})
);
}
renderUl = function(items) {
var ul = $('<ul>');
$.each(items, function(i, item){
var li = $('<li>');
li.html($(item).html());
li.appendTo(ul);
});
return ul;
}
var items = $('#original li').toArray();
var chunked = items.chunk(10);
var output = $('<div />');
$.each(chunked, function(i, items){
output.append(renderUl(items));
})
$('#original').replaceWith(output);
The chunk function is shamelessly taken from Split an array into chunks
0
source to share
Using an earlier answer , you can group these items like this:
$.fn.every = function(n) {
var arr = [];
for (var i = 0; i < this.length; i += n) {
arr.push(this.slice(i, i + n));
}
return this.pushStack(arr, "every", n);
}
var id = 1; // current id
$('#ul1 > li')
.slice(10) // skip first ten
.every(10) // group every ten thereafter
.each(function() {
// create new <ul> and place after the last one
$('#ul' + id)
.after($('<ul>', {id: 'ul' + ++id}).append(this));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="ul1" style="">
<li><a href="#"><span>Adidas</span></a></li>
<li><a href="#"><span>jason markk</span></a></li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
<li>... 28 mores </li>
</ul>
0
source to share