我正在使用 Rails 3 插件 Will_Paginate,并一直在使用以下教程来学习如何自定义分页链接:
我的问题是,如何使分页链接更像GMAIL,例如:
1 - 100 of 25409 较旧的 › 最旧的 »
<较新 101 - 200/25409 较旧 › 最旧 »
«最新 ← 较新 25401 - 25409 共 25409
谢谢
最佳答案
看起来该链接包含(几乎)您需要的所有信息。
“旧版”基本上是“下一页”,因此您可以在渲染器中重写 next_page
方法。
“最旧的”是“最后一页”;您需要添加一个方法,然后确保它包含在 pagination
方法返回的数组中(will_paginate 中内置的 total_pages
方法将在这里提供帮助)。
然后对较新/最新执行相反的操作。
看看link_renderer.rb和 link_renderer_base.rb文件。它们具有您将重写的方法。
我编写了一个自定义 will_paginate 3 渲染器来模拟 GitHub/Twitter 风格的“更多”分页。我已经注释了下面的代码。它不会让您准确到达您需要去的地方,但它是一个开始。
module TwitterPagination
class LinkRenderer < WillPaginate::ViewHelpers::LinkRenderer
protected
# Tells WP how to render the "Next Page" link
def next_page
# The only difference from the default here is we renamed the link to "More"
# and added a custom class, twitter_pagination
previous_or_next_page(@collection.next_page, "More", 'twitter_pagination') if @collection.next_page
end
# Remove all links except our :next_page
def pagination
[ :next_page ]
end
end
end
为了做到这一点,我需要知道的一切都可以通过阅读上面链接的两个源文件来弄清楚。
令我惊讶的是,这竟然如此简单; will_paginate 的最新设计在这方面非常出色。
关于ruby-on-rails - Rails 3- Will_Paginate - 使分页链接更像 GMAIL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3896945/