简体   繁体   English

在对数组进行排序后如何对数组元素进行分页?

[英]How to paginate elements of an array after sorting on it?

I've got a controller method that sorts elements by a derived attribute. 我有一个控制器方法,该方法按派生属性对元素进行排序。 I can print to the view as long as I don't attempt to paginate. 只要不尝试分页,就可以打印到视图。 When I call @foos = @foos.page params[:page] I get the following error: undefined method 'page' for #<Array:...> 当我调用@foos = @foos.page params[:page] ,出现以下错误: undefined method 'page' for #<Array:...>

Can anyone provide some guidance here? 有人可以在这里提供一些指导吗? Thanks. 谢谢。

Here's the whole controller method: 这是整个控制器方法:

def index_by_capacity
    if current_user.is_view_major?
      @foos = Foo.major_attr(:name)
    else
      @foos = Foo.order(:name)
    end


  @foos = @foos.sort_by! {|a| a.capacity_available.to_i }
  @total_foos = @foos.count

  @foos = @foos.page params[:page]

  respond_to do |format|
    format.html
    format.json { render json: @foos }
  end
end
  • The .sort_by! .sort_by! method returns an Array , which has no method page (a ruby Array can't be paginated, you would have to implement it yourself or use an external lib). method 返回一个Array ,它没有方法page (ruby Array无法分页,您必须自己实现或使用外部库)。

  • The pagination works on ActiveRecord::Relation objects , returned by queries like .where or .order 分页适用于ActiveRecord :: Relation对象 ,由.where.order类的查询返回

  • In you case , you should do an order at the DB-level (faster than doing a sort via Ruby): 在这种情况下 ,您应该在数据库级别下订单(比通过Ruby进行排序更快):

     @foos.order(:capacity_available) 
  • If the capacity_available attribute is a String (not an Integer), you can CAST it as an INT : 如果capacity_available属性是字符串(不是整数),则可以将其作为INT进行转换

     @foos.order('CAST(capacity_available AS INT) ASC') 

(You may need to edit the CAST() function, it should work on PostGreSQL, not sure about MySQL) (您可能需要编辑CAST()函数,它应可在PostGreSQL上运行,不确定MySQL)

If you want to continue using an array instead of an active record relation object, you can simply include WillPaginate::Collection . 如果要继续使用数组而不是活动记录关系对象,则可以简单地包含WillPaginate::Collection

From WillPaginate Documentation 从WillPaginate文档

Previously, all objects returned from paginate methods were of WillPaginate::Collection type. 以前,从分页方法返回的所有对象均为WillPaginate :: Collection类型。 This is no longer true; 这不再是事实; in Active Record 3, the paginate and page methods return a Relation that is extended to look like a WillPaginate::Collection. 在Active Record 3中,分页和分页方法返回一个Relation,该关系被扩展为类似于WillPaginate :: Collection。 Similarly, DataMapper returns a regular DataMapper::Collection that is also extended. 类似地,DataMapper返回一个常规的DataMapper :: Collection,它也得到了扩展。

Both ActiveRecord::Relation and DataMapper::Collection are lazy arrays in the way that they don't execute any SQL queries until the point when data is needed. ActiveRecord :: Relation和DataMapper :: Collection都是惰性数组,它们直到需要数据时才执行任何SQL查询。 This makes them better than ordinary arrays. 这使它们比普通数组更好。

The WillPaginate::Collection class is still available, however, and mostly unchanged. 但是,WillPaginate :: Collection类仍然可用,并且几乎没有变化。

The Array#paginate method still exists, too, but is not loaded by default. Array#paginate方法也仍然存在,但是默认情况下不加载。 If you need to paginate static arrays, first require it in your code: 如果需要对静态数组进行分页,请首先在代码中要求它:

require 'will_paginate/array' 需要'will_paginate / array'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM