Learning jQuery - Marking the Current Page
Our pager can be made more user-friendly by highlighting the current page number. We just need to update the classes on the buttons every time one is clicked:
var $pager = $('</p> <div class="pager"></div> '); for (var page = 0; page < numPages; page++) { $('<span class="page-number">' + (page + 1) + '</span>') .bind('click', {'newPage': page}, function(event) { currentPage = event.data['newPage']; repaginate(); $(this).addClass('active').siblings().removeClass('active'); }) .appendTo($pager).addClass('clickable'); } $pager.find('span.page-number:first').addClass('active'); $pager.insertBefore($table);
var%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.bind%28%27click%27%2C%20%7B%27newPage%27%3A%20page%7D%2C%20function%28event%29%20%7B%20%0A%0D%0AcurrentPage%20%3D%20event.data%5B%27newPage%27%5D%3B%20%0A%0D%0Arepaginate%28%29%3B%20%0A%0D%0A%24%28this%29.addClass%28%27active%27%29.siblings%28%29.removeClass%28%27active%27%29%3B%20%0A%0D%0A%7D%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.find%28%27span.page-number%3Afirst%27%29.addClass%28%27active%27%29%3B%20%0A%0D%0A%24pager.insertBefore%28%24table%29%3B
Now we have an indicator of the current status of the pager:

Paging with Sorting
We began this discussion by noting that sorting and paging controls needed to be aware of one another to avoid confusing results. Now that we have a working pager, we need to make sort operations respect the current page selection.
Doing this is as simple as calling our repaginate() function whenever a sort is performed. The scope of the function, though, makes this problematic. We can't reach repaginate() from our sorting routine because it is contained inside a different $(document).ready() handler. We could just consolidate the two pieces of code, but instead let's be a bit sneakier. We can decouple the behaviors, so that a sort calls the repaginate behavior if it exists, but ignores it otherwise. To accomplish this, we'll use a handler for a custom event.
In our earlier event handling discussion, we limited ourselves to event names that were triggered by the web browser, such as click and mouseup. The .bind() and .trigger() methods are not limited to these events, though; we can use any string as an event name. In this case, we can define a new event called repaginate as a stand-in for the function we've been calling:
$table.bind('repaginate', function() { $table.find('tbody tr').show() .lt(currentPage * numPerPage) .hide() .gt((currentPage + 1) * numPerPage - 1) .hide() });
%24table.bind%28%27repaginate%27%2C%20function%28%29%20%7B%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%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
Now in places where we were calling repaginate(), we can call:
$table.trigger('repaginate');
%24table.trigger%28%27repaginate%27%29%3B%20
We can issue this call in our sort code as well. It will do nothing if the table does not have a pager, so we can mix and match the two capabilities as desired.
Trackback(0)
|