简体   繁体   English

如何在没有分页的情况下在json / csv中导出数据

[英]How to export data in json/csv without pagination

I'm using active_admin for one of my projects, and I have some tables with a lot of data (+100,000 records). 我正在为我的一个项目使用active_admin,并且我有一些包含大量数据的表(+100,000条记录)。

Its fine to paginete to browse the data, but when I want to export the data (so I can do custom match in excel), and I try to export in xml, it just export the current records of the page, instead of exporting the total records on the filtering. 浏览数据很好,但是当我想导出数据时(所以我可以在excel中进行自定义匹配),我尝试在xml中导出,它只导出页面的当前记录,而不是导出过滤的总记录。

module ActiveAdmin
  class ResourceController
    module Collection
      module Pagination
        def max_csv_records
          1_000_000
        end
      end
    end
  end
end

but that won't work for xml/json, any ideas how to solve that? 但这对xml / json不起作用,任何想法如何解决?

Thx 谢谢

I ended up doing another monkey patch, there may be a better way but this one works 我最后做了另一个猴子补丁,可能有更好的方法,但这个有效

module ActiveAdmin
  class ResourceController
    module Collection
      module Pagination
        def per_page
          return 1_000_000 if %w(text/csv application/xml application/json).include?(request.format)

          return max_per_page if active_admin_config.paginate == false

          @per_page || active_admin_config.per_page
        end
      end
    end
  end
endend

This might help for some versions of active admin: 这可能对某些版本的活动管理员有所帮助:

module ActiveAdmin::ResourceController::DataAccess
  protected

  def apply_pagination_with_csv(chain)
    return chain if request.format == 'text/csv'
    apply_pagination_without_csv(chain)
  end

  alias_method_chain :apply_pagination, :csv
end

To figure out if the patch works you should pick into the installed gem this file 要确定补丁是否有效,您应该选择已安装的gem 这个文件

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

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