Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Struts radio tag create a vertical list of radio buttons

I'm using a struts radio tag that is being populated with a list of objects that have two fields:

class MyAction {
     List<MyObject> myList;
     String selectedId
     public String execute() {
         ...
         myList = new ArrayList<MyObject>();
         myList.add(new MyObject("1","first object");
         myList.add(new MyObject("2","second object");
         myList.add(new MyObject("3","second object");
         ...
     }

     // Getters and Setters for myList & selectedId
     ...
}

class MyObject {
    String id;
    String name;

    MyObject(String id, String name) {
         this.id = id;
         this.name = name;
    }
    // Getters and Setters for id & name
    ...
}

Here's what I was using on my page to display the list of radio buttons

<s:radio key="selectedId" list="myList" listKey="id" listValue="name"/>

However, this yields a horizontal list of radio buttons. I tried adding a css style to them:

<style>
    .vertical input { display: block; }
</style>

But this causes the labels and the radio buttons to show up on separate lines as well, instead of the radio button and label on the same line:

  • first object
  • second object
  • third object
  • what I want is:

  • first object
  • second object
  • third object
  • like image 882
    plainjimbo Avatar asked Apr 27 '11 19:04

    plainjimbo


    3 Answers

    its actually simple, i mean use theme simple :)

    <s:iterator value="myList"> 
      <s:radio theme="simple" name="someNameToSubmit" list="#{id:name}"/><br>
    </s:iterator> 
    

    This will make name as a label and id as the property to submit

    like image 176
    Anupam Avatar answered Nov 15 '22 19:11

    Anupam


    after some googling around a bit... I found a few solutions:

    1. Modify extend the theme and modify the struts FTL for radio buttons: Instructions here. This seemed overkill for me - or at least I'm too lazy for that :)

    2. Use an iterator tag, iterate over each list item, and output one radio button and line break for each list element. Answer came from here

    I chose option two (because I'm lazy primarily), although option one would make for a good exercise.

    Here's what my struts tag looks like now:

    <s:iterator value="myList"> 
        <s:radio key="selectedId" list="{myObject}" listKey="id" listValue="name"/><br/> 
    </s:iterator> 
    

    So the iterator works on a List, so you set the list attribute of the radio tag to be a list of containing only the current myObject. The listKey and listValue are then myObject.id and myObject.name

    like image 39
    plainjimbo Avatar answered Nov 15 '22 18:11

    plainjimbo


    I have a simple solution. In the list, add <br> to each item such as,

    first object <br> 
    

    It works though looks like a hack.

    like image 3
    Hongliu Li Avatar answered Nov 15 '22 19:11

    Hongliu Li