3

I have a page with several h:selectOneMenu or p:selectOneMenu and I want to use the same page for editing and adding data. When I will edit data I need f:selectItem. I know that this component doesn't have attribute rendered. And I read that I can use <c:if>.

Ok. For example, if I write

<p:selectOneMenu rendered="#{not empty bean.id}"
    value="#{bean.selectedId}">
    <c:if test="${editableBean != null}">
        <f:selectItem itemLable="#{editableBean.name} itemValue=#{editableBean.id} />
    </c:if>
    <f:selectItems value="#{bean.listItems}" var="item"
        itemLabel="#{item.name}" itemValue="#{item.id}"/>
</p:selectOneMenu>

Will it works without any problems in primefaces and with ajax listeners?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ray
  • 1,788
  • 7
  • 55
  • 92
  • 1
    @GaborSch if you don't know how JSF works, please refrain to add comments that doesn't help OP to address a good solution. – Luiggi Mendoza Jan 25 '13 at 15:46
  • @LuiggiMendoza I know how JSF works, but I want the OP to try before he asks. BTW, the answer is `Yes` or `No`, so this is not a good question. – gaborsch Jan 25 '13 at 15:50
  • 1
    @GaborSch if you know how JSF works, then you already know that OP's suggestion is not the best solution =\, and if you really know, please provide an answer :). – Luiggi Mendoza Jan 25 '13 at 15:51
  • @LuiggiMendoza Let's not fight on this. OP wants to know whether a solution (piece of code) works or not. To decide this question, someone has to try it. – gaborsch Jan 25 '13 at 15:56

1 Answers1

4

The easy solution (but with poor performance) will be to have a boolean editMode attribute in your managed bean to enable/disable the components. Basic example:

<p:selectOneMenu rendered="#{not empty bean.id}" disabled="#{bean.editMode}"
    value="#{bean.selectedId}">
    <f:selectItems value="#{bean.listItems}" var="item"
        itemLabel="#{item.name}" itemValue="#{item.id}"/>
</p:selectOneMenu>

In your bean

@ManagedBean
@ViewScoped
public class Bean {

    private int id;
    private boolean editMode;
    //other attributes...
    //getters and setters...

    @PostConstruct
    public void init() {
        //a way to know if the bean it's in edit mode
        editMode = (id != 0);
    }
}

This solution will have poor performance because every <p:selectOneMenu> will have to load all the data and then select the actual value, but it will do what you want. Another option will be to use this attribute for the rendered property of <p:selectOneMenu> and for an <h:inputText disabled="true" readonly="true" /> (or maybe <h:outputText />). Another basic sample:

<p:selectOneMenu rendered="#{not empty bean.id && not bean.editMode}"
    value="#{bean.selectedId}">
    <f:selectItems value="#{bean.listItems}" var="item"
        itemLabel="#{item.name}" itemValue="#{item.id}"/>
</p:selectOneMenu>

<h:inputText rendered="#{bean.editMode}" value="{bean.selectedText}"
    disabled="true" readonly="true" />
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • mayb i dint understand the question properly but can't we just change the backing collection which is wired with `f:selectItems` conditionally ? – PermGenError Jan 25 '13 at 15:56
  • 1
    @GanGnaMStYleOverFlowErroR that could be another solution but I tend to have almost all the list for `` in an `@ApplicationScoped` managed bean and updating the values from time to time instead of getting the list values on each request. – Luiggi Mendoza Jan 25 '13 at 16:00