Ruby-on-rails rails 3,Kaminari 对简单数组的分页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6545990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:30:34  来源:igfitidea点击:

rails 3,Kaminari pagination for an simple Array

ruby-on-railskaminari

提问by Swapnil Chincholkar

For paginating a common array I got this solution,

为了对公共数组进行分页,我得到了这个解决方案,

@arr_name = Kaminari.paginate_array(@arr_name).page(params[:page]).per(PER_PAGE_RECORDS)

@arr_name = Kaminari.paginate_array(@arr_name).page(params[:page]).per(PER_PAGE_RECORDS)

PER_PAGE_RECORDSis a variable with value as per needed for pagination.

PER_PAGE_RECORDS是一个具有分页需要的值的变量。

Any better Ideas??

任何更好的想法?

Also to have an ajax call for using pagination one can use this,

也有一个使用分页的ajax调用,可以使用它,

In your view,

在你看来,

give id to your div tab

为您的 div 选项卡提供 id

div id="paginate"

and inside it

在里面

<%= paginate @arr_name, :remote => true %>

<%= 分页 @arr_name, :remote => true %>

And in js response file put,

并在js响应文件中放置,

$('#paginate').html('<%= escape_javascript(paginate(@arr_name, :remote => true).to_s) %>');

$('#paginate').html('<%= escape_javascript(paginate(@arr_name, :remote => true).to_s) %>');

So your requests will be AJAX.

所以你的请求将是 AJAX。

Thanks.

谢谢。

回答by Sethupathi

This is the only available helper method to paginate an array object using Kaminari. Another alternative is, as suggested solution in kaminari wiki page, add the instance methods to the array object.

这是使用 Kaminari 对数组对象进行分页的唯一可用辅助方法。另一种选择是,作为kaminari wiki page 中建议的解决方案,将实例方法添加到数组对象。

If you are trying a common solution based on the ActiveModel return type ( .all returns array and .where returns ARL) then following is an workaround.

如果您正在尝试基于 ActiveModel 返回类型( .all 返回数组和 .where 返回 ARL)的通用解决方案,那么以下是一种解决方法。

unless @arr_name.kind_of?(Array)
  @arr_name = @arr_name.page(params[:page]).per(PER_PAGE_RECORDS)
else
  @arr_name = Kaminari.paginate_array(@arr_name).page(params[:page]).per(PER_PAGE_RECORDS)
end