Learning jQuery - JavaScript Pagination
Let's examine how we would add JavaScript pagination to the table we have already made sortable in the browser. First, we'll focus on displaying a particular page of data, disregarding user interaction for now:
$(document).ready(function() { $ ('table.paginated'). each(function() { var currentPage = 0; var numPerPage = 10; var $table = $(this); $table.find('tbody tr').show() .lt(currentPage * numPerPage) .hide() .gt((currentPage + 1) * numPerPage - 1) .hide() }); });
%24%28document%29.ready%28function%28%29%20%7B%20%0A%0D%0A%24%28%27table.paginated%27%29.each%28function%28%29%20%7B%20%0A%0D%0Avar%20currentPage%20%3D%200%3B%20%0A%0D%0Avar%20numPerPage%20%3D%2010%3B%20%0A%0D%0Avar%20%24table%20%3D%20%24%28this%29%3B%20%0A%0D%0A%24table.find%28%27tbody%20tr%27%29.show%28%29%20%0A%0D%0A.lt%28currentPage%20%2A%20numPerPage%29%20%0A%0D%0A.hide%28%29%20%0A%0D%0A.end%28%29%20%0A%0D%0A.gt%28%28currentPage%20%2B%201%29%20%2A%20numPerPage%20-%201%29%20%0A%0D%0A.hide%28%29%20%0A%0D%0A.end%28%29%3B%20%0A%0D%0A%7D%29%3B%20%0A%0D%0A%7D%29%3B
This code displays the first page—ten rows of data.
Once again we rely on the presence of a &tl; tbody > element to separate data from headers; we don't want to have the headers or footers disappear when moving on to the second page. For selecting the rows containing data, we show all the rows first, then select the rows before and after the current page, hiding them.
The method chaining supported by jQuery makes another appearance here when we filter the set of matched rows twice, using .end() in between to pop the current filter off the stack and start afresh with a new filter.
The most error-prone task in writing this code is formulating the expressions to use in the filters. To use the .lt() and .gt() methods, we need to find the indices of the rows at the beginning and end of the current page. For the beginning row, we just multiply the current page number by the number of rows on each page.
Multiplying the number of rows by one more than the current page number gives us the beginning row of the next page; to find the last row of the current page, we must subtract one from this.
Displaying the Pager
To add user interaction to the mix, we need to place the pager itself next to the table. We could do this by simply inserting links for the pages in the HTML markup, but this would violate the progressive enhancement principle we've been espousing. Instead, we should add the links using JavaScript, so that users without scripting available are not misled by links that cannot work.
To display the links, we need to calculate the number of pages and create a corresponding number of DOM elements:
var numRows = $table.find('tbody tr').length; var numPages = Math. ceil(numRows / numPerPage ); var $pager = $('</p> <div class="pager"></div> '); for (var page = 0; page < numPages; page++) { $('<span class="page-number">' + (page + 1) + '</span>') .appendTo($pager).addClass('clickable'); } $pager.insertBefore($table);
var%20numRows%20%3D%20%24table.find%28%27tbody%20tr%27%29.length%3B%20%0A%0D%0Avar%20numPages%20%3D%20Math.ceil%28numRows%20%2F%20numPerPage%29%3B%20%0A%0D%0Avar%20%24pager%20%3D%20%24%28%27%3C%2Fp%3E%0D%0A%3Cdiv%20class%3D%22pager%22%3E%3C%2Fdiv%3E%0D%0A%27%29%3B%20%0A%0D%0Afor%20%28var%20page%20%3D%200%3B%20page%20%3C%20numPages%3B%20page%2B%2B%29%20%7B%20%0A%0D%0A%24%28%27%3Cspan%20class%3D%22page-number%22%3E%27%20%2B%20%28page%20%2B%201%29%20%2B%20%27%3C%2Fspan%3E%27%29%20%0A%0D%0A.appendTo%28%24pager%29.addClass%28%27clickable%27%29%3B%20%0A%0D%0A%7D%20%0A%0D%0A%24pager.insertBefore%28%24table%29%3B%20
The number of pages can be found by dividing the number of data rows by the number of items we wish to display on each page. If the division does not yield an integer, we must round the result up using Math.ceil() to ensure that the final partial page will be accessible. Then, with this number in hand, we create buttons for each page and position the new pager before the table:

Trackback(0)
|