AJAX Pagination
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.



