Can I rerender a jsf ui component when a valuechangelistener method is run?
The reason i'm asking is that my valuechangelistener method changes the values of the input boxes in the backing bean but they don't seem to be rerender.
What happens eventually is that the values that are printed on screen are saved to the backing bean, overriding the values loaded through the valuechangelistener method.
The following doesn't work:
<h:inputText id="inputbox_id" value="#{name}"/>
<h:selectOneMenu valueChangeListener="#{myBean.changeCountryMenu}">
<a4j:support event="onchange" rerender="inputbox_id" action="#{bean.test}>
</h:selectOneMenu>
Notice that bean.test() is never run. (UPDATE: It does, I had a renderResponse() that skipped it before. The values still do not reRender though)
So the solution I thought of is to rerender the inputbox from the valueChangeListener.
If there is some other better solution i'd be glad to hear...
Thank you!
Ben.
The attribute name is reRender (with capital R the second time). Perhaps you have to add immediate="true" ?
Also, check this article
Related
I have a dialog which need to be rerender after a certain selectOneMenu ist chosen.
To accomplish that, the following code is used inside the selectOneMenu:
<p:ajax event="valueChange" oncomplete="PF('dialog').initPosition();" update="panelGrid" />
However, after the dialog is rerendered, all user inputs in my p:inputTextare lost (reset to value from java bean).
How can I make the inputText keep the new value without persisting it to backend?
provide the XHTML page where your inputText component is situated. My best guess to solve your problem is by adding the p:ajax component inside the inputText component. p:ajax as defined below triggers on the default event which is change and processes #this which is the inputText component. this way it saves your input on the backing bean as soon as you exit the field.
<p:inputText value="#{bean.value}" >
<p:ajax />
</p:inputText>
I've got this code where some buttons show only under a certain condition, like this:
<h:panelGroup rendered="#{mayusculasBean.jefeCerca}">
<h:panelGrid columns="4">
<h:commandButton value="Listar"
actionListener="#{gestorEmpleados.listarEmpleados}"
immediate="true"/>
...`
</h:panelGroup>
The rendered attribute is working ok. The problem is that the actionListener method is not being called when the commandButton is pressed. However, if I get rid of the rendered attribute, the button is working properly.
I think it might be related to the different phases of the Jsf request, so when the commandButton is pressed the rendered attribute, I don't know why, evaluates to false, therefore avoiding the call to the actionListener method... but it's just a guess, I really have no idea.
Any help?
If your mayusculasBean is #RequestScoped, then, after response committed, your command button component will not be tracked by the server.
If this is your case, you need to change your mayusculasBean to a wider scope such as #ViewScoped.
I'm having trouble to understand why valueChangeListener isn't triggered.
I came accross this topic valueChangeListener Not fired when rendered/disabled attribute is added
but it didn't help so much.
When my xhtml is this :
<h:inputText value="#{cm.reference}"
rendered="#{cm.endDate eq null}"
valueChangeListener="#{userDataBean.ContactMethodChanged}" />
it's working (I mean the valueChangeListener is triggered)
But when I try this :
<h:inputText value="#{cm.reference}"
rendered="#{cm.contactMethodId eq param.contactMethodID}"
valueChangeListener="#{userDataBean.ContactMethodChanged}" />
it doesn't.
Unfortunately I need to have the second option working.
More info :
I'm inside a h:dataTable where I iterate on a list of ContactMethod (cm)
UserDataBean is applicationScoped
Apache Tomcat 7.0
JSF 2.2 (Mojarra 2.2.0)
Thank you for any help.
If the input is not processed during the form submit, then that can only mean that the rendered attribute didn't evaluate true during processing the form submit, which in turn can only mean that the #{cm.contactMethodId} has incompatibly changed (e.g. because it's a request scoped bean property instead of a view scoped bean property) and/or that #{param.contactMethodID} isn't present during the form submit.
Provided that the bean is in the right scope for the job (otherwise the original rendered approach would likely not have worked either), then retaining the request parameter as follows in the command button/link responsible for submitting the form should do it:
<h:commandButton ...>
<f:param name="contactMethodID" value="#{param.contactMethodID}" />
</h:commandButton>
i wanted to know if theres a way how to hide a commandbutton to the end users when he deselects all items in the selectCheckboxMenu.
thanks for every comments and suggestions.
Just let the rendered attribute of the command button check if the very same property behind the value attribute of select checkbox menu does not represent an empty collection. You can re-execute this check by updating a persistent parent component of the button using <p:ajax> on change of the select checkbox menu.
So, in a nutshell:
<p:selectCheckboxMenu value="#{bean.selectedItems}">
<f:selectItems value="#{bean.availableItems}" />
<p:ajax update="button" />
</p:selectCheckboxMenu>
<h:panelGroup id="button">
<p:commandButton value="Submit" rendered="#{not empty bean.selectedItems}" />
</h:panelGroup>
No need for unnecessary code such as additional bean properties or listener methods specifically for value change and rendered attribute or ugly hacks such as valueChangeListener as mentioned by the other answer whose answerer is apparently having JSF 1.x in mind.
For command button set rendered attribute for ex
In managed bean create a boolean variable allCheckBoxNotSelected with getters and setters
For checkboxes in valueChangeListener attribute call a managed bean method which will check current values for all check box. If it is not selected then put false to allCheckBoxNotSelected variable and then upadate command button through its id.
I currently have a giant ui:repeat. Within this ui:repeat, some of the repeated objects have a url to a popup image associated with them. When someone clicks display under that particular object, I need the url to popup in a p:dialog.
<ui:repeat var="thing" value="#{bean.thingList}">
<p:commandLink value="details" onclick="miniImage.show();"
update=":#{p:component('chart')}"
action="#{bean.setCurrentImg(thing.imageUrl)}"
rendered="#{thing.includeImage}">
</p:commandLink>
</ui:repeat>
and at the bottom of the page:
<p:dialog id="chart" widgetVar="miniImage" >
<h:graphicImage value="#{bean.currentImg}"/>
</p:dialog>
And in the backing bean I tried using a simple setter and getter for currentImg.
I am a bit confused on this now and would like to accomplish this without having to submit the entire form as well. Any help is greatly appreciated.
If you're using PrimeFaces 3.3 or newer, you could just add partialSubmit="true" to the command component. You can then control the to-be-processed components in process attribute. In this particular case, just the current component (the command component itself) is sufficient, thus so process="#this":
<p:commandLink ... process="#this" partialSubmit="true" />
This way only the request parameters which are really necessary for the process will be sent.
Unrelated to the concrete problem, I suggest to use oncomplete instead of onclick to open the dialog. Otherwise the dialog is opened before update takes place and may cause poor user experience as the enduser would see the image instantly changing.