简体   繁体   中英

Multiple Dynamic Controls - Results as Array?

I'm building an editor that works with .CSV files. I have the application importing the file fine, but now I want the user to be able to select a few columns to work with.

I display the top 5 columns of the file in an HTML table, and in the table TH tag I'm creating some checkboxes at the top of the table like this:

It ends up looking like this:

表

All of this is wrapped up in a form and when it gets submitted the params contain the IDs of the checked checkboxes/columns.

"0"=>"0", "3"=>"3"

I want to find out which columns have been selected, but to my mind, scraping through the params and trying to work out which columns is a tad messy.... is there a way to get the selected checkboxes back as an array so I can just iterate through them? The number of columns is variable.

Solved! Changed the checkbox generation to this:

and all of the selected columns go into an array called selected_columns. Simple!

Changed the checkbox generation to this:

<% 0.upto(@column_index_max) do |column_index| %> 
<%= check_box_tag "selected_columns[]" , column_index %> 
<% end %>

and all of the selected columns go into an array called selected_columns. Simple!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM