简体   繁体   English

使用rails will_paginate列表中的复选框

[英]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. 当记录超过一页并需要检查第2或3页上的记录时,将丢失上一页的检查值。 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. 每个页面都是对show(或其他)方法的独立请求。

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: 在视图中,添加检查:@previous_checks在新页面的链接中:

<%= 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. 在控制器中,当您处理提交表单时的检查时,只需使用:checks和: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 您可能需要对代码进行一些更改以处理以前的检查和参数数组,因为可能需要进行一些转换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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