护栏和kaminari [英] rails and kaminari

查看:74
本文介绍了护栏和kaminari的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个系统,该系统可以在单个请求中生成数以千计的报告以填充表格,并且我使用kamanari来分页每页20条记录.由于单个请求中可能有成千上万的报告正在加载,因此有时无论请求格式是html还是json,它们都需要花费几分钟的时间来加载.我用kaminari尝试了很多东西.

I have a system that produces literally thousands of reports in a single request to populate a table and I use kamanari to paginate 20 records per page. Because there could be thousands of reports being loaded in a single request, it sometimes takes several minutes for them to load, regardless of the request format is html or json. I tried a number of things with kaminari.

my_controller.rb
def my_table
  reports = unit.reports_for(history_date_range)  #This could return possibly 10,000 reports
  paginated_reports = reports.order('time desc').page(params[:page]).per(20)
  @reports = paginated_reports
  ...
end

my_table.html.haml
- @reports.each do |r|
  ...
= paginate @reports.page(params[:page]).per(20)

上述问题是kaminari不会仅将20条记录发送回视图.例如,它发送所有10,000.并以20个为一组对它们进行分页.即使单击第二页,它也会再次遍历整个分页路线,并查询10,000条记录并在页面上显示20条记录.

The problem with the above is that kaminari doesn't send only 20 records back to the view. It sends all 10,000, for example. And just paginates them in groups of 20. Even when I click the second page, it goes through the whole pagination route again, and queries 10,000 records and shows 20 records on the page.

而不是:

= paginate @reports.page(params[:page]).per(20)

我也尝试过:

= paginate @reports, :total_pages => 2

这仍然需要很长时间,但只显示2个页面,而不是全部页面.

And this still takes long but only shows 2 total pages, not all the pages that there are.

我要尝试的操作如下.假设总共有100条记录,这意味着每20条记录中有50页.我只想查询前20条记录,将其发送回视图,并用这20条记录填充视图,并带有一个小箭头指示还有更多.然后,当用户单击该箭头符号时,它仅查询下一组20条记录,将它们发送回查看,依此类推.我不想一次查询这么多记录,而用它们填充整个视图.

What I am trying to do is as follows. Let's say there are 100 records total and that means there are 50 pages in groups of 20. I only want to query the first 20, send it back to the view, and populate the view with those 20 records and have a little arrow sign indicating that there are more. Then when the user clicks that arrow sign, it then queries only the next set of 20 records, sends them back to view, and so forth. I don't want to have to be querying so many records at a single time and have the entire view populate with them.

推荐答案

下面的行正在加载所有数据:

Below line is loading all data:

 reports = unit.reports_for(history_date_range)

在加载数据后,您正在使用分页,这与在performanceresponsiveness上不使用分页相似.因此,您需要在此查询上使用分页.如果您可以提供有关查询内容的更多详细信息,则将有助于准确回答.

After loading data you are using pagination which is similar to not using pagination in terms of performance and responsiveness. So you need to use pagination on this query. If you can give more details of what you are querying it will help to exact answer.

这篇关于护栏和kaminari的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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