Yusuf
Yusuf

Reputation: 35

Rails - How to add pagination to html table which is populated from csv file

am populating the html table from a csv file, i would like to know how to add pagination to this table.

Controller

def index
  @csv_table = CSV.open("file.csv", :headers => true).read
end

View

%table.table.table-bordered
%thead
  %tr
    - @csv_table.headers.each do |header|
      %th= header
%tbody
  - @csv_table.each do |row|
    %tr
      - row.each do |element|
        %td= element[1]

Basically am getting the table, but it is very long, i would like to add pagination to it. can someone help me on this, am new to rails.

Upvotes: 1

Views: 1332

Answers (1)

Michael Arkhipov
Michael Arkhipov

Reputation: 765

Yusuf. I think you have to use something like Kaminari or any pagination gem. For example, your controller with Kaminari should looks like:

def index
  csv_file = CSV.open('file.csv', headers: true).read

  @csv_headers = csv_file.headers
  @csv_data = Kaminari.paginate_array(csv_file).page(params[:page]).per(10)
end

Upvotes: 1

Related Questions