【问题标题】:How to make jquery pagination? [closed]jquery如何进行分页? [关闭]
【发布时间】:2018-02-06 22:57:14
【问题描述】:

我已经搜索了如何在 jquery (ajax) 中进行动态分页;在 php 中从数据库中获取数据,但我没有找到一个好的答案。我知道如何通过 php 来实现,但我需要通过 jquery 来实现,因为我的网站使用 ajax 加载。

我想要这样的例子jquery pagination

所以,如果有人有教程的链接...把它放在这里!,或者如果你能给出一个快速的解释把它。

如果有插件可以快速完成,那就太好了。

谢谢... :)

【问题讨论】:

  • 进行分页需要哪些数据?在你的 ajax 调用中返回这个数据就是这样。并且您以与 php 中相同的方式创建分页

标签: php jquery ajax pagination


【解决方案1】:

【讨论】:

  • 感谢您的回答,但我说我希望它像链接中的网站一样。它使用了漂亮的 jquery ajax 动画,我想要!
  • 基本上你想要做的是构建一个幻灯片和fadeOut()/fadeIn,但是你需要一次创建许多元素。这应该让你开始:plugins.jquery.com/project/fadeslideshow
【解决方案2】:

这里是ajaxified分页的完整解决方案

<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
        // loading page if page is refreshed or accessed directly from a bookmark
        load_page();

        $(window).bind('hashchange', function() {
            // loading page on hashchange event that is triggered by a click on pagination link
            load_page();
        });

        function load_page(page_number){
            var hash = window.location.hash;
            id = hash.split('-')[1];
            if(id > 0){
                // replace http://www.example.com/ with your site url
                $.get("http://www.example.com/page.php", { page_id: id }, function(data){
                    //updating the div with dynamically loaded data
                    $('#post').html(data);
                });
            }

        }
    });

</script>

<div id="post">

</div>

<div class="pagination">
    <a href="#page-1">1</a>
    <a href="#page-2">2</a>
    <a href="#page-3">3</a>
</div>

page.php

mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());

$id = (int) $_GET['page_id'];
$data = mysql_query("SELECT * FROM posts WHERE ID = $id") or die(mysql_error());

$post = mysql_fetch_array($data);
echo $post['post_content'];

下面是一个生成分页链接的jquery插件

simplePagination.js

【讨论】:

    【解决方案3】:

    使用:https://github.com/ahmedhijazi94/HZpagination.js

    $(document).ready(function(){
    
      var HZperPage = 2,//number of results per page
         HZwrapper = 'paginationTable',//wrapper class
         HZlines   = 'tableItem',//items class
         HZpaginationId ='pagination-container',//id of pagination container
         HZpaginationArrowsClass = 'paginacaoCursor',//set the class of pagi
         HZpaginationColorDefault =  '#880e4f',//default color for the pagination numbers
         HZpaginationColorActive = '#311b92', //color when page is clicked
         HZpaginationCustomClass = 'customPagination'; //custom class for styling the pagination (css)
       
    
       /*-------------------------F/ AHMED HIJAZI -------------------------*/
       /*------------------------- HOPE YOU LIKE -------------------------*/
    
    
      function paginationShow(){if($("#"+HZpaginationId).children().length>8){var a=$(".activePagination").attr("data-valor");if(a>=4){var i=parseInt(a)-3,o=parseInt(a)+2;$(".paginacaoValor").hide(),exibir2=$(".paginacaoValor").slice(i,o).show()}else $(".paginacaoValor").hide(),exibir2=$(".paginacaoValor").slice(0,5).show()}}paginationShow(),$("#beforePagination").hide(),$("."+HZlines).hide();for(var tamanhotabela=$("."+HZwrapper).children().length,porPagina=HZperPage,paginas=Math.ceil(tamanhotabela/porPagina),i=1;i<=paginas;)$("#"+HZpaginationId).append("<p class='paginacaoValor "+HZpaginationCustomClass+"' data-valor="+i+">"+i+"</p>"),i++,$(".paginacaoValor").hide(),exibir2=$(".paginacaoValor").slice(0,5).show();$(".paginacaoValor:eq(0)").css("background",""+HZpaginationColorActive).addClass("activePagination");var exibir=$("."+HZlines).slice(0,porPagina).show();$(".paginacaoValor").on("click",function(){$(".paginacaoValor").css("background",""+HZpaginationColorDefault).removeClass("activePagination"),$(this).css("background",""+HZpaginationColorActive).addClass("activePagination");var a=$(this).attr("data-valor"),i=a*porPagina,o=i-porPagina;$("."+HZlines).hide(),exibir=$("."+HZlines).slice(o,i).show(),"1"===a?$("#beforePagination").hide():$("#beforePagination").show(),a===""+$(".paginacaoValor:last").attr("data-valor")?$("#afterPagination").hide():$("#afterPagination").show(),paginationShow()}),$(".paginacaoValor").last().after($("#afterPagination")),$("#beforePagination").on("click",function(){var a=$(".activePagination").attr("data-valor"),i=parseInt(a)-1;$("[data-valor="+i+"]").click(),paginationShow()}),$("#afterPagination").on("click",function(){var a=$(".activePagination").attr("data-valor"),i=parseInt(a)+1;$("[data-valor="+i+"]").click(),paginationShow()}),$(".paginacaoValor").css("float","left"),$("."+HZpaginationArrowsClass).css("float","left");
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <!DOCTYPE html>
    <html>
    <head>
    	<title>HZpagination</title>
    	<style type="text/css">
    		.customPagination, .paginacaoCursor{
    			margin: 5px;
    			padding: 5px 8px;
    			color: #fff;
    			background: #880e4f;
    			cursor: pointer;
    		}
    	</style>
    </head>
    <body>
    
    
    	<ul class="paginationTable">
                  <li class="tableItem"> 1 </li>
                  <li class="tableItem"> 2 </li>
                  <li class="tableItem"> 3 </li>
                  <li class="tableItem"> 4 </li>
                  <li class="tableItem"> 5 </li>
                  <li class="tableItem"> 6 </li>
                  <li class="tableItem"> 7 </li>
                  <li class="tableItem"> 8 </li>
                  <li class="tableItem"> 9 </li>
                  <li class="tableItem"> 10 </li>
                  <li class="tableItem"> 11 </li>
                  <li class="tableItem"> 12 </li>
                  <li class="tableItem"> 13 </li>
                  <li class="tableItem"> 14 </li>
                  <li class="tableItem"> 15 </li>
              </ul>
              <div id="pagination-container">
                <p class='paginacaoCursor' id="beforePagination"> < </p>
                <p class='paginacaoCursor' id="afterPagination"> > </p>
              </div>
    
    
    	<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    	<script type="text/javascript" src="HZpagination.js"></script>
    
    </body>
    </html>

    【讨论】:

    • 嗨!尽管代码本身可能会说话,但提供一些细节将有助于提高您的答案质量!
    猜你喜欢
    • 2011-01-11
    • 2012-08-28
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 2012-01-03
    • 1970-01-01
    • 2017-02-06
    相关资源
    最近更新 更多