You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Kalyan Ayyagari <ka...@revenuetech.com> on 2005/05/10 16:47:00 UTC

Radio button for a 3rd level list

My form contains a list, say topLevelList. topLevelList in turn contains
lists, lets say level2List(s). This level2List in turn contains lists, say
level3List(s).

Level3List contains objects of type Component.

 

public class Component{

 

int id;

boolean amISelected;

 

}

 

My JSP needs to have a radio button for each component. For example, say
level3List has 3 components, red, green and blue. Then the JSP will have 3
radio buttons, one for red, one for green and one for blue. Now let's say
that we have only clicked the radio button for red. Now when we submit the
form I want the boolean amIselected for red to be true; and false for green
and blue.

 

How can I do it?

 

I tried to search for a similar post but couldn't find one. If there is a
similar post already and I've missed it then please point me to it.

 

Thanks,

Kalyan


Re: Radio button for a 3rd level list

Posted by Rick Reumann <st...@reumann.net>.
Kalyan Ayyagari wrote the following on 5/10/2005 12:16 PM:

> Here is what I'm trying to do:
> I get the top level list from the backend. The list contains the other lists
> that I already described before. Apart from that it contains the information
> based on which I have to generate the UI. That is, my UI might look like
> this:
> 
> -------------------------------------------
> 
> GROUP-1:
> 	ITEM-11
> 	  . component111
>         . component112
>         . component113
> 
>       ITEM-12
>         = component121
>         = component122
>         = component123
> 
> GROUP-2:
>    ...
>    ...
> 
> --------------------------------------------
> 
> The back-end might specify that I need to use radio buttons for components
> under ITEM-11 and use checkboxes for ITEM-12.
> 
> I can use the ids for value of radio buttons instead of trying to set a
> boolean as you said. But I'm not sure how I can do this.

But what is that you truly need when the user submits this form? Is it 
just the comonents they selected?

The cleanest solution would be if you could add an

Integer componentSelected property to the Item object.

Then on the form, it depends on how you are building it. I suggest for 
this using the nested tags... makes thigns clean (I use some JSTL here
using c:out but you can use bean or nested:write if you want).

<nested:form action="/wherever">
<nested:iterate id="group" property="groups">
     <c:out value="${group.name}"/><br/>
        <nested:iterate id="item" property="items">
        <c:out value="${item.name}"/><br/>
	  <nested:iterate id="comp" property="components">
  	     <nested:radio property="componentSelected/">
		<%--in above '/' is important-goes up a leve to item--%>
	     <c:out value="${comp.name}"/>
           </nested:iterate>
        </nested:iterate>
</nested:iterate>
<input type='submit'/>
</nested:form>

When the form submits you'll then be able to pull out all the "Item" 
objects and look at the "componentSelected" property in each Item which 
will give you the component ID of the one selected in the Item';s group.

I didn't test the above so there might be a typo. I think the above is 
your cleanest solution.

There are other ways you can accomplish what you want but what makes it 
a bit tricky is the "radio" button concept set up in groups. You can 
avoid using the Item object to hold the componentSelected property if 
you instead decide to make your own separate object to hold it. In that 
case you could have...

MyObjectForHoldingSelected
   Integer componentID;

In form Bean...

List listOfMyObjects //holds list of MyObjectForHoldingSelecte objects

Then this section below would be replaced..

replace..

<nested:iterate id="comp" property="components">
  	     <nested:radio property="componentSelected/">
		<%--in above '/' is important-goes up a leve to item--%>
	     <c:out value="${comp.name}"/>
           </nested:iterate>

with...

<nested:iterate id="comp" property="components" indexId="cIndex">
    <nested:radio property="componentSelected/">
       <html:checkbox property="listOfMyObjects[<c:out 
value='${cIndex}'/>].componentID"/>
       <c:out value="${comp.name}"/>
    </nested:iterate>

With the above though you'll have to use a LazyList or make you give the 
List and appropriate size to accomodate the number of items you'll need 
or you'll get indexOfOutOfBounds errors. THe first solution won't give 
you those problems.


-- 
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Radio button for a 3rd level list

Posted by Kalyan Ayyagari <ka...@revenuetech.com>.
Thanks Rick for the response.

Here is what I'm trying to do:
I get the top level list from the backend. The list contains the other lists
that I already described before. Apart from that it contains the information
based on which I have to generate the UI. That is, my UI might look like
this:

-------------------------------------------

GROUP-1:
	ITEM-11
	  . component111
        . component112
        . component113

      ITEM-12
        = component121
        = component122
        = component123

GROUP-2:
   ...
   ...

--------------------------------------------

The back-end might specify that I need to use radio buttons for components
under ITEM-11 and use checkboxes for ITEM-12.

I can use the ids for value of radio buttons instead of trying to set a
boolean as you said. But I'm not sure how I can do this.

Thanks,
Kalyan
-----Original Message-----
From: Rick Reumann [mailto:struttin@reumann.net] 
Sent: Tuesday, May 10, 2005 11:00 AM
To: Struts Users Mailing List
Subject: Re: Radio button for a 3rd level list

Kalyan Ayyagari wrote the following on 5/10/2005 10:47 AM:
> 
> public class Component{
> int id;
> 
> boolean amISelected; }
> 
> My JSP needs to have a radio button for each component. For example, say
> level3List has 3 components, red, green and blue. Then the JSP will have 3
> radio buttons, one for red, one for green and one for blue. Now let's say
> that we have only clicked the radio button for red. Now when we submit the
> form I want the boolean amIselected for red to be true; and false for
green
> and blue.

Well, this can be a little tricky or really simple. The first thing I 
need to ask is "What is your business requirement that you are actually 
trying to tackle?" The reason I ask this is, before I get into how you 
can do what you want, their could possibly be a much cleaner solution.

For example, if you want the user to "select" between three components, 
even if selecting between multiple sets of three components (red, green, 
blue). Why not provide radio buttons but for the value simply provide 
the unique componentID for the value ? Then when the form submits you 
have the 'id's of the components the user selected? I think this would 
be a much cleaner solution, however sometimes you do need to do 
something like you are describing, so there are ways.

The tricky part is that name of each radio button would be different 
based on the fact that you are wanting to modify a property of bean in 
the form (not a simple boolean property directly in the form). I think 
you would have to use javascript here. I'd probably go with a checkbox 
for each and although the names would be different for the checkbox, you 
could give them all a similar id based on your iteration. Then when one 
is checked you'd pass in the id of that checkbox and your javascript 
function would know how to turn off the other ones. Again, first step is 
figuring out what exactly you need to capture, because my guess is you 
don't have to go this route.


-- 
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Radio button for a 3rd level list

Posted by Rick Reumann <st...@reumann.net>.
Kalyan Ayyagari wrote the following on 5/10/2005 10:47 AM:
> 
> public class Component{
> int id;
> 
> boolean amISelected; }
> 
> My JSP needs to have a radio button for each component. For example, say
> level3List has 3 components, red, green and blue. Then the JSP will have 3
> radio buttons, one for red, one for green and one for blue. Now let's say
> that we have only clicked the radio button for red. Now when we submit the
> form I want the boolean amIselected for red to be true; and false for green
> and blue.

Well, this can be a little tricky or really simple. The first thing I 
need to ask is "What is your business requirement that you are actually 
trying to tackle?" The reason I ask this is, before I get into how you 
can do what you want, their could possibly be a much cleaner solution.

For example, if you want the user to "select" between three components, 
even if selecting between multiple sets of three components (red, green, 
blue). Why not provide radio buttons but for the value simply provide 
the unique componentID for the value ? Then when the form submits you 
have the 'id's of the components the user selected? I think this would 
be a much cleaner solution, however sometimes you do need to do 
something like you are describing, so there are ways.

The tricky part is that name of each radio button would be different 
based on the fact that you are wanting to modify a property of bean in 
the form (not a simple boolean property directly in the form). I think 
you would have to use javascript here. I'd probably go with a checkbox 
for each and although the names would be different for the checkbox, you 
could give them all a similar id based on your iteration. Then when one 
is checked you'd pass in the id of that checkbox and your javascript 
function would know how to turn off the other ones. Again, first step is 
figuring out what exactly you need to capture, because my guess is you 
don't have to go this route.


-- 
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org