Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination : Remove element using jquery?

I have a section where i have to paginate records. Now the issue is the number gets added but i want to remove the prev number

I have currently having something as below:

Prev 1 2 3 Next

When i click on next 4 gets added and it becomes Prev 1 2 3 4 Next

When i click on next again 5 gets added and it becomes Prev 1 2 3 4 5 Next

What i am trying to do and what should be having

Prev 1 2 3 Next

When i click on next 4 gets added and it should become Prev 2 3 4 Next

When i click on next again 5 gets added and it becomes Prev 3 4 5 Next

When i click on Previous Now it should be Prev 2 3 4 Next

Please find my runnable code below:

$('ul.pagination').on('click', 'a', function() { // listen for click on pagination link
    if($(this).hasClass('active')) return false;
  
    var active_elm = $('ul.pagination a.active');
  
    if(this.id == 'next'){
      var _next = active_elm.parent().next().children('a');
      if($(_next).attr('id') == 'next') {
        
        // appending next button if reach end
        var num = parseInt($('a.active').text())+1;
        active_elm.removeClass('active');
        $('<li><a class="active" href="#">'+num+'</a></li>').insertBefore($('#next').parent());
        return; 
      }
      _next.addClass('active');   
      
      
      
      
    }
    else if(this.id == 'prev') {
      var _prev = active_elm.parent().prev().children('a');
      if($(_prev).attr('id') == 'prev') return false;
      _prev.addClass('active');   
    } else {
      $(this).addClass('active');
    }
    active_elm.removeClass('active');
    
});
ul.pagination {
    display: inline-block;
    padding: 0;
    margin: 0;
}

ul.pagination li {display: inline;}

ul.pagination li a {
    color: black;
    float: left;
    padding: 8px 16px;
    text-decoration: none;
    transition: background-color .3s;
    border: 1px solid #ddd;
}

ul.pagination li a.active {
    background-color: #4CAF50;
    color: white;
    border: 1px solid #4CAF50;
}

ul.pagination li a:hover:not(.active) {background-color: #ddd;}

div.center {text-align: center;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="center">
  <ul class="pagination">
    <li><a id="prev">«</a></li>
    <li><a id="test1" href="#">1</a></li>
    <li><a id="test2" class="active" href="#">2</a></li>
    <li><a href="#">3</a></li>
    <li><a href="#" id="next">»</a></li>
  </ul>
</div>
like image 722
sTg Avatar asked Nov 28 '16 07:11

sTg


2 Answers

Use this after adding next number

$('#prev').parent().next().remove();

Codepen http://codepen.io/mastersmind/pen/ZBJZjo

like image 170
mastermind Avatar answered Sep 22 '22 11:09

mastermind


Here is how I would do it.
I would add a class to the 3 <li> tags with the numbers inside for easy access with the jQuery selector. Inside the if-statement where you check weather the next button has the id next, remove the first child like so:

$('.three_links').first().remove();

and add the next number after the last child like so:

$('.three_links').last().after('<li class="three_links"><a class="active" href="#">'+num+'</a></li>');

and if the id is prev remove the last child and add the previous number before the first child but only if the previous number is greater than 1:

if(num > 0){
    active_elm.removeClass('active');
    $('.three_links').last().remove();
    $('.three_links').first().before('<li class="three_links"><a class="active" href="#">'+num+'</a></li>');
}

Here is a working jsfiddle.

html:

<div class="center">
    <ul class="pagination">
        <li><a href="#" id="prev">«</a></li>
        <li class="three_links"><a id="test1" href="#">1</a></li>
        <li class="three_links"><a id="test2" class="active" href="#">2</a></li>
        <li class="three_links"><a id="test3" href="#">3</a></li>
        <li><a href="#" id="next">»</a></li>
    </ul>
</div>

js:

$('ul.pagination').on('click', 'a', function() { // listen for click on pagination link
    if($(this).hasClass('active')) return false;

    var active_elm = $('ul.pagination a.active');

    if(this.id == 'next'){
        var _next = active_elm.parent().next().children('a');
        if($(_next).attr('id') == 'next') {

            // appending next button if reach end
            var num = parseInt($('a.active').text())+1;
            active_elm.removeClass('active');
            $('.three_links').first().remove();
            $('.three_links').last().after('<li class="three_links"><a class="active" href="#">'+num+'</a></li>');
            return; 
        }
        _next.addClass('active');   
    }
    else if(this.id == 'prev') {
        var _prev = active_elm.parent().prev().children('a');
        if($(_prev).attr('id') == 'prev'){
            var num = parseInt($('a.active').text())-1;
            if(num > 0){
                active_elm.removeClass('active');
                $('.three_links').last().remove();
                $('.three_links').first().before('<li class="three_links"><a class="active" href="#">'+num+'</a></li>');
            }
            return;
        }
        _prev.addClass('active');   
    } else {
        $(this).addClass('active');
    }
    active_elm.removeClass('active');
});
like image 29
Timo Avatar answered Sep 19 '22 11:09

Timo