Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
ajax call with will_paginate
4 Posts
ajax call with will_paginate

Hi,

could any one please let me know, is there any possibility to use will_paginate plug-in as an ajax call.

I could implement the pagination with will_paginate, but my requirement is like on click of page links the part of the page should be updated with new data.

suppose say a table displayed with 5 records, by clicking on 'Next' link, it should display the next 5 rows without reloading the page.

I have tried the examples got from the net, but all are working as refreshing whole page.

Please direct me to a solution which gets my requirements done.

Thanks! Balu.

Hey Balu,

Matt Aimonetti has written a blog post on his blog (Railsontherun.com) which you can read here: http://railsontherun.com/2007/9/27/ajax-pagination-in-less-than-5-minutes

I believe this is probably what you're looking for.

Good luck!

Balakrishna,

I use my own Renderer to do this, and it works well!

The following code will display the pagination links using my custom link renderer:

'RemoteLinkRenderer' %>

Then, I have my renderer defined as:

class RemoteLinkRenderer < WillPaginate::LinkRenderer

def initialize(collection, options, template)
  @remote = options.delete(:remote) || {}
  super
end
def page_link_or_span(page, span_class = 'current', text = nil)
  text ||= page.to_s
  if page and page != current_page
    @template.link_to_remote(text, {:url => url_options(page), :method => :get}.merge(@remote)) 
  else
    @template.content_tag :span, text, :class => span_class
  end
end

end

This file is put in as app/helpers/remote_link_renderer.rb.

Don't forget that this will NOT update your pagination links as you click on the page numbers! So, if you use any kind of "current page state" to signify which page the user is on, remember that you'll need to use some additional javascript or RJS to update your will_paginate div.

Let me know if this helps you out. I'd be curious to see what you do with it.

-Kevin Elliott "WeLikeFood Beta":http://www.welikefood.com/ Discover great restaurants. Meet great food enthusiasts with your taste.

Just a side note if SEO is a concern. I implemented this but I reverted to the classic pagination for SEO. I will implement it again adding a handler to turn the click into an ajax call if javascript is enabled.

Is this a valid concern or is there another way around it so the bots can follow my links. Thanks

4 Posts
Login to add your message