Jquery中的动态分页

时间:2015-08-10 13:04:36

标签: javascript jquery pagination jquery-pagination

我有这段代码:



//Pagination
	pageSize = 8;

	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())) 
	});
&#13;
.current {
  color: green;
}

#pagin li {
  display: inline-block;
}
&#13;
<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">
            <li><a class="current" href="#">1</a></li>
            <li><a href="#">2</a></li>
            <li><a href="#">3</a></li>
            <li><a href="#">4</a></li>
</ul>
&#13;
&#13;
&#13;

我不想在我的HTML文件中手动编写数字,而是希望根据要显示的div数自动生成数字。

我的代码有效,但第3页和第4页没有任何内容。我希望它们能够使用Jquery动态更改

,而不是更新HTML文件中的数字。

7 个答案:

答案 0 :(得分:5)

计算页数而不是通过循环创建指向页面的链接。

//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>

答案 1 :(得分:3)

您需要使用Math.ceil($(".line-content").size() / pageSize)统计网页,然后为每个页面动态添加<li>

我已将初始化代码移到$()内(即Ready事件)。

&#13;
&#13;
//Pagination
pageSize = 8;

$(function() {
  var pageCount = Math.ceil($(".line-content").size() / pageSize);

  for (var i = 0; i < pageCount; i++) {
    if (i == 0)
      $("#pagin").append('<li><a class="current" href="#">' + (i + 1) + '</a></li>');
    else
      $("#pagin").append('<li><a href="#">' + (i + 1) + '</a></li>');
  }


  showPage(1);

  $("#pagin li a").click(function() {
    $("#pagin li a").removeClass("current");
    $(this).addClass("current");
    showPage(parseInt($(this).text()))
  });

})

showPage = function(page) {
  $(".line-content").hide();

  $(".line-content").each(function(n) {
    if (n >= pageSize * (page - 1) && n < pageSize * page)
      $(this).show();
  });
}
&#13;
.current {
  color: green;
}
#pagin li {
  display: inline-block;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 2 :(得分:2)

将它放在DOM准备就绪时执行的地方,但是在添加click事件处理程序之前。

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

然后您可以将#pagin留空,JavaScript会为您填写:

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

Disclaimar:我还没有测试过这段代码。

答案 3 :(得分:1)

//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>

这符合您的需要。

答案 4 :(得分:1)

&#13;
&#13;
//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())) 
	});
&#13;
.current {
  color: green;
}

#pagin li {
  display: inline-block;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 5 :(得分:0)

如果您希望在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);
});

答案 6 :(得分:0)

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

}