web-dev-qa-db-ja.com

Jqueryでの動的ページ付け

私はこのコードを持っています:

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

HTMLファイルに手動で数値を書き込む代わりに、表示するdivの数に応じて数値を自動的に生成する必要があります。

私が持っているコードは機能しますが、3ページと4ページには何もありません。HTMLファイルの数値を更新する代わりに、Jqueryで動的に変更したいです。

6

ループを介してページ数を計算し、ページへのリンクを作成します。

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

Math.ceil($(".line-content").size() / pageSize)を使用してページをカウントしてから、ページごとに<li>を動的に追加する必要があります。

初期化コードを$()内に移動しました(つまり、Readyイベント)。

//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();
  });
}
.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>
3
Ankit

これは、DOMの準備ができたときに実行される場所ですが、クリックイベントハンドラーが追加される前に配置します。

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

免責事項:このコードはテストしていません。

2
Anders
//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
Priya varshney
//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>

これはあなたが必要としていたものにインラインです。

1
joyBlanks

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
nazanin.h

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
Uddesh Keshri