Active Admin:在多列上排序 [英] Active Admin: sorting on multiple columns

查看:46
本文介绍了Active Admin:在多列上排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Active Admin似乎还不支持多列排序(即,将多个值传递给 config.sortable 选项)。
我在此处看到了一个旧的猴子补丁,但似乎无法使用我的版本(来自Github的1.0.0.pre)。

Active Admin doesn't seem to support multiple columns sorting yet (i.e. pass multiple values to the config.sortable option). I saw an old monkey patch here but it doesn't seem to work with my version (1.0.0.pre from Github).

有没有办法在最新的Active Admin版本上获取多个可排序的列?

Is there any way to get multiple sortable columns on the latest Active Admin version?

推荐答案

这也是一个猴子补丁:

在config / initializers或in中创建一个新文件lib文件夹: multiple_columns_sorting.rb

Create a new file in config/initializers or in the lib folder: multiple_columns_sorting.rb

module ActiveAdmin
  class ResourceController < BaseController
    module DataAccess
      def apply_sorting(chain)
        params[:order] ||= active_admin_config.sort_order

        orders = []
        params[:order].split('_and_').each do |fragment|
          order_clause = OrderClause.new fragment
          if order_clause.valid?
            orders << order_clause.to_sql(active_admin_config)
          end
        end

        if orders.empty?
          chain
        else
          chain.reorder(orders.shift).order(orders)
        end
      end
    end
  end
end

重新启动服务器。现在,您可以使用由 _ and _ 分隔的几列名称。例如:

Restart the server. Now you can use several columns name separeted by "_and_". For example:

config.sort_order = 'first_name_desc_and_last_name_asc'

这篇关于Active Admin:在多列上排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆