Topic: Need to make dynamic checkboxes.

I am working on a project for my company, I work for a bank.  Basically, the users will use the app to search for checks in the mysql database.  It will create a list of their results.  Each result will have a check box.  At the bottom of the list will be a "select all" and "deselect all" button.  There will also be a print button.  Basically, they click the check-boxes of whatever items they want to print (or they hit select all/deselect all) and then click print, and it will dynamically build a pdf file (I'm using the pdf::writer plugin) with images of the checks in a certain layout.

I have implemented the backend for the pdf generation, and that is working.  I have also implemented the select all, deselect all button with the following code:

http://www.shiningstar.net/articles/art … .asp?ID=AW

As, you can see, that form sets every checkbox to the same name, "list", then the javascript loops through the checkboxes with:

function checkAll(field)
{
for (i = 0; i < field.length; i++)
    field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
    field[i].checked = false ;
}


So apparently javascript has the ability to loop through form fields that have same name with the field.length and field[i] references.  However, in my code, when I submit the form, the ruby action is unable to see them as different fields.  I am setting the value of each checkbox to the corresponding CheckId value (CheckId is a unique id in the table that I am pulling the data from), but they are all given the same name, "list".. so when I submit the form and then put this in the view:

<%params[:list].each do |item|%>
    <%=item.to_s%>
<%end%>

It only shows the value of the first check_box.. apparently ruby has no way of parsing form fields with the same name like javascript does.

Does anyone know how to implement something like this, with dynamic form lengths, dynamic field names, and select and deselect all buttons?

Thanks in advance for any suggestions.

John

Last edited by jchahn (2006-11-17 16:11:10)

Re: Need to make dynamic checkboxes.

If you append "[]" to the check box name, Rails will interpret them separately so you can loop through them in the resulting action:

<%= check_box_tag "list[]", some_id %>

However, I don't know if this will cause problems with the JavaScript though.

Railscasts - Free Ruby on Rails Screencasts