3

私は 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" %>&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%> 

コントローラ:

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

check_boxes をチェックしたにもかかわらず、Nil cless の分割関数がないため、params[:file_ids] が空であるというエラーが表示されます。

4

3 に答える 3

1

セッション(クライアントまたはサーバー)または非表示の入力など、選択したアイテムを何らかの方法で保存する必要があります。

これはブラウザセッションに保存されています:

// 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 });
})

これを確認してください。

コントローラーで:

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

コントローラーでこれが必要かどうかはわかりませんそうでないかもしれない。多分あなたは何か違うものが必要です。

于 2013-07-19T12:54:43.520 に答える
0

そのためには、レール 3 で「turbolink gem」を選択できます。これは、ページ 1 でファイルを選択し、ページ 2 で他のファイルを選択すると、すべてがグループ化され、その後分析できるためです。

gem 'turbolinks'

その後、コントローラーを少し変更する必要があります。訪問: - http://railscasts.com/episodes/390-turbolinks

于 2013-07-19T12:18:07.793 に答える