索引中的Activeadmin自定义可排序列

时间:2017-10-26 12:40:50

标签: ruby-on-rails ruby activeadmin

我有自定义的可排序列'状态'在索引中,我怎样才能使它可以排序?

 index do
    selectable_column

    column :title
    column :counter
    column 'Status', do |purchase|
      if purchase.status == 'verified' && purchase.expiration_date < DateTime.now.utc
        'ended'
      elsif purchase.status == 'verified' && purchase.expiration_date >= DateTime.now.utc
        'active'
      else
        purchase.status
      end
    end
  end

3 个答案:

答案 0 :(得分:0)

根据the docs,您应该能够将:sortable密钥传递给column来电,如下所示:

  index do
    # SNIP
    column 'Status', sortable: :status do |purchase|
      # SNIP
    end
  end

答案 1 :(得分:0)

索引表自定义排序的文档位于index as table page

答案 2 :(得分:0)

以这种方式解决问题

        column 'Status', sortable: :status do |purchase|
          if purchase.status == 'verified' && purchase.expiration_date < DateTime.now.utc
            'ended'
          elsif purchase.status == 'verified' && purchase.expiration_date >= DateTime.now.utc
            span class: "purchase-status-active" do
              'active'
            end
          else
            purchase.status
          end
        end


      controller do
        def find_collection(options = {})
          if params[:order] == 'status_desc'
            super.reorder(status: :desc, expiration_date: :desc)
          elsif params[:order] == 'status_asc'
            super.reorder(status: :asc, expiration_date: :asc)
          else
            super
          end
        end
      end