Question

I m using JSF-2 and I want to show an Outputtext when changes h:selectOneMenu value to 'A' , but it doesnt work :

Here is the view:

 <p:column>
<p:selectOneMenu id="type"
value="#{Controller.typeR}" style="width:100px;">
    <f:selectItem itemLabel="--Selectionner--" itemValue="-1" />
    <f:selectItem itemLabel="A" itemValue="1" />
    <f:selectItem itemLabel="B" itemValue="2" />
    <f:selectItem itemLabel="C" itemValue="3" />
<p:ajax update="test"
 listener="#{Controller.handleTypeChange}" />
</p:selectOneMenu>
 </p:column>
 <p:column>
  <h:outputText id ="test" value="A OK :" rendered="#{Controller.typeAOk}" />
 </p:column>

the managed bean

 @SuppressWarnings("serial")
 @ManagedBean(name = "Controller")
 @ViewScoped
 public class NoIe{

 public void handleTypeChange(){        
        if (typeR.equals("1")) {
            setTypeAOk(true);
            System.out.print(typeAOk);
             }}

    //Getter and Setter

Any help will be greatly appreciated!

Was it helpful?

Solution

As #{Controller.typeAOk} seems to be false your outputText won't be part of the resulting html page and thus, it won't be available for updating.

In cases like this, you will need to wrap your outputText inside another component and then update that component which is always rendered. Here is an example:

<p:column>
    <p:outputPanel id="test">
        <h:outputText  value="A OK :" rendered="#{Controller.typeAOk}" />
    </p:outputPanel>
</p:column>

Once again: Only rendered components can be updated.

Here is the full code I used to test the solution (note that you can use a panelGroup too):

<h:form>
    <p:dataTable value="#{viewMBean.list}" var="l">
        <p:column>
            <p:selectOneMenu id="type" value="#{viewMBean.id}" style="width:100px;">
                <f:selectItem itemLabel="--Selectionner--" itemValue="-1" />
                <f:selectItem itemLabel="A" itemValue="1" />
                <f:selectItem itemLabel="B" itemValue="2" />
                <f:selectItem itemLabel="C" itemValue="3" />
                <p:ajax update="test" />
            </p:selectOneMenu>
        </p:column>
        <p:column>
            <h:panelGroup id ="test">
                <h:outputText value="A OK :" rendered="#{viewMBean.id eq 1}" />
            </h:panelGroup>
        </p:column>
    </p:dataTable>
</h:form>

The ManagedBean

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class ViewMBean implements Serializable {

    private Integer id;

    private List<SimpleBean> list;

    @PostConstruct
    public void setup() {
        list = new ArrayList<SimpleBean>();
        list.add(new SimpleBean(11, "A"));
        list.add(new SimpleBean(22, "B"));
        list.add(new SimpleBean(33, "C"));
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public List<SimpleBean> getList() {
        return list;
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top