• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts - Dynamically Populate checkbox

 
roseline ruba
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I used struts layout to populate my checkbox.

<layout:checkboxes key="flightallocation.location1" property="location" styleClass="LABEL" >
<layout: options collection="locationsList" property="data1" labelProperty="data2"/>
</layout:checkboxes>


'locationsList' having data from database.

It is populating correctly. And also when submitting the page I'm getting the values of selected locations in a array 'locationList'

when i want to update my records in database, I want to populate my records in the page. for example, If i have more than one location for the record, I fetch those locations, and put it in 'locationList' property in my form.

But i dont know how to populate these locations with selected checkboxes.
in my Page i'm having
'locationsList' having all the locations.it is a collection.
'locationList' having the location names to be selected. it is form bean property.it is ArrayList.

from these two collections, how can I populate my checkboxes with the selected locations?

[ October 03, 2007: Message edited by: roseline ruba ]
[ October 03, 2007: Message edited by: roseline ruba ]
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not use the layout tags. I guess that layout:checkboxes is like html:select (with multiple="true"?). What type is the location property of your form? If you want items to appear selected when the form is shown then you need to populate the location property. You mention locationList, but I do not see that referenced in your code.

- Brent
 
roseline ruba
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the type of my 'location' & 'locationList' property
struts-config.xml:
<form-property name="location" type="java.lang.String"/>
<form-property name="locationList" type="java.util.ArrayList"/>

My jsp :
<layout:checkboxes key="flightallocation.location1" property="location" styleClass="LABEL" >
<layout ptions collection="locationsList" property="data1" labelProperty="data2"/>
</layout:checkboxes>

html output for the above jsp:
<input type="checkbox" name="location[0]" value="EK01"> Carousel 01
<input type="checkbox" name="location[1]" value="EK02"> Carousel 02
<input type="checkbox" name="location[2]" value="EK03"> Carousel 03
<input type="checkbox" name="location[3]" value="EK04"> Carousel 04
<input type="checkbox" name="location[4]" value="EK05"> Carousel 05
<input type="checkbox" name="location[5]" value="PROB"> Mis-track line

i use he following function to set selected locations to locationList:

public void set(String name, int index, Object value) {
Object t_list = get(name+"List");
if (!(t_list instanceof ArrayList))
set(name+"List",new ArrayList());
ArrayList temp = (ArrayList) get(name+"List");
temp.add(value);
set(name+"List",temp);
log.info(name + " " + index + " = " + value);
}

It is working corretly.
When i fetch record from the database, if i have more than one location in a record, then i set those locations in 'locationList' property.
so i have 'locationList' property having the locations to be selected.
and also i have a collection 'locationsList' having all locations.

Help me to generate the checkboxes with the locations selected in 'locationList' property
 
roseline ruba
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anybody have solution?
 
Quang Pham
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
It seem to me that your problem is with the way you name your check boxes. In my opion, just make it simple. This is what I would do:
JSP:
<input type="checkbox" name="location1"> Carousel 01
<input type="checkbox" name="location2"> Carousel 02
Remove the value attributes.

Action Form class:
private boolean location1;
private boolean location2;
getter and setter for location1 and location2.
Don't forget this method:
public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest request){
location1 = false;
location2 = false;
}

Action class:
From data base set location1 to true if db field = "EK01" and location2 to true if db field = "EK02"

User submit the form:
If location1 is true, set database field to "EK01".
If location2 is true, set database field to "EK02"
update data base.

[ October 05, 2007: Message edited by: Quang Pham ]
[ October 05, 2007: Message edited by: Quang Pham ]
 
roseline ruba
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The locations is not constant.
It is coming from database.
And I'm using DynaValidatorActionForm.
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have the ArrayList locationList defined in your struts-config, but you are not using this in your jsp. I see you using location and locationsList, but not locationList. Look at your html. You have "location[0]" but location is a String. It seems like the property of your checkboxes tag should be locationList.

- Brent
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic