Dynamic pagination in Jquery

前端 未结 7 2166
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-06 10:59

I have this code :



        
相关标签:
7条回答
  • 2021-02-06 11:09

    Place this somewhere it is executed when the DOM is ready, but before the click event handlers are added.

    //How many pages do we want?
    elementCount = $('.line-content').size();
    pageCount = Math.ceil(elementCount / pageSize);
    
    //Add the buttons.
    buttons = '';
    for(i=1; i<=pageCount; i++) {
        buttons += '<li><a href="#">' + i + '</a></li>');
    }
    $('#pagin').html(buttons);
    

    Then you can just leave #pagin empty and the JavaScript will fill it for you:

    <ul id="pagin">
    </ul>
    

    Disclaimar: I haven't tested this code.

    0 讨论(0)
  • 2021-02-06 11:10

    calculate page count than via a loop create links to pages.

    //Pagination
    	pageSize = 8;
    
    	var pageCount =  $(".line-content").length / pageSize;
        
         for(var i = 0 ; i<pageCount;i++){
            
           $("#pagin").append('<li><a href="#">'+(i+1)+'</a></li> ');
         }
            $("#pagin li").first().find("a").addClass("current")
        showPage = function(page) {
    	    $(".line-content").hide();
    	    $(".line-content").each(function(n) {
    	        if (n >= pageSize * (page - 1) && n < pageSize * page)
    	            $(this).show();
    	    });        
    	}
        
    	showPage(1);
    
    	$("#pagin li a").click(function() {
    	    $("#pagin li a").removeClass("current");
    	    $(this).addClass("current");
    	    showPage(parseInt($(this).text())) 
    	});
    .current {
      color: green;
    }
    
    #pagin li {
      display: inline-block;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <div class="line-content">1 I have some content</div>
    <div class="line-content">2 I have some content</div>
    <div class="line-content">3 I have some content</div>
    <div class="line-content">4 I have some content</div>
    <div class="line-content">5 I have some content</div>
    <div class="line-content">6 I have some content</div>
    <div class="line-content">7 I have some content</div>
    <div class="line-content">8 I have some content</div>
    <div class="line-content">9 I have some content</div>
    <div class="line-content">10 I have some content</div>
    <div class="line-content">11 I have some content</div>
    <div class="line-content">12 I have some content</div>
    
    <ul id="pagin">
             
    </ul>

    0 讨论(0)
  • 2021-02-06 11:12

    you can use these code for pagination if you will have wanted appending to tag in html .

    var paginationfooter = {};
    paginationfooter.Pager = function() {
        this.currentPage = 1;
        this.pagingnumber = "#pagingnum";
        this.tagg = "#matn";
        
        this.numPages = function() {
            var numPages = 0;
            if (this.paragraphs != null && this.limit != null) {
                numPages = Math.ceil(this.paragraphs.length / this.limit);
            }
         
            return numPages;
        };
        
        this.showPage = function(page) {
            this.currentPage = page;
            var html = "";
            for (var i = (page-1)*this.limit; i < ((page-1)*this.limit) + this.limit; i++) {
                if (i < this.paragraphs.length) {
                    var disply = this.paragraphs.get(i);
                    html += "<" + disply.tagName + ">" + disply.innerHTML + "</" + disply.tagName + ">";
                }
            }
            
            $(this.tagg).html(html);
            
            pagenuming(this.pagingnumber, this.currentPage, this.numPages());
        }
        
        var pagenuming = function(container, currentPage, numPages) {
    		var paging = $("<div class='pagination'></div>");
    		
            var gridpaging = "<ul>";
            for (var i = 1; i <= numPages; i++) {
                if (i != currentPage) {
                     gridpaging += "<li><a href='#' onclick='pager.showPage(" + i + "); return false;'>" + i + "</a></li>";
                } else {
                    gridpaging += "<li>" + i + "</li>";
                }
            }
    
            gridpaging += "</ul>";
    		
    
    	paging.append(gridpaging);
        $(container).html(paging);
        }
    
    }
    //---------------------------here input values
    var pager = new paginationfooter.Pager();
    $(document).ready(function() {
    	pager.limit = 5; 
    	pager.pagingtag = $('#matn'); 
    	pager.paragraphs = $('p', pager.pagingtag); 
    	pager.showPage(1);
    });

    0 讨论(0)
  • 2021-02-06 11:13

    //Pagination
    pageSize = 8;
    
    showPage = function(page) {
      $('.line-content').hide();
      $('.line-content:gt('+((page-1)*pageSize)+'):lt('+(page)*(pageSize-1)+')').show();
       $('.line-content:eq('+((page-1)*pageSize)+')').show();
    }
    
    var pgs = Math.ceil($('.line-content').length/pageSize);
    var pgnt = '';
      for(var i=1;i<=pgs;i++){
    	pgnt += '<li><a href="#">'+i+'</a></li>';
    }
    $('#pagin').html(pgnt);
    $("#pagin li a").click(function() {
    	
      $("#pagin li a").removeClass("current");
    	$(this).addClass("current");
    	showPage(parseInt($(this).text())) 
    });
    showPage(1);
    .current {
      color: green;
    }
    
    #pagin li {
      display: inline-block;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <div class="line-content">1 I have some content</div>
    <div class="line-content">2 I have some content</div>
    <div class="line-content">3 I have some content</div>
    <div class="line-content">4 I have some content</div>
    <div class="line-content">5 I have some content</div>
    <div class="line-content">6 I have some content</div>
    <div class="line-content">7 I have some content</div>
    <div class="line-content">8 I have some content</div>
    <div class="line-content">9 I have some content</div>
    <div class="line-content">10 I have some content</div>
    <div class="line-content">11 I have some content</div>
    <div class="line-content">12 I have some content</div>
    
    <ul id="pagin">
    
    </ul>

    This is inline to what you needed.

    0 讨论(0)
  • 2021-02-06 11:24

    //Pagination
    	pageSize = 8;
    
    	var pageCount =  $(".line-content").length / pageSize;
        
         for(var i = 0 ; i<pageCount;i++){
            
           $("#pagin").append('<li><a href="#">'+(i+1)+'</a></li> ');
         }
            $("#pagin li").first().find("a").addClass("current")
        showPage = function(page) {
    	    $(".line-content").hide();
    	    $(".line-content").each(function(n) {
    	        if (n >= pageSize * (page - 1) && n < pageSize * page)
    	            $(this).show();
    	    });        
    	}
        
    	showPage(1);
    
    	$("#pagin li a").click(function() {
    	    $("#pagin li a").removeClass("current");
    	    $(this).addClass("current");
    	    showPage(parseInt($(this).text())) 
    	});
    .current {
      color: green;
    }
    
    #pagin li {
      display: inline-block;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <div class="line-content">1 I have some content</div>
    <div class="line-content">2 I have some content</div>
    <div class="line-content">3 I have some content</div>
    <div class="line-content">4 I have some content</div>
    <div class="line-content">5 I have some content</div>
    <div class="line-content">6 I have some content</div>
    <div class="line-content">7 I have some content</div>
    <div class="line-content">8 I have some content</div>
    <div class="line-content">9 I have some content</div>
    <div class="line-content">10 I have some content</div>
    <div class="line-content">11 I have some content</div>
    <div class="line-content">12 I have some content</div>
    
    <ul id="pagin">
             
    </ul>

    0 讨论(0)
  • 2021-02-06 11:28

    pageSize = 5;

    var pageCount =  $(".setPagination").length / pageSize;
    
    for(var i = 0 ; i<pageCount;i++){
        $("#pagin").append('<li><a href="#">'+(i+1)+'</a></li> ');
    }
    $("#pagin li").first().addClass("active")
    showPage = function(page) {
        $(".setPagination").hide();
        $(".setPagination").each(function(n) {
            if (n >= pageSize * (page - 1) && n < pageSize * page)
                $(this).show();
        });        
    }
    showPage(1);
    
    $("#pagin li").click(function() {
        $("#pagin li").removeClass("active");
        $(this).addClass("active");
        showPage(parseInt($(this).text())) 
    });
    

    }

    0 讨论(0)
提交回复
热议问题