[英]Rails 3 - “More” ajax pagination with Kaminari
你是說你需要一個“更多”按鈕? 如何創建這樣的幫助?
# 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
我已經准備好將這種輔助方法包含在寶石中,如果你發現它很有用,那么請讓我知道你的想法。 謝謝!
請記住, link_to_next_page(@items, :remote => true)
無法正常使用。 由於在Ajax請求之后無法確定當前頁面,因此在獲取新項目后需要替換鏈接。 使用不引人注目的javascript,這將是這樣的:
# 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) %>");
如果這沒有意義,請查看Railscasts上的Unobtrusive Javascript截屏視頻 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.