#67 ✓invalid
Florent Piteau

Ajax Support

Reported by Florent Piteau | July 18th, 2007 @ 03:11 AM

Here is a first patch which adds Ajax calls to Will Paginate.

It can be very useful if you have a lots of models with pagination in the same page. You will only update one at the time ( and you can put effects between each pages ;)

Be careful, you have to put the will_paginate call inside the element you want to update in order to also update the pagination control. Maybe, i'll improve this later ( or maybe you can already do it outside with RJS but I haven't tried )

Example usage:

In your controller :

@posts = Post.paginate :page => params[:page_posts], :per_page => 10

In your view :

    <% for post in @posts -%>

  1. Render `post` in some nice way.
  2. <% end -%>

Now let's render us some pagination!

<%= will_paginate @posts,{

:param_name => 'page_posts',

:remote_options => {

:method => :get,

:update => "div_posts",

:before => "Element.show('busy_paginate')",

:success => "Element.hide('busy_paginate')"

}

} %>

Comments and changes to this ticket

  • Chris Wanstrath

    Chris Wanstrath July 18th, 2007 @ 03:11 AM

    • State changed from “new” to “open”
    • Assigned user changed from “Chris Wanstrath” to “Mislav”
  • Matt Aimonetti (mattetti)

    Matt Aimonetti (mattetti) September 28th, 2007 @ 09:52 PM

    I don't think the plugin itself should handle any ajax calls.

    simply use unobtrusive javascript and you can achieve the same result in less than 5 minutes without hacking anything ;)

    http://railsontherun.com/2007/9/...

    And I'm pretty sure you can probably do the same with Prototype only, Mislav?

    -Matt

  • Mislav

    Mislav September 29th, 2007 @ 10:21 AM

    • State changed from “open” to “invalid”
    • Title changed from “[Will Paginate][Patch] Ajax Support” to “Ajax Support”

    I agree with Matt. I kept this open to give me time to think in what way could I help Ajax pagination ... I realized that the user is on his own, because I'm definitely not generating JavaScript in the view (I know that Rails does, but I don't).

  • Ayyanar
  • Ayyanar

    Ayyanar December 16th, 2007 @ 09:41 PM

    Hi, I updated my views_helper.rb with the lines from will_paginate_with_ajax_calls.diff. Iam not able to get the pagination working. So many errors. I attached my view_helpers.rb. Plz let me know where Iam missing.

  • Tom

    Tom December 13th, 2007 @ 05:41 PM

    The lowpro.js approach works great if you only have one pagination area on a page. But I have a couple of cases where I'd like to have more than one paginator on the page which would need to be handled via different actions. And will_paginate uses the params from the current template to generate the URL used by the paginator. So I am stuck with one action for all paginators and no way to differentiate between them.

    Can you give us a way to override the URL used to create the links in will_paginator?

  • bdoyle

    bdoyle December 13th, 2007 @ 06:44 PM

    @Tom

    I had this same problem and I modified the will paginate code to take a url in the options and it's working great. In my template file I do something like:

    <%= will_paginate @guides, :url => {:action => 'show_guides', :login => params[:login] }, :class => 'author_guides' %>

    I had to make a slight change in the LinkRenderer#page_link_or_span method:

    def page_link_or_span(page, span_class, text)

    unless page

    @template.content_tag :span, text, :class => span_class

    else

    1. page links should preserve GET/POST parameters

    if @options[:url]

    @options[:url][:page] = page

    @template.link_to text, @options[:url]

    else

    @template.link_to text, @template.params.merge(param => page != 1 ? page : nil)

    end

    end

    end

  • Clemens Kofler

    Clemens Kofler April 22nd, 2008 @ 11:18 PM

    I stumbled across this ticket looking for the exact same thing.

    I haven't come up with a solution yet, but I think the best way would be to write your custom renderer (like WillPaginate::ViewHelpers::LinkRenderer) to render remote links instead of regular ones. After that, AJAX pagination is just a simple <%= will_paginate @posts, :renderer => "AjaxPaginationRenderer" %> away (or one could set it up as the default renderer in environment.rb or an initializer).

  • Aketzu

    Aketzu May 21st, 2008 @ 07:57 AM

    I solved it with little patch to LinkRenderer.page_link_or_span()

        def page_link_or_span(page, span_class, text = nil)
          text ||= page.to_s
          classnames = Array[*span_class]
    
          if page and page != current_page
            if @options[:update] then
              @template.link_to_remote text, {:url => url_for(page), :method => :get, :update => @options[:update]}, {:rel => rel_value(page), :class => classnames[1] }
            else
              @template.link_to text, url_for(page), :rel => rel_value(page), :class => classnames[1]
            end
          else
          @template.content_tag :span, text, :class => classnames.join(' ')
          end
        end
    

    and then just use <%= will_paginate @permits, :update => :entries %>

  • Clemens Kofler

    Clemens Kofler May 21st, 2008 @ 08:37 AM

    Aketzu:

    Not the best way, I think, especially because your not supplying a :href option for the link_to_remote which makes it unusable for people with JavaScript deactivated. Plus sometimes you may want not only to run Ajax.Updater (which :update => something does) but a real Ajax.Request ...

    I've come to like Matt's lowpro approach - it's clean and it just works! :)

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Everyone's favorite Ruby library for pagination of practically anything!

Referenced by

Pages