lunaclaire
2009-Nov-01 23:26 UTC
how to pass selected checkboxes from form when a select element changes?
I have a list that is enclosed in a form. Each item in the list has a checkbox, so an excerted bit of the code is (the part to list the contacts is really in a partial): <% form_tag fxn_for_selected_contacts_path, :method => :put do %> ... <%= for contact in @contacts do %> <%= check_box_tag(''contact_ids[]'', contact.id, false, :class => ''selector'') %> <%= contact.name %> <% end %> ... <% end %> The form also has a select menu that is used to list a set of ''groups'' that the selected items can be added to. I''d like to add the selected items to the selected group either using an :onchange handler or observe_field when the user changes the menu selection. At the moment I''m calling a remote_function like so: :onchange => remote_function(:url => {:action => :add_to_group}, :with => "''value='' + value") this works well to pass the selected group in params[:value] My problem is that because I''m calling remote_function, I dont get the contact_ids[] passed back, so I dont know what the selections are in the add_to_group action. Any thoughts on how to determine which checkboxes are selected and pass them to this remote function?
lunaclaire
2009-Nov-02 02:47 UTC
Re: how to pass selected checkboxes from form when a select element changes?
OK! I figured it and and share below to help anyone else who comes across this problem... I created a JS fxn to translate the selected checkboxes into query string parameters that Rails will read as params. function getSelections(collectionName, className) { paramsString = "" boxes = document.getElementsByClassName(className); for (i = 0; i < boxes.length; i++) if (boxes[i].checked) { paramsString += "&" + collectionName + "[]=" + boxes[i].value; } return paramsString ; } note that you pass in the name of the array of checkboxes as you''d like to pick it up in the params hash in your controller. Also, you pass the class name that you gave to the checkboxes in your view so that the fxn can pick them up from the DOM. So, for my example, this is how I use all this in my view for the select menu: <%= select_tag :selected_group, options_from_collection_for_select(@groups, ''id'', ''name''), :onchange => remote_function(:url => {:action => :add_to_group}, :with => "''value='' + value + getSelections (''contact_ids'', ''selector'')") %> so, the controller picks up the select menu''s choice in params[:value] and the selected objects from the list in params[:contact_ids]. Hope that helps someone. On Nov 1, 3:26 pm, lunaclaire <szager...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a list that is enclosed in a form. Each item in the list has a > checkbox, so an excerted bit of the code is (the part to list the > contacts is really in a partial): > > <% form_tag fxn_for_selected_contacts_path, :method => :put do %> > > ... > > <%= for contact in @contacts do %> > <%= check_box_tag(''contact_ids[]'', contact.id, false, :class => > ''selector'') %> > <%= contact.name %> > <% end %> > > ... > > <% end %> > > The form also has a select menu that is used to list a set of ''groups'' > that the selected items can be added to. I''d like to add the selected > items to the selected group either using an :onchange handler or > observe_field when the user changes the menu selection. > > At the moment I''m calling a remote_function like so: > > :onchange => remote_function(:url => {:action > => :add_to_group}, :with => "''value='' + value") > > this works well to pass the selected group in params[:value] > > My problem is that because I''m calling remote_function, I dont get the > contact_ids[] passed back, so I dont know what the selections are in > the add_to_group action. > > Any thoughts on how to determine which checkboxes are selected and > pass them to this remote function?