简体   繁体   中英

Table pagination, displaying links

I have a table with around 350 rows in and i want to paginate it to make it more manageable for users. It paginates into groups of 10 and then shows all the page links. I would like the page links show as follows: 1,2,3,4 ... 35 (or whatever the last page might be). This is where i am now stuck..

This section of code works, but i can't work out how to display the links in such a way. Any help would be much appreciated.

$('table.table-styled').each(function() {
    var currentPage = 0;
    var rowsPerPage = 10;
    var $table = $(this); 

    $table.bind('repaginate', function(){
        $table.find('tbody tr').hide().slice(currentPage * rowsPerPage, (currentPage + 1) * rowsPerPage).show();
    });

    $table.trigger('repaginate');
    var numRows = $table.find('tbody tr').length;
    var numPages = Math.ceil(numRows / rowsPerPage);
    var $pager = $('<div class="pager"></div>');
    for (var page = 0; 
         page < numPages; page++){
        $('<span class="page-number"></span>').text(page + 1).bind('click', {
            newPage: page
        }, function(event){ console.log(currentPage);
            currentPage = event.data['newPage'];
            $table.trigger('repaginate');
            $(this).addClass('active').siblings().removeClass('active');


        }).appendTo($pager).addClass('clickable');

    }
    $pager.insertBefore($table).find('span.page-number:first').addClass('active');
});

thanks.

I created a small jQuery plugin some time ago called jQuery-paging . Although yours is using a static page (rather than loading the requested page as mine does) you should be able to adapt it to your needs.

The parts you're probably interested in are -

function addLi(ul, text, page) {
  var li = $("<li>" + text + "</li>");
  li.data("paging", { page: page });
  ul.append(li);
}

And -

$this.empty();
  var pages = parseInt(settings.pages);
  var currentPage = parseInt(settings.currentPage);
  var first = "<span class='ui-icon ui-icon-seek-first'></span>";
  var last = "<span class='ui-icon ui-icon-seek-end'></span>";
  var prev = "<span class='ui-icon ui-icon-seek-prev'></span>";
  var next = "<span class='ui-icon ui-icon-seek-next'></span>";
  var ul = $("<ul></ul>");
  $this.addClass("pagingList");
  $this.append(ul);
  if (currentPage > 1) {
    // Add first & prev
    addLi(ul, first, 1);
    addLi(ul, prev, currentPage - 1);
  }
  if (currentPage === pages && pages > 4) {
    // Add currentPage - 4
    addLi(ul, currentPage - 4, currentPage - 4);
  }
  if (currentPage > pages - 1 && pages > 3) {
    // Add currentPage - 3
    addLi(ul, currentPage - 3, currentPage - 3);
  }
  if (currentPage > 2) {
    // Add currentPage - 2
    addLi(ul, currentPage - 2, currentPage - 2);
  }
  if (currentPage > 1) {
    // Add currentPage - 1
    addLi(ul, currentPage - 1, currentPage - 1);
  }
  // Add current page
  addLi(ul, "<b>" + currentPage + "</b>", currentPage);
  if (pages > currentPage) {
    // Add current page + 1
    addLi(ul, currentPage + 1, currentPage + 1);
  }
  if (pages > currentPage + 1) {
    // Add current page + 2
    addLi(ul, currentPage + 2, currentPage + 2);
  }
  if (currentPage < 3 && pages > currentPage + 2) {
    // Add current page + 3
    addLi(ul, currentPage + 3, currentPage + 3);
  }
  if (currentPage === 1 && pages > currentPage + 3) {
    // Add current page + 4
    addLi(ul, currentPage + 4, currentPage + 4);
  }
  if (currentPage < pages) {
    // Add next & last
    addLi(ul, next, currentPage + 1);
    addLi(ul, last, pages);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM