Ryo
Ryo

Reputation: 131

re build ul and li tag with Jquery

I try to find the way to split ul tag by showing only 10 li tag per ul.

Suppose I have li 30 elements. script will be re build into 3 ul and each ul has 10 li tag.

How can I do this ?

suppose 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 re build ul in 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 guide, Thanks

Upvotes: 3

Views: 122

Answers (9)

Ja͢ck
Ja͢ck

Reputation: 173642

Using an earlier answer, you could group those elements together like so:

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

Upvotes: 0

Varun
Varun

Reputation: 1946

My try:

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

Upvotes: 0

Balachandran
Balachandran

Reputation: 9637

Try it:

var liLength=$("li").length;
for(var i=0;i<liLength;i=i+10){
     $("li:eq("+i+"),li:gt("+i+"):lt("+(i+9)+")").wrapAll( "<div id=ui"+(i/10)+" />");
}

DEMO

Upvotes: 0

Bogdan Kuštan
Bogdan Kuštan

Reputation: 5577

I would create separate functions for chunking array and displaying objects, something like:

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

demo fiddle

chunk function shamelessly taken from Split array into chunks

Upvotes: 0

Ryo
Ryo

Reputation: 131

thks for your answer :) .

I also just get the solution here's what im 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>

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82241

You can use:

var lis = $("ul > li");
for(var i = 0; i < lis.length; i+=20) {
    lis.slice(i, i+20).wrapAll("<ul id='ul"+(parseInt(i)/20+1)+"'></li>");
}
$("ul > ul").unwrap();

Working Demo

Upvotes: 0

Shaunak D
Shaunak D

Reputation: 20636

You can use .nth-child(10n) to iterate over each 10th element.

$("#ul1 li:nth-child(10n)").each(function(){
   $(this).prevAll('li').andSelf().wrapAll('<ul/>') 
});

Fiddle

Edit :

$("#ul1 li:nth-child(10n)").each(function(i){
    $(this).prevAll('li').andSelf().wrapAll($('<ul/>',{ id: 'ul'+(i+1)})) 
});

Upvotes: 0

bjfletcher
bjfletcher

Reputation: 11518

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

Upvotes: 0

Anoop Joshi P
Anoop Joshi P

Reputation: 25537

You can do like this

var n = 5;
var uls = $("#ul1 li").length / n;
for (i = 0; i < uls; i++) {
    var newUl = $("<ul/>", { id: "ul" + (i + 2) });
    $("ul").eq(i).after(newUl);
    newUl.append(newUl.prev().find("li:gt(" + (n - 1) + ")"));

}

Change the value of n according to your need

Fiddle

Upvotes: 1

Related Questions