SimplePager - an easy jquery paging plugin

This plugin has been updated with new features. Check out the blog post for the update

In a recent project, a need came up for a very simple jQuery paging plugin that would page between a small amount of data on the client side.  A quick search online didn't find any suitable candidates, so naturally we decided the best thing to do was to write one.

Introducing quickPager, simple jquery pager plugin.

A few caveats before dishing out the code - simplePager was written for a distinct set of requirements, and we're releasing it here as much as a learning tool as a fully formed jQuery plugin.

  • Before using a client-side javascript pager, decide if server-side paging wouldn't suit your needs more.  Server-side paging is usually recommended
  • This plugin will only work on one element per page
  • This plugin breaks chaining (!)

So whilst it's definitely far from perfect, this little plugin fulfills the need for an easy to use, lightweight client-side pager.

Here's the code:

//-------------------------------------------------
//		Quick Pager jquery plugin
//		www.geckonewmedia.com
//-------------------------------------------------

(function($) {
	    
	$.fn.quickPager = function(options) {
	
		var defaults = {
            pageSize: 10,
            currentPage: 1,
			holder: ""
    	};
    	var options = $.extend(defaults, options);
	  	
		//leave this
		var selector = $(this);
		var totalRecords = $(this).children().length;
		var pageCounter = 1;

		selector.children().each(function(i){
			if(i < pageCounter*options.pageSize && i >= (pageCounter-1)*options.pageSize) {
				$(this).addClass("page"+pageCounter);
			}
			else {
				$(this).addClass("page"+(pageCounter+1));
				pageCounter ++;
			}	
		});
		 
		//show/hide the appropriate regions 
		selector.children().hide();
		$(".page"+options.currentPage).show();
		
		//first check if there is more than one page. If so, build nav
		if(pageCounter > 1) {
			
			//Build pager navigation
			var pageNav = "";
			
			if(options.holder == "") {
				selector.after(pageNav);
			}
			else {
				$(options.holder).append(pageNav);
			}
						
			//pager navigation behaviour
			$(".pageNav a").live("click", function() {			
				//grab the REL attribute 
				var clickedLink = $(this).attr("rel");
				options.currentPage = clickedLink;
				//remove current current (!) page
				$("li.currentPage").removeClass("currentPage");
				//Add current page highlighting
				$(this).parent("li").addClass("currentPage");
				//hide and show relevant links				
				selector.children().hide();			
				selector.find(".page"+clickedLink).show();
				return false;
			});
			
		}
			  
	}
	

})(jQuery);

Options

There are deliberately few options for the script - they are as follows:

  • pageSize: The amount of items per page (default 10)
  • currentPage: Want to start on a page other than one?  Put it in here
  • Holder: By default the paging nav will be inserted after the chosen element to be paged.  This allows you to create en element to hold the nav

Usage

The default usage is as follows

$(document).ready(function() { 
	$(ul.pageme").quickPager();
});

This will page an unordered list with the default options. (demo)

$(document).ready(function() { 
	$("table.pageme tbody").quickPager( {
		pageSize: 5,
		currentPage: 1,
		holder: ".pager"
	});
});

The above code will page table rows (provided a tbody element is used) with 5 rows per page, putting the navigation into an element with a class of "pager". (demo)

Download the plugin and demos here.

 

22 comment(s) for “SimplePager - an easy jquery paging plugin”

  1. 30 Jul - 2:50AM

    Gravatar for MarcMarc

    It should be working fine but I can't seem to integrate it into my source:

    < ul class = "paging" >

    //foreach($reacties as $row) {

    < li >< p class="info" >
    //echo $row['comment']; ?>< /p >< /li >
    }
    < /ul >

    I want to get a new page every 10 comments. Any idea to help me out?

    1. 3 Aug - 1:32AM

      Gravatar for DanDan

      Hi Marc

      This would depend on the markup generated. If the output is a simple list then it should work as in the example above. What is the final html markup generated by your code above?

      Dan

  2. 6 Aug - 3:04AM

    Gravatar for CemCem

    first; sorry for my bad english, and many thans for the small but great pager plugin.

    i have a problem;

    i set 2 div with same class name for pagenav holder. its work fine, but selected(current) page number does not activated.

    for example; i click 2. page, its highlighted, but other pagenav number does not highlighted.

    any solution ?

    thanks.

    1. 6 Aug - 1:44AM

      Gravatar for DanDan

      Hi Cem

      A great suggestion! I've updated the plugin to allow for multiple pager navs and have each nav retain it's "currently active" state.

      You can now see this in effect in demo 2 linked above, and you can just re-download and replace the plugin file to take advantage of the new feature.

      Dan

  3. 8 Aug - 8:43AM

    Gravatar for CemCem

    thats great !

    thanks for update Dan.

    nice work

  4. 15 Aug - 9:25AM

    Gravatar for krikekrike

    I'm building a cms tutorial system and on the profile page I show the latest tutorials of the user and his last favorites.

    So I would need 2 different paginations on the same page. is that possible with your plugin?

    thanks anyway

    1. 17 Aug - 1:13AM

      Gravatar for DanDan

      Hi Krike

      That is pretty high on my list of improvements to make to the script - I'll see if I can get that added this week. Unfortunately it may make it a bit less "simple", as I can imagine having to specify a holder for each pager navigation.

      I'll update this post when I get around to updating the pager script.

      Dan

      1. 17 Aug - 6:44AM

        Gravatar for krikekrike

        no rush, take your time :)

        1. 20 Aug - 4:11AM

          Gravatar for DanDan

          Hi Krike

          We've updated the plugin to allow for multiple instances on a single page - check the link in the post above.

          Dan

          1. 21 Aug - 4:04AM

            Gravatar for krikekrike

            wow, that was fast. thanks :D

  5. 18 Aug - 4:07AM

    Gravatar for chanleechanlee

    Thanks Dan.
    It's good pager plugin.

    I'm newbie with jquery. But, try to add prev and next link to page navigation.

    http://chanlee.tistory.com/attachment/cfile3.uf@127D490E4A8A138F93A281.zip

    It's work. ^^;;

    Just using and evaluate it.

    1. 18 Aug - 11:49AM

      Gravatar for DanDan

      Hi Chanlee

      Nice work! I'll take a look at this and perhaps fold it back in to the original plugin.

      Cheers,
      Dan

  6. 19 Aug - 5:01AM

    Gravatar for devdev

    Hi I am a total newbie to jquery/programming. Was wondering if you could make it work with the Asual jquery address plugin (http://www.asual.com/jquery/address/)

    OR maybe give me a few pointers.

    any help would be really appreciated.

    Many thanks
    Dev

    1. 20 Aug - 4:12AM

      Gravatar for DanDan

      Hi Dev

      I've never actually seen that plugin before, but thanks for pointing it out to me. I'll definitely have a look at it and see if it makes sense to make the two plugins work together.

      Dan

      1. 2 Sep - 1:55AM

        Gravatar for devdev

        Thanks Dan. Much appreciate it.

  7. 8 Jul - 10:44AM

    Gravatar for KSKS

    Hi, When I use it with dynamically generated page by Ajax, the quickPager() is not working. If I use it whenever the dynamic page is changed, the navigation number set shown multiple. I thing there is a method like reload() or stop(), it would be great. Do you have any solution? Thanks from Korea

    1. 8 Jul - 10:47AM

      Gravatar for DanDan

      Hi KS

      You could try calling the plugin only when the ajax on the page has completed? Also, you should use the updated version of the plugin at http://www.geckonewmedia.com/blog/2009/8/20/simplepager---jquery-paging-plugin--updated

      Cheers,
      Dan

      1. 9 Jul - 11:33AM

        Gravatar for ksks

        I posted this in the site for new version of this plugin. Anyway, I also tried to call the plugin when the Ajax is completed, but the paging number set shown several times (like 1 2 3 1 2 3 1 2 3 )
        I use your plugin with div sets (not with UL and LI sets). it works well at the first time.

        1. 9 Jul - 11:35AM

          Gravatar for DanDan

          Do you have a test page I could have a look at?

          Dan

  8. 14 Jul - 12:11PM

    Gravatar for puppypuppy

    Great man

  9. 26 Jul - 3:34AM

    Gravatar for SEOSEO

    Thank you, that has me very helped.

  10. 29 Jul - 8:04AM

    Gravatar for madatanicmadatanic

    You sir helped me a bunch. Thank you!

  11.  

Have your say…

  1. HTML is not allowed!

  2. Do not fill this in

© 2010 Gecko New Media Ltd, All Rights Reserved.