私は will_paginate を使用して、さまざまなページに大量のファイルをリストしています。さらに分析するファイルを選択するためのチェックボックスもあります。
コントローラ:
@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 つのページにあるファイルを選択できることです。そのため、[すべてチェック/チェック解除] をクリックすると、使用可能なすべてのファイルではなく、この特定のページのファイルがチェックされます。さまざまなページのさまざまなファイルをまとめてチェックできるようにしたいと考えています。
例: ページ 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" %>
</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%>
コントローラ:
what_to_do
a = params[:file_ids].split(',')
end
check_boxes をチェックしたにもかかわらず、Nil cless の分割関数がないため、params[:file_ids] が空であるというエラーが表示されます。