Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Radio buttons, ajax and rendering.

 
Vladislav Simovic
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So basically what i am trying to achieve is when i select certain radio button some things get rendered and some not. Ofcorse, i dont need to render the hole page again, just some things. I have written something like this, but this is not working:



How to do this properly?
 
karan khosla
Greenhorn
Posts: 25
IntelliJ IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I believe you have to do few things to make it work.

Firstly,

<f:ajax event="change" render="area" listener="#{beanName.methodWithYourLogic}" />

Here, "methodWithYourLogic" is the method name can be anything that you define in your bean class with the logic on what basis you want to display the component or the other way around. In this method by the end make that variable true so it will show the component.

Secondly,


<h:inputTextarea id="area" render=#{beanName.someboolVar} style="margin-left: 50px"/>

Hope! This Helps.
 
Vladislav Simovic
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<f:ajax> Unable to attach <f:ajax> to non-ClientBehaviorHolder parent

is the error i get. it seems you cant put <f:ajax> into <f:selectItem>. Without this i cant do ajax request when certain radio button is selected. Is there any other way to do this?
 
karan khosla
Greenhorn
Posts: 25
IntelliJ IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes! You have to put in <f:ajax> inside a component. For Example:



Here,

variableInBean, most likely has to be string. In bean you have to fetch this variable to know what user has selected.
listInBean, has to be a container. for example, ArrayList.

This is a dynamic way of doing it.

OR,

Static way where radio buttons has fixed labels.



Hope! This Helps.
 
deva kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

<h:selectOneRadio id="selectRadioId" style="padding-left: 10px; padding-right: 10px;" rendered="true" >
<f:selectItem value="one" rendered="true" />
<f:selectItem value="two" rendered="true" />
<f:ajax event="change" render="roleData" listener="#{testClass.getRolesList()}"/>
</h:selectOneRadio>



<h:dataTable id="roleData" rows="3" value="#{testClass.allRoles}" var="role" rendered="true" border="2">
<h:column>
<f:facet name="header">name</f:facet>
#{role.role}
</h:column>
<h:column>
<f:facet name="header">id</f:facet>
${role.id}
</h:column>
</h:dataTable>

While trying above code i'm getting error

"java.lang.String cannot be cast to javax.faces.model.SelectItem"

also the datatable is not rendered.

help to resolve this.

thanks
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Deva!

You can make your sample code and XML easier to read by using the "Code" button in our message editor. Also, we would prefer that you didn't "piggyback" onto an existing topic if you have a question of your own. Start your own topic. It makes it easier for people to follow.

Your exception comes from the fact that the SelectOneRadiobutton control does not take string labels, it takes SelectItem model objects. A SelectItem is a label/value pair where the itemlabel is the text displayed next to the button and the itemvalue is what the radioButton's value will be set to when the button is clicked.

In essence, the SelectOneRadiobutton has mechanics that work exactly like a selectOneMenu control, except that instead of rendering as a dropdown list, you get buttons.
 
deva kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic