checkbox - Ruby on Rails, will_paginate, check box -


i using will_paginate list huge ammont of files in different pages. have check_box in order choose files futher analysis.

controller:

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

view:

  <button type="button" id="check_all">check/uncheck all</button>  <%= button_tag :class => "btn btn-primary", :name => 'analyse' %>analyse<% end %>      <% @filess.each |file| %>     <p><td> <%= check_box_tag "files[]", file.id %></td><%= file.name %></p>     lala... 

my problem can choose files situated in 1 page.so clicking on "check/uncheck all" checks not available files, files of specific page. able check different files different pages together.

example: 10 files on page 1, 4 files on page 2. want check 5 files page 1 , files page 2 , clicking on buttom "analyse", 9 files have analyzed together, checkbox should remember different files different pages

thanks in advance

edit according solution of juanpastas

view:

 <script type='text/javascript'>      // see note below sessionstorage.fileids = ' ';  // way: // 1. execute function appending 1 handler // 2. can change content of .checkboxes-container ajax pagination  $('.checkboxes-container').on('change', ':checkbox', function(){   // search "space id space", examples: " 1 " or " 10 "   var id = new regexp(' ' + this.value + ' ');    // part of string   var match = (sessionstorage.fileids.match(id) || [])[0];    // toggle value, is: replicate check box behaviour in variable   // ids saved " 1 2 3 10 15 ", spaces in both sides 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 " sent [1,2,3,10]   var fileids = sessionstorage.ids.split(',').filter(function(e){if(e[0]) return e;})    // change url, check server receiving array.   $.get('/files', { file_ids: fileids }); }) </script>                     <%= form_for group.new, url: what_to_do_files_path ,method: :get ,:validate => true |f| %>                       <div class="field">                     <%=f.text_field :group_name, placeholder: "group name" %>&nbsp;&nbsp;                     </div>                      <%= button_tag :class => "btn btn-primary", :name => 'analyse' %>analyse;<% end %>                     <button type="button" id="check_all" class="btn"> check/uncheck all</button>                          <% @files.each |file| %>                         <p><td> <%= check_box_tag "files[]", file.id , false %></td>file.name</p>                             <%end%>                    <%end%>  

controller:

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

it shows me error, there not split-function nil cless, params[:file_ids] empty, though checked check_boxes

you need save selected items somehow, example in session(client or server) or hidden input.

this saving in browser session:

// see note below sessionstorage.fileids = ' ';  // way: // 1. execute function appending 1 handler // 2. can change content of .checkboxes-container ajax pagination  $('.checkboxes-container').on('change', ':checkbox', function(){   // search "space id space", examples: " 1 " or " 10 "   var id = new regexp(' ' + this.value + ' ');    // part of string   var match = (sessionstorage.fileids.match(id) || [])[0];    // toggle value, is: replicate check box behaviour in variable   // ids saved " 1 2 3 10 15 ", spaces in both sides eliminate special cases.   if(match) sessionstorage.fileids = sessionstorage.fileids.replace(id, ' ');    else sessionstorage.fileids = sessionstorage.fileids + this.value + ' ';  }) 

then, when analyse clicked can send of ids:

$('[name=analyse]').click(function(){   // " 1 2 3 10 " sent [1,2,3,10]   var fileids = sessionstorage.ids.split(',').filter(function(e){if(e[0]) return e;})    // change url, check server receiving array.   $.get('/files', { file_ids: fileids }); }) 

check this.

in controller:

before_filter :make_array_from_file_ids   def make_array_from_file_ids   # depends on name used in ajax call   params[:file_ids] = params[:file_ids].split ',' end 

note not sure if need in controller. maybe not. maybe need different.


Comments