Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - "More" ajax pagination with Kaminari

I am trying to do a "Twitter like" pagination with a "More" button to load results with Kaminari.

I found this question here

But I can't figure out how to make it works and if it's a good approach.

Thanks in advance

like image 935
invaino Avatar asked Apr 20 '11 18:04

invaino


2 Answers

Do you mean you need a "more" button? How about creating a helper like this?

# usage: 
#   link_to_next_page(@items)
#   link_to_next_page(@items, :remote => true)  # Ajax
def link_to_next_page(scope, name, options = {}, &block)
  param_name = options.delete(:param_name) || Kaminari.config.param_name
  link_to_unless scope.last_page?, name, {param_name => (scope.current_page + 1)}, options.merge(:rel => 'next') do
    block.call if block
  end
end

I'm ready to include this kind of helper methods to the gem if you find it useful, so please let me know what you think. Thanks!

like image 110
Akira Matsuda Avatar answered Oct 05 '22 16:10

Akira Matsuda


Keep in mind that link_to_next_page(@items, :remote => true) won't work correctly out of the box. Since it has no way to determine the current page after an Ajax request, the link needs to be replaced after new items are fetched. Using unobtrusive javascript, this would something look like this:

# app/views/items/index.js.erb
$(".items").append("<%= escape_javascript(render(@items)) %>");
$(".more_link").replaceWith("<%= escape_javascript(
    link_to_next_page @items, 'View more',
                        :remote => true,
                        :id     => :view_more) %>");

If this doesn't make sense, take a look at the Unobtrusive Javascript screencast at Railscasts.

like image 21
dkobozev Avatar answered Oct 05 '22 17:10

dkobozev