Topic: Pagination with AJAX

Hi,

I have a page that has several paginated lists on it. When someone selects next ot previous page on one of the lists, I'd like for only that list to be updated in an AJAXy way.

I'ev found a few postts on blogs and places, but none of them seem to give the full story. For example, one gives a controller method, then says 'then just write the appropriate RJS and you're done'.

Well, sadly, I have no idea what would be the appropriate RJS. I've been at this a few days now, and finally have decided that maybe I should just ask if anyone knows of any examples or instructions on how to do this.

TIA

Re: Pagination with AJAX

The RJS may be as simple as this:

list.rjs

page.replace_html 'list', :partial => 'controller/action'

The _list.rhtml view would have, at the minimum:
<div id="list"></div>

I would probably use a link_to_remote for the previous and next links in a seperate partial, or the same partial (I use partials too much, I think), passing the variable of the last or first id listed.

I'm learning this as well, but that's how I would start out.

Re: Pagination with AJAX

# In your application_helper.rb

  def ajax_pagination_links(paginator, options={})

    options.merge!(ActionView::Helpers::PaginationHelper::DEFAULT_OPTIONS) {|key, old, new| old}

    window_pages = paginator.current.window(options[:window_size]).pages

    return if window_pages.length <= 1 unless

    options[:link_to_current_page]

    first, last = paginator.first, paginator.last

    returning html = '' do

      if options[:always_show_anchors] and not window_pages[0].first?

        html << link_to_remote(first.number, :update => options[:update], :url => { options[:name] => first }.update(options[:params] ))

        html << ' ... ' if window_pages[0].number - first.number > 1

        html << ' '

      end

      window_pages.each do |page|

        if paginator.current == page && !options[:link_to_current_page]

          html << page.number.to_s

        else

          html << link_to_remote(page.number, :update => options[:update], :url => { options[:name] => page }.update(options[:params] ))

        end

        html << ' '

      end

      if options[:always_show_anchors] && !window_pages.last.last?

        html << ' ... ' if last.number - window_pages[-1].number > 1

        html << link_to_remote(paginator.last.number, :update => options[:update], :url => { options[:name] => last }.update( options[:params]))

      end

    end

  end


# In your views
<%= ajax_pagination_links(@WHATEVER_pages, {:update => 'DIV_TO_UPDATE', :window_size => 10, :params => {:action => :ACTION }}) %>

Re: Pagination with AJAX

Thanks guys, I got it going.