使用 will_paginate 在 Ruby 中对数组进行分页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13187076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:58:42  来源:igfitidea点击:

Paginating an Array in Ruby with will_paginate

ruby-on-railsrubyruby-on-rails-3will-paginatekaminari

提问by Pi Horse

I have an array @level1 which looks like this :

我有一个数组@level1,它看起来像这样:

[[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])

But it throws the following error:

但它抛出以下错误:

undefined method `paginate' for [[3.0, 4, 2], [2.0, 48, 3], [2.1, 56, 4]]:Array

How can I perform pagination on this using will_paginate?

如何使用 will_paginate 对此执行分页?

采纳答案by Chris Heald

Sure, you can use WillPaginate::Collectionto construct arbitrary pagination across collections. Assuming an array called values:

当然,您可以使用WillPaginate::Collection跨集合构建任意分页。假设一个名为 的数组values

@values = WillPaginate::Collection.create(current_page, per_page, values.length) do |pager|
  pager.replace values
end

You can then treat @valuesjust like any other WillPaginate collection, including passing it to the will_paginateview helper.

然后,您可以@values像对待任何其他 WillPaginate 集合一样进行处理,包括将其传递给will_paginate视图助手。

回答by Philip Hallstrom

See https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/array.rb

https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/array.rb

Just require 'will_paginate/array'before you try it and it will work just fine. If it were me, I'd put that in say config/initalizers/will_paginate_extensions.rbso it's available right away and from everywhere.

require 'will_paginate/array'在你尝试之前,它会工作得很好。如果是我,我会说出来,config/initalizers/will_paginate_extensions.rb这样它就可以立即从任何地方获得。

回答by Francisco Quintero

Given a collectionarray in a Rails project

给定collection一个 Rails 项目中的数组

will_paginate ~>3.0.5

collection.paginate(page: params[:page], per_page: 10)

returns the pagination for the array values.

返回数组值的分页。

Do not forget require 'will_paginate/array'on top of the controller.

不要忘记require 'will_paginate/array'在控制器的顶部。

回答by Alberto Camargo

This is my example based in the answer of @chris-heald

这是我基于@chris-heald 的回答的示例

products = %w(i love ruby on rails)
@products = WillPaginate::Collection.create(params[:page], params[:per_page], products.length) do |pager|
   pager.replace products[pager.offset, pager.per_page]
end

回答by Jeremie Ges

I wasn't really happy with the answers provided in this thread.

我对这个线程中提供的答案并不满意。

I came up with this solution.

我想出了这个解决方案。

# app/lib/paged_array.rb

require 'will_paginate/array'

class PagedArray
  delegate_missing_to :@paged_collection

  def initialize(collection, page: 1, per_page: 20)
    @paged_collection = collection.paginate(
      page: page,
      per_page: per_page
    )
  end
end

And you can use it like that:

你可以这样使用它:

superheroes = %w[spiderman batman superman ironman]

PagedArray.new(superheroes, page: 1, per_page: 2)

I like this solution since it's explicit and you are not polluting your standard classes.

我喜欢这个解决方案,因为它是明确的,而且你不会污染你的标准类。

That being said, you are dependent of rails since I use "delegate_missing_to".

话虽如此,因为我使用“delegate_missing_to”,所以您依赖于 rails。