will_paginate with ajax

It is very easy to handle ajax pagination as described on this post here.

I want to give one full example.

Here is how my form looks:

<% form_remote_tag :url => {
                      :action => 'handle_urls',
                      :script_id => @script,
                      :page => params[:page],
                    },
  :html => {:id => 'f'},
  :update => "filtered_articles_pane" do %>

Here is how my will_paginate helper looks:

<%= will_paginate @filtered_urls,
  :renderer => 'RemoteLinkRenderer',
  :remote => { :with => "'script_id=#{@script.id}&approved_flag='+$(\'approved_flag\')",
  :update => 'filtered_articles_pane'} %>

I have used the improved version of the will_paginate:

# Use it like so…
# will_paginate :collection, :remote => {:with => 'value', :update => 'some_div'} 

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

Update for will_paginate 2.3.x

# app/helpers/remote_link_renderer.rb

class RemoteLinkRenderer < WillPaginate::LinkRenderer
  def prepare(collection, options, template)
    @remote = options.delete(:remote) || {}
    super
  end

protected
  def page_link(page, text, attributes = {})
    @template.link_to_remote(text, {:url => url_for(page), :method => :get}.merge(@remote))
  end
end

1 Comment

  1. Thanks!

    I had to code a couple of workarounds, but this article got me on track!

Comments are closed.

© 2024 Gudasoft

Theme by Anders NorénUp ↑