File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Dynamic checkbox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Dynamic checkbox" Watch "Dynamic checkbox" New topic
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: 60046
    
  65

Make the checkbox value the primary key of the record it pertains to.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by 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: 60046
    
  65

Moved to the JSP forum.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

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: 60046
    
  65

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: 60046
    
  65

"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
 
jQuery in Action, 2nd edition
 
subject: Dynamic checkbox
 
Similar Threads
Checkbox usage
access rows from html table
How to Get the check box in html:checkbox automatically get checked on display .
Casting question
retrieve a row in a table