metaduck
12Abr/100

Will_paginate and AJAX

I have been using will_paginate ever since I started on Rails.

It's a great plugin for classic page rendering, but what if you want to use it with AJAX, so it updates a document element (a floating window layer, for instance) with the results instead of reloading the page?

Well, inspired on Redline's blog post (which almost worked for the latest version of the will_paginate plugin), I have the solution.

Create a link renderer. I put in under lib/remote_link_renderer.rb so Rails always loads it on startup.

class RemoteLinkRenderer < WillPaginate::LinkRenderer

  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_for(page), :method => :get}.merge(@remote))
    else
      @template.content_tag :span, text, :class => span_class
    end
  end
end

On the view, when you are rendering the pager, do it like this:

<%= will_paginate @collection, :renderer => 'RemoteLinkRenderer' , :remote => {:with => ’value’, :update => ‘some_div’} %>

That's it!

About Pedro Teixeira

Pedro Teixeira is a former Enterprise Java Beans technical project manager for Clarity Europe (BES Tech Ventures). On 2004 he founded a services company ( http://berro.pt ), focused on the tourism industry throughout 2005-2008, briefly worked for http://www.expedita.com, shifted to product development on 2007, rebuilt http://netmadeira.com on 2009 for ZON Madeira. On early 2010 he started freelance work for Avallain, an e-Learning company strongly focused on delivering multi-media learning content and learning community expericences.
Filed under: Rails, Ruby Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.