| Author |
Dynamic checkbox
|
Atul Mishra
Ranch Hand
Joined: Jun 08, 2006
Posts: 140
|
|
All, In my app, in the JSP I have to populate the html table rows dynamically with values coming from database. Now, each table row should have a checkbox and then three other values. The display will be checkbox id name telephone How do I take the selected value of the checkbox to my action class in this case, since the checkboxes r being added dynamically ? How do I do it ? Thanks,please help
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56221
|
|
|
Make the checkbox value the primary key of the record it pertains to.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Atul Mishra
Ranch Hand
Joined: Jun 08, 2006
Posts: 140
|
|
Bear, Not sure whether the question I asked is clear. In jsp, values from database,getResults are being displayed as different rows. Now the user should be able to select one/multiple ids to proceed frther. JSP displays like this checkbox id name number checkbox id1 name1 number ;; ;;; ;;; ;;; The user when checks the checkbox, the value of the checked checkbox should be transferred. How do I transmit values of multiple checkbox ? and dynamically created textbox ? I use <logic:iterate> <input type="checkbox" name="select" value="<bean:write...../> </logic:iterate> Now here, how do I name dynamically created checkboxes ? If I can, how do I retrieve values of them ? Thanks
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56221
|
|
|
Moved to the JSP forum.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56221
|
|
Along with the display information for each "row", you'll also need to pass the id value (primary key or whatever other value uniquely identifies the entity). Then in the body of your row loop, set the value of each checkbox to that id value. You will end up with a bunch of checkboxes, each of which has the same name but a unique value. When submited, use the request.getParameterValues() method, passing the common name of the checkboxes, to obtain a String array of all checked values. [ June 12, 2006: Message edited by: Bear Bibeault ]
|
 |
Wang HC
Greenhorn
Joined: Jun 14, 2006
Posts: 1
|
|
Hi,You may be like this: jsp: <logic:iterate> <input type="checkbox" name="checkbox_<bean:write name="Your Item primary key"/>" value="1"/> </logic:iterate> action: for repeat { String checkbox = request.getParameter("checkbox_"+"Your Item primary key"); if("1".equals(checkbox)) it's checked. } [ June 14, 2006: Message edited by: Wang HC ]
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56221
|
|
|
No, each checkbox should have the same name, but a different value. Why embed the primary key as part of the name and have to parse it out later?
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56221
|
|
"Wang HC", There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it. In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious. Thanks! bear JavaRanch Sheriff
|
 |
 |
|
|
subject: Dynamic checkbox
|
|
|