ruby-on-rails - Ruby on Rails,will_paginate,复选框

标签 ruby-on-rails checkbox will-paginate

我正在使用 will_paginate 在不同页面列出大量文件。我还有一个复选框,以便选择文件进行进一步分析。

Controller :

@files = FileDB.search(search_string).paginate(:per_page => 10, :page => params[:page]) 

查看:

  <button type="button" id="check_all">Check/Uncheck all</button>
 <%= button_tag :class => "btn btn-primary", :name => 'analyse' do %>Analyse<% end %>

    <% @filess.each do |file| %>
    <p><td> <%= check_box_tag "files[]", file.id %></td><%= file.name %></p>
    lala...

我的问题是我可以选择位于一页中的文件。因此,通过单击“选中/取消选中所有”,它不会检查所有可用文件,而是检查该特定页面的文件。我希望能够一起检查来自不同页面的不同文件。

示例:第 1 页有 10 个文件,第 2 页有 4 个文件。我想检查第 1 页的 5 个文件和第 2 页的所有文件,然后单击“分析”按钮,这 9 个文件必须一起分析,所以复选框应该记住来自不同页面的不同文件

提前致谢

根据juanpastas的解决方案编辑

查看:

 <script type='text/javascript'>    
 // see note below
sessionStorage.fileIds = ' ';

// this way:
// 1. you execute the function appending just one handler
// 2. you can change content of .checkboxes-container with AJAX to do pagination

$('.checkboxes-container').on('change', ':checkbox', function(){
  // search for "space id space", examples: " 1 " or " 10 "
  var id = new RegExp(' ' + this.value + ' ');

  // get that part of the string
  var match = (sessionStorage.fileIds.match(id) || [])[0];

  // toggle value, this is: replicate check box behaviour in this variable
  // ids will be saved as " 1 2 3 10 15 ", spaces in both sides to eliminate special cases.
  if(match) sessionStorage.fileIds = sessionStorage.fileIds.replace(id, ' ');

  else sessionStorage.fileIds = sessionStorage.fileIds + this.value + ' ';

})
</script>
 <script type='text/javascript'>   
$('[name=analyse]').click(function(){
  // " 1 2 3 10 " is sent as [1,2,3,10]
  var fileIds = sessionStorage.ids.split(',').filter(function(e){if(e[0]) return e;})

  // change url, check server is receiving an array.
  $.get('/files', { file_ids: fileIds });
})
</script>
                    <%= form_for Group.new, url: what_to_do_files_path ,method: :get ,:validate => true do |f| %>


                    <div class="field">
                    <%=f.text_field :group_name, placeholder: "Group Name" %>&nbsp;&nbsp;
                    </div>

                    <%= button_tag :class => "btn btn-primary", :name => 'analyse' do %>Analyse;<% end %>
                    <button type="button" id="check_all" class="btn"> Check/Uncheck all</button>

                        <% @files.each do |file| %>
                        <p><td> <%= check_box_tag "files[]", file.id , false %></td>file.name</p>




                        <%end%>

                  <%end%> 

Controller :

what_to_do
    a = params[:file_ids].split(',')
end

它告诉我一个错误,Nil cless 没有拆分函数,所以 params[:file_ids] 是空的,尽管我检查了复选框

最佳答案

您需要以某种方式保存选定的项目,例如在 session (客户端或服务器)或隐藏的输入中。

这是在浏览器 session 中保存:

// see note below
sessionStorage.fileIds = ' ';

// this way:
// 1. you execute the function appending just one handler
// 2. you can change content of .checkboxes-container with AJAX to do pagination

$('.checkboxes-container').on('change', ':checkbox', function(){
  // search for "space id space", examples: " 1 " or " 10 "
  var id = new RegExp(' ' + this.value + ' ');

  // get that part of the string
  var match = (sessionStorage.fileIds.match(id) || [])[0];

  // toggle value, this is: replicate check box behaviour in this variable
  // ids will be saved as " 1 2 3 10 15 ", spaces in both sides to eliminate special cases.
  if(match) sessionStorage.fileIds = sessionStorage.fileIds.replace(id, ' ');

  else sessionStorage.fileIds = sessionStorage.fileIds + this.value + ' ';

})

然后,当点击分析时,您可以发送所有这些 ID:

$('[name=analyse]').click(function(){
  // " 1 2 3 10 " is sent as [1,2,3,10]
  var fileIds = sessionStorage.ids.split(',').filter(function(e){if(e[0]) return e;})

  // change url, check server is receiving an array.
  $.get('/files', { file_ids: fileIds });
})

检查 this .

在 Controller 中:

before_filter :make_array_from_file_ids


def make_array_from_file_ids
  # this depends on the name you used in AJAX call
  params[:file_ids] = params[:file_ids].split ','
end

注意 我不确定您是否需要在 Controller 中使用它。也许不会。也许您需要一些不同的东西。

关于ruby-on-rails - Ruby on Rails,will_paginate,复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17745722/

相关文章:

ruby-on-rails - 从 Active Storage 中删除所有数据?

php - 如何根据checkbox值将数据存储到多个MySQL表中?

html - will_paginate 分页控件没有水平对齐

ruby-on-rails - 将分页 : Limit number of results

ruby-on-rails - 嵌套形式的输入未显示在 Rails 中

ruby-on-rails - 在 Rails 中查找自引用关系中的后代

javascript - 取消选中一组复选按钮

ruby-on-rails - 如何限制will_paginate链接中显示的页面数量

ruby-on-rails - Digest::SHA2.hexdigest() 在哪里定义?

jquery - bool 属性的语法是什么,例如一个选中的复选框,在 HTML 中?