Wednesday, December 22, 2010

AJAX Pagination


or ajax pagination , you need to add following js code in application.js file.
Note: Add spinner.gif image in images folder .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//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
1
<%=will_paginate @object,:class=>'pagination ajax'%>
Call  Non AJAX pagination: Simply call will_paginate method
1
<%=will_paginate @object%>
Set your controller method according you ajax / non-ajax call
For ajax call , you need to update a div ,
1
2
3
4
5
6
7
@object = Model.paginate :page=>params[:page],:per_page => 10
 
render  :update do |page|
 
page.replace_html :div_id ,:partial => 'result_page'
 
end

No comments:

Post a Comment