-
-
Save jeroenr/3142686 to your computer and use it in GitHub Desktop.
module ApplicationHelper | |
def paginate(collection, params= {}) | |
will_paginate collection, params.merge(:renderer => RemoteLinkPaginationHelper::LinkRenderer) | |
end | |
end |
source 'https://rubygems.org' | |
gem 'rails', '3.2.3' | |
# Gems used only for assets and not required | |
# in production environments by default. | |
group :assets do | |
gem 'sass-rails', '~> 3.2.3' | |
gem 'coffee-rails', '~> 3.2.1' | |
# See https://github.com/sstephenson/execjs#readme for more supported runtimes | |
gem 'therubyracer', :platform => :ruby | |
gem 'uglifier', '>= 1.0.3' | |
end | |
gem 'jquery-rails' | |
gem 'will_paginate', '~> 3.0' |
<div id="page_links"> | |
<%= paginate @results %> | |
</div> |
module RemoteLinkPaginationHelper | |
class LinkRenderer < WillPaginate::ActionView::LinkRenderer | |
def link(text, target, attributes = {}) | |
attributes['data-remote'] = true | |
super | |
end | |
end | |
end |
This is awesome ! After many unsuccesful hacks, i finally found this. why don't the author and mislav work together to make this part of the will_paginate gem. This definitely extends the gem for those of us who want pagination ajax calls
You can try also:
will_paginate @collection, {link_options: {'data-remote': true}, params: {action: 'other_action'}}
Thanks, that's even shorter and works great!
Works like charm 👍 Thanks a lot :)
The thing @tarmotalu posted won't work in vanilla will_paginate
. It will only work if a custom renderer is used, my guess is that people for whom this works use will_paginate-bootstrap
, which adds support for the link_options
parameter.
thanks
Good job. Thanks
It seems does not work in rails5. Some changes should be done:
params.permit(:controller, :page, :id, :page).merge(:renderer => RemoteLinkPaginationHelper::LinkRenderer).deep_symbolize_keys
I have read the willpaginate souce code:
renderer = case options[:renderer]
when nil
raise ArgumentError, ":renderer not specified"
when String
klass = if options[:renderer].respond_to? :constantize then options[:renderer].constantize
else Object.const_get(options[:renderer]) # poor man's constantize
end
klass.new
when Class then options[:renderer].new
else options[:renderer]
end
If you don't convert the key to symbol, it will raise an error ":renderer not specified"
One line in my js.erb file (e.g., index.js.erb) and will_paginate started working with ajax. Here it is:
$('.pagination a').attr('data-remote', 'true');
Thx steve981cr !!
Thanks to @tarmotalu
Thanks to @tarmotalu
Thanks a lot! It's really cool!!!
For those who want to use it with the bootstrap gem:
module RemoteLinkPaginationHelper
class LinkRenderer < WillPaginate::ActionView::BootstrapLinkRenderer
def link(text, target, attributes = {})
attributes['data-remote'] = true
super
end
end
end
One line in my js.erb file (e.g., index.js.erb) and will_paginate started working with ajax. Here it is: $('.pagination a').attr('data-remote', 'true');
Steve you're a rockstar!!
This works pretty well, but I can't get the page buttons to correspond to the correct page. How would I go about doing this?