3

I am using Kaminari for paging in my rails application. I need to select all for items on the current page or every page. The current page is rather simple bundle how can I select all the items on other pages.

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'
...
Antarr Byrd
  • 24,863
  • 33
  • 100
  • 188
  • I don't think that's possible because you are limiting how many records to query by paginating them. It's like you're doing `Foo.all.limit(5)` .. With this in mind, you'd probably have to create a method where if your select param is checked/true, it will get all items in the database or something like that. `if param[:all] { Foo.all }` – Vlad Apr 24 '15 at 19:44
  • From the code it looks like you're trying to support a "View All" option in addition to regular pagination. Am I correctly understanding your question? – Andrew Kothmann Apr 24 '15 at 21:29
  • @xathra that is correct – Antarr Byrd Apr 24 '15 at 22:18
  • I think this also will be helpful https://stackoverflow.com/a/11818392/1770571 – Salma Gomaa Mar 13 '18 at 08:40

1 Answers1

1

This example assumes ActiveRecord but you should be able to adapt it to your situation:

class ItemsController < ApplicationController
  def index
    @items = if params[:per_page] == 'all'
      Item.all
    else
      Item.page( params[:page] ).per( params[:per_page] )
    end
  end
end
Andrew Kothmann
  • 607
  • 3
  • 4