使用 will_paginate 在 Ruby 中对数组进行分页 [英] Paginating an Array in Ruby with will_paginate
问题描述
我有一个数组@level1,它看起来像这样:
I have an array @level1 which looks like this :
[[3.0, 4, 2], [2.0, 48, 3], [2.1, 56, 4], ............]
我想在这个数组上应用分页,这样每个页面一次只显示几行.我试过这个:
I want to apply pagination on this array such each page displays only a few rows at once. I tried this:
@temp1 = @level1.paginate(:page => params[:page])
但它抛出以下错误:
undefined method `paginate' for [[3.0, 4, 2], [2.0, 48, 3], [2.1, 56, 4]]:Array
如何使用 will_paginate 对此进行分页?
How can I perform pagination on this using will_paginate?
推荐答案
当然,您可以使用 WillPaginate::Collection
来构建跨集合的任意分页.假设一个名为 values
的数组:
Sure, you can use WillPaginate::Collection
to construct arbitrary pagination across collections. Assuming an array called values
:
@values = WillPaginate::Collection.create(current_page, per_page, values.length) do |pager|
pager.replace values
end
然后您可以像对待任何其他 WillPaginate 集合一样对待 @values
,包括将其传递给 will_paginate
视图助手.
You can then treat @values
just like any other WillPaginate collection, including passing it to the will_paginate
view helper.
这篇关于使用 will_paginate 在 Ruby 中对数组进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!