Dynamically generating multiple sets of radio buttons

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Dynamically generating multiple sets of radio buttons

Dave Weis
Hello

I am trying to make multiple sets of radio buttons with independent lists
of valid options. The options are all generated dynamically.

I'm having trouble figuring out how to declare the item list provider in my
java code. I have it working fine for a single item. With the sample below
I get the same list for both my radio buttons and my dropdown but I need to
be able to modify the list returned based on which line of input I'm on.

Thank you
dave





<s:iterator value="product.variations">

<table>
<!--  variation stuff -->

<tr>
<td colspan="2">
we have a variation <s:property value="name" />
</td>
</tr>

<s:if test="display == 'radio'">

<s:radio key="name"   list="itemList"   />

</s:if>

<s:if test="display == 'dropdown'">

<s:select list="itemList" key="name"></s:select>

</s:if>

</table>

</s:iterator>



public ArrayList<String> getItemList() {

logger.debug("get item list");
ArrayList<String> retval = new ArrayList<String>();
retval.add("the first option");
retval.add("the second option");
return retval;
}
Reply | Threaded
Open this post in threaded view
|

Re: Dynamically generating multiple sets of radio buttons

Dave Newton-6
It’s not entirely clear to me what you’re asking.

`getItemList` will return whatever it returns—if the logic for what should
return lies in the view layer then it’s an architectural problem.

If you need to return two lists then you should have two list getters. If
the view layer *presentation* of the list varies by list content then the
list should be presented in a structure that represents that, e.g.,

    Class ViewList {
        String representation; // e.g., “radio”, “dropdown”, etc.
        List items; // The things to show
    }

If it depends on actions *taken* in the view layer, e.g., something
JavaScript-y, then all this is out the window and we’d need more
information.

Dave

On Sat, Feb 24, 2018 at 5:00 PM Dave Weis <[hidden email]> wrote:

> Hello
>
> I am trying to make multiple sets of radio buttons with independent lists
> of valid options. The options are all generated dynamically.
>
> I'm having trouble figuring out how to declare the item list provider in my
> java code. I have it working fine for a single item. With the sample below
> I get the same list for both my radio buttons and my dropdown but I need to
> be able to modify the list returned based on which line of input I'm on.
>
> Thank you
> dave
>
>
>
>
>
> <s:iterator value="product.variations">
>
> <table>
> <!--  variation stuff -->
>
> <tr>
> <td colspan="2">
> we have a variation <s:property value="name" />
> </td>
> </tr>
>
> <s:if test="display == 'radio'">
>
> <s:radio key="name"   list="itemList"   />
>
> </s:if>
>
> <s:if test="display == 'dropdown'">
>
> <s:select list="itemList" key="name"></s:select>
>
> </s:if>
>
> </table>
>
> </s:iterator>
>
>
>
> public ArrayList<String> getItemList() {
>
> logger.debug("get item list");
> ArrayList<String> retval = new ArrayList<String>();
> retval.add("the first option");
> retval.add("the second option");
> return retval;
> }
>
--
e: [hidden email]
m: 908-380-8699
s: davelnewton_skype
t: @dave_newton <https://twitter.com/dave_newton>
b: Bucky Bits <http://buckybits.blogspot.com/>
g: davelnewton <https://github.com/davelnewton>
so: Dave Newton <http://stackoverflow.com/users/438992/dave-newton>
Reply | Threaded
Open this post in threaded view
|

Re: Dynamically generating multiple sets of radio buttons

Dave Weis
I'm trying to make a form that can contain an arbitrary number of attribute
choices. Think of a tshirt with color and size.

I have a Product that contains Variations (size, color) that contain
VariationItems (blue, XL).

The variation can be either a dropdown or a radio button set.

<s:iterator value="product.variations" status="variation">

<table>

<tr>
<td colspan="2">
we have a variation <s:property value="name" />
</td>
</tr>

<s:if test="display == 'radio'">

<s:radio listKey="num" listValue="value" list="itemList"
name="variation[%{#variation.index}].num" />

</s:if>

<s:if test="display == 'dropdown'">

<s:select list="itemList" listKey="num" listValue="value"
name="variation[%{#variation.index}].num" ></s:select>

</s:if>

</table>

</s:iterator>




On Sun, Feb 25, 2018 at 8:57 AM, Dave Newton <[hidden email]> wrote:

> It’s not entirely clear to me what you’re asking.
>
> `getItemList` will return whatever it returns—if the logic for what should
> return lies in the view layer then it’s an architectural problem.
>
> If you need to return two lists then you should have two list getters. If
> the view layer *presentation* of the list varies by list content then the
> list should be presented in a structure that represents that, e.g.,
>
>     Class ViewList {
>         String representation; // e.g., “radio”, “dropdown”, etc.
>         List items; // The things to show
>     }
>
> If it depends on actions *taken* in the view layer, e.g., something
> JavaScript-y, then all this is out the window and we’d need more
> information.
>
> Dave
>
> On Sat, Feb 24, 2018 at 5:00 PM Dave Weis <[hidden email]> wrote:
>
> > Hello
> >
> > I am trying to make multiple sets of radio buttons with independent lists
> > of valid options. The options are all generated dynamically.
> >
> > I'm having trouble figuring out how to declare the item list provider in
> my
> > java code. I have it working fine for a single item. With the sample
> below
> > I get the same list for both my radio buttons and my dropdown but I need
> to
> > be able to modify the list returned based on which line of input I'm on.
> >
> > Thank you
> > dave
> >
> >
> >
> >
> >
> > <s:iterator value="product.variations">
> >
> > <table>
> > <!--  variation stuff -->
> >
> > <tr>
> > <td colspan="2">
> > we have a variation <s:property value="name" />
> > </td>
> > </tr>
> >
> > <s:if test="display == 'radio'">
> >
> > <s:radio key="name"   list="itemList"   />
> >
> > </s:if>
> >
> > <s:if test="display == 'dropdown'">
> >
> > <s:select list="itemList" key="name"></s:select>
> >
> > </s:if>
> >
> > </table>
> >
> > </s:iterator>
> >
> >
> >
> > public ArrayList<String> getItemList() {
> >
> > logger.debug("get item list");
> > ArrayList<String> retval = new ArrayList<String>();
> > retval.add("the first option");
> > retval.add("the second option");
> > return retval;
> > }
> >
> --
> e: [hidden email]
> m: 908-380-8699
> s: davelnewton_skype
> t: @dave_newton <https://twitter.com/dave_newton>
> b: Bucky Bits <http://buckybits.blogspot.com/>
> g: davelnewton <https://github.com/davelnewton>
> so: Dave Newton <http://stackoverflow.com/users/438992/dave-newton>
>
Reply | Threaded
Open this post in threaded view
|

Re: Dynamically generating multiple sets of radio buttons

Dave Weis
In reply to this post by Dave Newton-6
I've distilled my issue down a bit. Now I can't get my indexed setters to
be called with the values from the page.

This is in my HTML created with the radio tag:
<tr>
<td class="tdLabel"></td>
<td
class="tdInput"
><input type="radio" name="variation[2000]" id="cart_variation_2000_500"
value="500"/><label for="cart_variation_2000_500">HOT</label>
<input type="radio" name="variation[2000]" id="cart_variation_2000_1500"
value="1500"/><label for="cart_variation_2000_1500">HOTTER</label>
</td>
</tr>



<tr>
<td class="tdLabel"></td>
<td
class="tdInput"
><select name="variation[13000]" id="cart_variation_13000_">
<option value="5500">Small</option>
<option value="5600">Medium</option>
<option value="5700">Large</option>


</select>

My setter in the destination action is:

     public void setVariation(HashMap<String, String> variationItem){

    logger.error("setVariation was called with variations");

    for (Map.Entry<String, String> entry : variationItem.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();

        logger.error(key + "=" + value);
    }


     }

It gets called but there is no data in the hashmap.

I also turned up the debugging in log4j and see the following:

07:05:39.556 DEBUG com.opensymphony.xwork2.ognl.SecurityMemberAccess -
Checking access for [target:
com.subecon.actions.commerce.CartAction@56dda392, member: public void
com.subecon.actions.commerce.CartAction.setVariation(java.util.HashMap),
property: variation]
07:05:39.556 DEBUG com.opensymphony.xwork2.ognl.SecurityMemberAccess -
Checking access for [target:
com.subecon.actions.commerce.CartAction@56dda392, member: public void
com.subecon.actions.commerce.CartAction.setVariation(java.util.HashMap),
property: variation]
07:05:39.557 DEBUG com.opensymphony.xwork2.ognl.SecurityMemberAccess -
Checking access for [target:
com.subecon.actions.commerce.CartAction@56dda392, member: public void
com.subecon.actions.commerce.CartAction.setVariation(java.util.HashMap),
property: variation]
07:05:39.557 DEBUG com.opensymphony.xwork2.ognl.SecurityMemberAccess -
Checking access for [target:
com.subecon.actions.commerce.CartAction@56dda392, member: public void
com.subecon.actions.commerce.CartAction.setVariation(java.util.HashMap),
property: variation]
07:05:39.557 DEBUG com.opensymphony.xwork2.ognl.SecurityMemberAccess -
Checking access for [target:
com.subecon.actions.commerce.CartAction@56dda392, member: public void
com.subecon.actions.commerce.CartAction.setVariation(java.util.HashMap),
property: variation]
07:05:39.557 DEBUG com.opensymphony.xwork2.ognl.SecurityMemberAccess -
Checking access for [target:
com.subecon.actions.commerce.CartAction@56dda392, member: public void
com.subecon.actions.commerce.CartAction.setVariation(java.util.HashMap),
property: variation]
07:05:39.557 ERROR com.subecon.actions.commerce.CartAction - setVariation
was called with variations
07:05:39.557 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter -
Retrieving convert for class [class
com.subecon.actions.commerce.CartAction] and property [variation.null]
07:05:39.557 DEBUG com.opensymphony.xwork2.conversion.impl.XWorkConverter -
Retrieving convert for class [class
com.subecon.actions.commerce.CartAction] and property [variation.null]

Is my method signature incorrect? Thank you!
dave







On Sun, Feb 25, 2018 at 8:57 AM, Dave Newton <[hidden email]> wrote:

> It’s not entirely clear to me what you’re asking.
>
> `getItemList` will return whatever it returns—if the logic for what should
> return lies in the view layer then it’s an architectural problem.
>
> If you need to return two lists then you should have two list getters. If
> the view layer *presentation* of the list varies by list content then the
> list should be presented in a structure that represents that, e.g.,
>
>     Class ViewList {
>         String representation; // e.g., “radio”, “dropdown”, etc.
>         List items; // The things to show
>     }
>
> If it depends on actions *taken* in the view layer, e.g., something
> JavaScript-y, then all this is out the window and we’d need more
> information.
>
> Dave
>
> On Sat, Feb 24, 2018 at 5:00 PM Dave Weis <[hidden email]> wrote:
>
> > Hello
> >
> > I am trying to make multiple sets of radio buttons with independent lists
> > of valid options. The options are all generated dynamically.
> >
> > I'm having trouble figuring out how to declare the item list provider in
> my
> > java code. I have it working fine for a single item. With the sample
> below
> > I get the same list for both my radio buttons and my dropdown but I need
> to
> > be able to modify the list returned based on which line of input I'm on.
> >
> > Thank you
> > dave
> >
> >
> >
> >
> >
> > <s:iterator value="product.variations">
> >
> > <table>
> > <!--  variation stuff -->
> >
> > <tr>
> > <td colspan="2">
> > we have a variation <s:property value="name" />
> > </td>
> > </tr>
> >
> > <s:if test="display == 'radio'">
> >
> > <s:radio key="name"   list="itemList"   />
> >
> > </s:if>
> >
> > <s:if test="display == 'dropdown'">
> >
> > <s:select list="itemList" key="name"></s:select>
> >
> > </s:if>
> >
> > </table>
> >
> > </s:iterator>
> >
> >
> >
> > public ArrayList<String> getItemList() {
> >
> > logger.debug("get item list");
> > ArrayList<String> retval = new ArrayList<String>();
> > retval.add("the first option");
> > retval.add("the second option");
> > return retval;
> > }
> >
> --
> e: [hidden email]
> m: 908-380-8699
> s: davelnewton_skype
> t: @dave_newton <https://twitter.com/dave_newton>
> b: Bucky Bits <http://buckybits.blogspot.com/>
> g: davelnewton <https://github.com/davelnewton>
> so: Dave Newton <http://stackoverflow.com/users/438992/dave-newton>
>
Reply | Threaded
Open this post in threaded view
|

Re: Dynamically generating multiple sets of radio buttons

Yasser Zamani-2


On 2/27/2018 4:37 PM, Dave Weis wrote:

> <input type="radio" name="variation[2000]" id="cart_variation_2000_1500"
> value="1500"/><label for="cart_variation_2000_1500">HOTTER</label>
>      public void setVariation(HashMap<String, String> variationItem){
>
>     logger.error("setVariation was called with variations");
>
>     for (Map.Entry<String, String> entry : variationItem.entrySet()) {
>         String key = entry.getKey();
>         String value = entry.getValue();
>
>         logger.error(key + "=" + value);
>     }
>
>
>      }
> Is my method signature incorrect? Thank you!

I guess Struts wont call setVariation for e.g. variation[2000]=HOTTER.
Instead I guess it calls getVariation().put(2000, "HOTTER"); maybe you
should use Map<Integer, String> for defining variation instead with java
simple getter and setter.

Regards.


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Dynamically generating multiple sets of radio buttons

Dave Weis
That was it! It's now putting key-value pairs in the map obtained by
getVariation and they appear when I get to the execute method.

08:19:35.682 ERROR CartAction - getVariation was called
08:19:35.698 ERROR CartAction - getVariation was called
08:19:36.086 DEBUG CartAction - cart action execute
08:19:36.086 ERROR CartAction - 13000=5600
08:19:36.086 ERROR CartAction - 2000=500

Thank you!

dave





On Sat, Mar 3, 2018 at 1:17 AM, Yasser Zamani <[hidden email]>
wrote:

>
>
> On 2/27/2018 4:37 PM, Dave Weis wrote:
> > <input type="radio" name="variation[2000]" id="cart_variation_2000_1500"
> > value="1500"/><label for="cart_variation_2000_1500">HOTTER</label>
> >      public void setVariation(HashMap<String, String> variationItem){
> >
> >     logger.error("setVariation was called with variations");
> >
> >     for (Map.Entry<String, String> entry : variationItem.entrySet()) {
> >         String key = entry.getKey();
> >         String value = entry.getValue();
> >
> >         logger.error(key + "=" + value);
> >     }
> >
> >
> >      }
> > Is my method signature incorrect? Thank you!
>
> I guess Struts wont call setVariation for e.g. variation[2000]=HOTTER.
> Instead I guess it calls getVariation().put(2000, "HOTTER"); maybe you
> should use Map<Integer, String> for defining variation instead with java
> simple getter and setter.
>
> Regards.
>
>