【发布时间】:2015-04-25 06:34:11
【问题描述】:
我正在使用Kaminari 在我的 Rails 应用程序中进行分页。我需要为当前页面或每一页上的项目全选。当前页面是相当简单的捆绑如何选择其他页面上的所有项目。
file_items_controller.rb
def index
account = Account.includes(:credentials).find params[:account_id]
@page = page_param.to_i
@tab = 'Files'
@sort_by = sort_by
@credential = if params[:credential_id].blank?
account.credentials.first || nil
else
account.credentials.find(params[:credential_id])
end
return unless @credential
file_items = FileItem.file_item_list(@credential.root_folder, sort_by)
@total_count = file_items.count
@max_per_page = file_items.count <= 15 ? 'all' : max_per_page.to_i
file_items_scope = Kaminari.paginate_array(file_items.select(&:file?), total_count: file_items.count).page(page_param)
@file_items = if max_per_page == 'all'
file_items_scope.per(file_items.count) || []
else
file_items_scope.per(max_per_page) || []
end
end
file_items.js
$('a.select-all-current').click(function(e){
e.preventDefault();
$('input:not(:checked)').each(function(_, element){
$(element).click();
});
});
$('a.select-all-files').click(function(e){
e.preventDefault();
$('a.select-all-current').click();
});
index.html.slim
...
.row
ul.file-list
- @file_items.each do |file_item|
li.row.file-item
.col-lg-9
p.label
= "#{file_item[:path]}/#{file_item[:name]}"
p.file-size
= "#{number_to_human_size(file_item[:size]).downcase} | "
.col-lg-1.pull-right
= check_box_tag "file_id_#{file_item[:id]}", file_item[:id], false, class: 'file-box'
...
【问题讨论】:
-
我认为这是不可能的,因为您通过对它们进行分页来限制要查询的记录数。就像您在做
Foo.all.limit(5).. 考虑到这一点,您可能必须创建一个方法,如果您的选择参数被选中/为真,它将获取数据库中的所有项目或类似的东西。if param[:all] { Foo.all } -
从代码看来,除了常规分页之外,您还试图支持“查看全部”选项。我是否正确理解了您的问题?
-
@xathra 是正确的
-
我认为这也会有所帮助stackoverflow.com/a/11818392/1770571
标签: javascript ruby-on-rails pagination kaminari