Author-icon Posted By : Satish Chauhan @ 01 Aug, 2007 at 04:49PM

Pagination is a very common task in web application development. But sometimes we need to paginate something with ajax request. The interest to use Ajax for this is to provide a dynamic interface which doesn’t need to reload the entire page when next results(page) displayed. Ajax is really nicely integrated with Rails, and using it is very easier with this great framework. To work with ajax requests your browser must be enabled to handled java script requests. I knew two methods for that problem.



First Method

Your Controller code


def ajax_pagination_links
@posts_pages, @posts= paginate :posts, :per_page => 3, :order=> “id DESC”
respond_to do |format|
format.html {render :partial=>”posts”,:layout=>”application”}
format.js {render :update do |page|
page.replace_html ‘total_posts’, :partial=>’total_posts’
page.visual_effect :highlight, “total_posts”
end}
end
end

Layout


<%= javascript_include_tag :defaults %>

View


<%= render :partial=>'total_posts' %>

_total_posts.rhtml


<% for post in @posts %>
<%= truncate(h(post.title.capitalize!),50) -%>
<%= truncate(h(post.description.capitalize!),50) -%>
<%end -%>
<%= link_to_remote 'Previous page',{:url=>{:action=>ajax_pagination_links, :page => @posts_pages.current.previous }, if @posts_pages.current.previous -%>
<%= link_to_remote 'Next page', {:url=>{:action=>ajax_pagination_links, :page => @posts_pages.current.next} if @posts_pages.current.next -%>

Second Method

Put this code in a helper somewhere


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

and use following code for creating links

View


<%= ajax_pagination_links @posts_pages, {:params => {:posts => @posts} } %>

30 Comments to "How to paginate with ajax"
Heel said,
on 03 Jul, 2008 at 10:18PM
Your comment is awaiting moderation.
Bill said,
on 03 Jul, 2008 at 10:20PM
Your comment is awaiting moderation.
Hero said,
on 03 Jul, 2008 at 10:43PM
Your comment is awaiting moderation.
Diesel said,
on 03 Jul, 2008 at 10:49PM
Your comment is awaiting moderation.
Dominic said,
on 03 Jul, 2008 at 11:17PM
Your comment is awaiting moderation.
Arnie said,
on 03 Jul, 2008 at 11:20PM
Your comment is awaiting moderation.
Kir said,
on 03 Jul, 2008 at 11:58PM
Your comment is awaiting moderation.
Still said,
on 04 Jul, 2008 at 12:14AM
Your comment is awaiting moderation.
Still said,
on 04 Jul, 2008 at 12:25AM
Your comment is awaiting moderation.
Diesel said,
on 04 Jul, 2008 at 12:33AM
Your comment is awaiting moderation.
Arnie said,
on 04 Jul, 2008 at 12:53AM
Your comment is awaiting moderation.
Halo said,
on 04 Jul, 2008 at 12:59AM
Your comment is awaiting moderation.
Halo said,
on 04 Jul, 2008 at 01:29AM
Your comment is awaiting moderation.
Joe said,
on 04 Jul, 2008 at 01:46AM
Your comment is awaiting moderation.
Diesel said,
on 04 Jul, 2008 at 02:05AM
Your comment is awaiting moderation.
Hero said,
on 04 Jul, 2008 at 02:21AM
Your comment is awaiting moderation.
Heel said,
on 04 Jul, 2008 at 02:52AM
Your comment is awaiting moderation.
Diesel said,
on 04 Jul, 2008 at 03:01AM
Your comment is awaiting moderation.
Bill said,
on 04 Jul, 2008 at 03:18AM
Your comment is awaiting moderation.
Hero said,
on 04 Jul, 2008 at 03:41AM
Your comment is awaiting moderation.
Heel said,
on 04 Jul, 2008 at 03:44AM
Your comment is awaiting moderation.
Hero said,
on 04 Jul, 2008 at 04:09AM
Your comment is awaiting moderation.
Halo said,
on 04 Jul, 2008 at 04:14AM
Your comment is awaiting moderation.
Hero said,
on 04 Jul, 2008 at 04:22AM
Your comment is awaiting moderation.
Joe said,
on 04 Jul, 2008 at 04:59AM
Your comment is awaiting moderation.
Kir said,
on 04 Jul, 2008 at 05:20AM
Your comment is awaiting moderation.
Arnie said,
on 04 Jul, 2008 at 05:20AM
Your comment is awaiting moderation.
Arnie said,
on 04 Jul, 2008 at 05:22AM
Your comment is awaiting moderation.
Joe said,
on 04 Jul, 2008 at 05:34AM
Your comment is awaiting moderation.
Diesel said,
on 04 Jul, 2008 at 05:52AM
Your comment is awaiting moderation.

Leave a comment

( *required )
( *required )

Sponsored Links