Category: Pagination

AJAX Pagination

By , September 10, 2010 12:19 am

Earlier I writtern a blog about AJAX pagination in rails in which changes required in will_paginate files.

But now no change is required in will_paginates file(s) for ajax pagination.

For ajax pagination , you need to add following js code in application.js file.

Note: Add spinner.gif image in images folder .


//For AJAX Pagination:

document.observe("dom:loaded", function() {
// the element in which we will observe all clicks and capture
// ones originating from pagination links
var container = $(document.body)

if (container) {
var img = new Image
img.src = '/images/spinner.gif'

function createSpinner() {
return new Element('img', { src: img.src, 'class': 'spinner' })
}

container.observe('click', function(e) {
var el = e.element()
if (el.match('.pagination.ajax a')) {
el.up('.pagination.ajax').insert(createSpinner())

new Ajax.Request(el.href, { method: 'get' })
e.stop()
}
})
}
})

Call AJAX pagination: Add class pagination ajax  in method


<%=will_paginate @object,:class=>'pagination ajax'%>

Call  Non AJAX pagination: Simply call will_paginate method


<%=will_paginate @object%>

Set your controller method according you ajax / non-ajax call

For ajax call , you need to update a div ,


@object = Model.paginate :page=>params[:page],:per_page => 10

render  :update do |page|

page.replace_html :div_id ,:partial => 'result_page'

end

I hope this will help make simple ajax pagination.

AJAX Pagination in rails

By , June 17, 2009 4:29 am

Visit more proper way ajax pagination in rails:  Click here

Pagination is a very common feature of web application development. Sometimes we need pagination with ajax.

I found lot of blogs and discussion on this but none of them provide better and easy way solution.

Finally I found one of easy solution based on will_paginate gem. For this you have apply below patch in will_paginate/lib/view_helper.rb file

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]
@template.link_to_remote text, :update => @options[:update], :url =>url_for(page)
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

Now add  below code in your view file.


<%= will_paginate @users, :update=>'DIV_ID',:params=>{:controller=>'CONTROLLER_NAME',:action=>'ACTION_NAME'},:container => false %>

Let me know if you have any better solution for this or you faced any problems in this.

Panorama Theme by Themocracy