1

InputText field in the following dialog retains previous value even though I set it to blank before calling show(). The inputText field is only displayed blank when show() is called for the first time. My bean is session scoped.

<p:dialog id="dlgId" widgetVar="dlgVar" dynamic="true">
<h:form>
    <h:panelGrid columns="1"> 
       <h:outputLabel for="nametext" value="Name" />
       <p:inputText id="nametext" value="#{myBean.name}" />
    </h:panelGrid>

    <p:commandButton value="Save" actionListener="#{myBean.saveAction}" />
</h:form>

public void add(TreeNode selectedTreeNode) {
    setName("");
    RequestContext.getCurrentInstance().execute("PF('dlgVar').show()");
}

public void setName(String name) {
   this.name = name;
}

public String getName() {
   return name;
}

How can I get the inputTEext field to display the value I set before calling show() rather then the value previously entered by the user?

amah
  • 73
  • 1
  • 13

1 Answers1

2

The thing is: you need to update your form. To make it, you can use one of these solutions.

Solution 1 : update it from your xhtml

<h:form id="form">
    <h:panelGrid columns="1"> 
       <h:outputLabel for="nametext" value="Name" />
       <p:inputText id="nametext" value="#{myBean.name}" />
    </h:panelGrid>

    <p:commandButton value="Save" actionListener="#{myBean.saveAction}" update=":form" />
</h:form>

Solution 2 : update it from your managedBean

YourXhtml

<h:form id="form">
...
</h:form>

YourManagedBean

public void saveAction() {
... 
name = ""; 
RequestContext.getCurrentInstance().update(":form");
}

You can also read this post Can I update a JSF component from a JSF backing bean method?.

Solution 3 : update it using an Ajax event

You can also add an ajax event

<p:commandButton value="Save" type="button" >
    <p:ajax event="click" listener="#{myBean.saveAction}" update=":form"/>
</p:commandButton>
Vasil Lukach
  • 3,658
  • 3
  • 31
  • 40
Yagami Light
  • 1,756
  • 4
  • 19
  • 39
  • I tried both of your above suggestions and It is still not working. Let me explain further: First time dialog displayed : InputText field is blank and the user enters "Mr Smith" Second time dialog displayed: InputText field is "Mr Smith" and not blank which means user has to manually clear "Mr Smith" before entering a name. – amah Jun 22 '17 at 12:01
  • where do you have to update your dialog 1. when you open the dialog or 2. when you click on saveAction ?!? – Yagami Light Jun 22 '17 at 12:05
  • 1
    Good answer! just wrapped all possible alternatives. – cнŝdk Jun 22 '17 at 12:23
  • @YagamiLight Thanks for your help - I used your first solution and it is working now. I have a same problem with another dialog in which I would like to set a predefined value before dialog is displayed rather then in action listener. Can this be done? i.e. call name ="some value" before calling show().. – amah Jun 22 '17 at 12:40
  • @amah please make it as an answer and post a new question and i will look and try to give an answer – Yagami Light Jun 22 '17 at 14:05
  • I have accepted your answer and also posted a new question https://stackoverflow.com/questions/44704050/how-to-display-dialog-with-default-values-in-primefaces – amah Jun 22 '17 at 15:45