简体   繁体   中英

checkbox in list using rails will_paginate

I have the list that includes checkbox in each row. All the process are fine in one page. When the records exceeds one page and need to check the record on page 2 or 3, lost the checked value of the previous page. How can I solve this problem. Give me some great ideas.

When you paginate, your page only shows some records and only checkboxes for these records are shown. When you change page, you will show a new set of records, with their own checkboxes (and not the others). Each page is an independent request to the show (or other) method.

What you should do is save the list of checkboxes previously selected, in the controller, pass this list to the new page view, and add them as params in the link to a new page.

I have done it before, but I don't have the code at hand, but it's something like this:

Controller

def show
  @previous_checks = []
  params[:previous_checks].each do |c|
    @previous_checks << c
  end
  params[:checks].each do |c|
    @previous_checks << c
  end
  #rest of normal code...
end

In the view, add checks: @previous_checks in the link to a new page:

<%= will_paginate(xxxxxx, :params => { :previous_checks => @previous_checks }) %>

In the controller, when you process checks when you submit the form, just use both :checks and :previous_checks.

You probably need some changes in the code to handle the array of previous checks and the params, as some conversion maybe needed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM