Created
November 23, 2009 16:27
-
-
Save bcalloway/241153 to your computer and use it in GitHub Desktop.
Ajax pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app/views/users/_results.html.haml | |
%table.table | |
- @users.each do |user| | |
%tr | |
%td | |
= h user.name | |
%td | |
= link_to 'Show', user | |
| | |
= link_to 'Edit', edit_user_path(user) | |
| | |
= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete | |
= will_paginate :renderer => 'RemoteLinkRenderer', :remote => {:update => 'all-events', :loading => "$('#spinner').show()", :complete => "$('#spinner').hide()"} | |
// This assumes there is an animated gif named "spinner.gif" in the images directory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app/views/users/index.html.haml | |
= image_tag("spinner.gif", :align => "absmiddle", :border => 0, :id => "spinner", :style =>"display: none;" ) | |
#all-results | |
= render 'results' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/helpers/remote_link_renderer.rb | |
class RemoteLinkRenderer < WillPaginate::LinkRenderer | |
def prepare(collection, options, template) | |
@remote = options.delete(:remote) || {} | |
super | |
end | |
protected | |
def page_link(page, text, attributes = {}) | |
@template.link_to_remote(text, {:url => url_for(page), :method => :get}.merge(@remote), attributes) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/user.rb | |
def self.all_users(page) | |
paginate(:page => page, :per_page => 10) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/users_controller.rb | |
def index | |
@users = User.all_users(params[:page]) | |
respond_to do |format| | |
format.html | |
format.js { render :partial => 'results' } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment