RE: preselect a radio button based on a value

Jacob Kjome <[email protected]>
Newsgroups gmane.comp.java.enhydra.barracuda.general
Message-ID <[email protected]>
Ok, forget my suggestion about using the BSelect component for a radio 
button.  I thought it might work, but it doesn't.  It renders, but with the 
<input> with a value as a comma delimited list of every value added to the 
list such as...

<input id="MyList" name="MyRadioSelectList" type="radio" value="One, Two, 
Three">

However, the fist case with the BList will work fine for you, although if 
you return a BList to the <input> element, the <input> element will not be 
overwritten in the page and you will get a list of input elements in 
addition to the original one.  That's somewhat problematic.  The way it is 
dealt with in CompEx1 is that the view being bound to is a <span id="blah"> 
where the span stays in the page after render, but is inconsequential since 
it is inocuous markup rather than a form element.  Seems like BSelect type 
functionality ought to be available for radio buttons and checkboxes since 
they can behave very similarly to select boxes.

Christian, what's your take on this?  Also, we really should add some 
howto's to either the Barracuda docs or the JGuru FAQ describing how to 
perform common operations like this.  A best practices of sorts.

Jake

At 03:29 PM 6/16/2003 -0500, you wrote:
>Hi Vijay,
>
>See comments below...
>
>At 10:42 AM 6/16/2003 -0700, you wrote:
>>Hi Jacob,
>>
>>Point 1 did not work.I need to have 2 separate id's for the radio button.
>
>Ah, yes.  Of course you are right.  However, what you can do in these 
>cases is the following...
>
><input type="radio" value="A" checked name="allocate" id="allocate">
><input type="radio" name="allocate" value="H" class="discardMe">
>
>The second radio button would be discarded at the XMLC compilation step 
>(as long as you provide the following to the XMLC compiler (ie. in the 
>options.xmlc file) .... -delete-class discardMe ).  So, now you would 
>leave your code the way it was before and the "allocate" property would 
>get picked up since the default identifier is the "id" attribute.
>
>Now you would populate the radio button with id (and name, of course) of 
>"allocate" using a BList component instead of a BToggleButton.  The latter 
>assumes a single form element which is either checked or not.  What you 
>want is a list of which only one element can be selected.  This is, 
>essentially, the same thing as a <select> list which has "multiple" set to 
>false (the default state).
>
>Here's is how you could do that. Check out 
>http://www.barracudamvc.org/Barracuda/CompTest1 and look at the source of 
>CompEx1.java and CompEx1.html.  The relevant bit of html is...
>
><p>The examples actually get even more interesting. For instance,
>we can also bind a list of complex components to a node. Here we
>return a series of input controls followed by text (simulating
>RadioButtons):<span id="List6">[list controls here]</span>
>
>The relevant bit of java code is...
>
>ListModel listModel4 = new CustomListModel4();
>BList wcList4 = new BList(listModel4);
>wcList4.setName("List4");
>wcList4.addView(new DefaultView(page.getElementList6()));
>wcRoot.addChild(wcList4);
>...
>...
>...
>class CustomListModel4 extends DefaultListModel {
>     public Object getItemAt(int index) {
>         ViewContext vc = getViewContext();
>         ElementFactory ef = vc.getElementFactory();
>         Element el = (Element) ef.getDocument().createElement("INPUT");
>         el.setAttribute("type","radio");
>         el.setAttribute("value","value"+index);
>         el.setAttribute("name","LM4");
>         Node textNode = (Node) 
> ef.getDocument().createTextNode("Dyn"+index+" ");
>         return new Object[] {el, textNode};
>     }
>
>     public int getSize() {return 3;}
>}
>
>Actually, you can probably just return a BSelect component since that is 
>essentially the functionality you want.  It has advantages over using a 
>plain old BList by allowing you do use a ListSelectionModel such as...
>
>DefaultListModel dlm = new DefaultListModel();
>DefaultListSelectionModel dlsm = new DefaultListSelectionModel();
>dlsm.setSelectionMode(_selectionMode); //can set this to multiple selects 
>here if needed
>dlm.add(new DefaultItemMap("0", "")); // default value
>dlsm.addSelectionInterval(0,0); // preselect the first choice
>Integer selectedVal = //get value from FormMap,Session, URLString etc....
>for (int i=0; i<answerOptions.size(); i++) {
>     answerOption = (AnswerOption) answerOptions.get(i);
>     answerOptionId = answerOption.getAnswerOptionId();
>     answerOptionDescription = answerOption.getDescription();
>     dlm.add(new DefaultItemMap(answerOptionId, answerOptionDescription));
>
>     if (answerOptionId==selectedVal.intValue()) {
>         dlsm.addSelectionInterval(i+1,i+1); //select the current choice
>     }
>}
>
>BSelect bsComp = new BSelect(dlm);
>bsComp.setSelectionModel(dlsm);
>bsComp.setName(_formElementName);
>
>
>Hope that helps.
>
>Jake
>
>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>-----Original Message-----
>>From: Jacob Kjome [mailto:[email protected]]
>>Sent: Friday, June 13, 2003 10:50 AM
>>To: Vijay Balakrishnan
>>Subject: Re: [Barracuda] preselect a radio button based on a value
>>
>>Hi Vijay,
>>barracudamvc.org seems to be down so I'm replying directly to you.
>>
>>As I look at your code below, I notice that on the input buttons, you use 
>>the "name" attribute.  Then you try to refer to those key names in your 
>>getItem() method.  However, unless you set the "name" attribute to be the 
>>identifier, the keys in your properties file containing the directives 
>>will never get triggered.  You can solve this in one of 2 ways.
>>1.  add an "id" attribute with the same value as the "name" attribute to 
>>each respective form element.  Doing this actually is more compatible 
>>with XHTML so you might think stongly about using this technique for 
>>forward compatibility reasons.
>>2.  set the "name" attribute to be the key in the properties flie using 
>>TemplateView#setIDAttrName(String iidAttrName) or sending the idAttrName 
>>into the appropriate TemplateView constructor.
>>Make sure to ping the list saying whether this works or not for you so we 
>>know that the issue is resolved or not.  You may have to wait a bit for 
>>barracudamvc.org to come back up again.  Not sure why it is down?
>>Jake
>>
>>At 02:45 PM 6/12/2003 -0700, you wrote:
>>
>>>Hi,
>>>I am trying to preselect a  radio button based on a  value.
>>><input type="radio" value="A" checked name="allocate">
>>><input type="radio" name="allocate" value="H">
>>>I always get the the value 'A' even though I have set the value for the 
>>>node
>>>to H.
>>>Here is how I am setting the BToggleButton in the handleViewEvent:
>>>String allocate = (String)session.getAttribute("allocate");
>>>       if (allocate == null) {
>>>         allocateSelected = 0;
>>>       } else if (allocate.equalsIgnoreCase("H")) {
>>>         allocateSelected = 1;
>>>       } else {
>>>         allocateSelected = 0;
>>>       }
>>>       Node         allocateNode =
>>>allocateNodeList.item(allocateSelected);<==== this has the value of "H" in
>>>it
>>>       TemplateView allocateView = new
>>>DefaultTemplateView(allocateNode,"name",
>>>           new MapStateMap(modelProps));
>>>      bodyTemplate.setView(allocateView);
>>>      bodyTemplate.render(new DefaultViewContext(context));
>>>      new DefaultDOMWriter().write(page,context.getResponse());
>>>I also have a getItem(String key) : <=== never seems to get in here
>>>    if (key.equals("allocate")) {
>>>         boolean       selA    = false;
>>>         boolean       selH    = false;
>>>         BToggleButton btbComp = null;
>>>         char          type    = poolDTO.getType();
>>>         String        typeStr = new Character(type).toString();
>>>         if (typeStr != null && typeStr.equalsIgnoreCase("A")) {
>>>           selA = true;
>>>         } else if (typeStr != null && typeStr.equalsIgnoreCase("H")) {
>>>           selH = true;
>>>         }
>>>         btbComp   = new BToggleButton(BInput.RADIO,"allocate","A",selA);
>>>         btbComp = new BToggleButton(BInput.RADIO,"allocate","H",selH);
>>>         return btbComp;
>>>       }
>>>Any help is appreciated or any example on pre-selecting a radio button 
>>>based
>>>on an input value would be great.
>>>Thanks,
>>>Vijay
>>>_______________________________________________
>>>Barracuda mailing list
>>>[email protected]
>>>http://barracudamvc.org/lists/listinfo/barracuda
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.