开发者

Change the properties of an Input within a ui:repeat

开发者 https://www.devze.com 2023-01-29 06:19 出处:网络
I\'d like to change the \"required\" property of an InputText that is located within an ui:repeat, but I\'m not able to access to 开发者_如何学编程the component from the ManagedBean:

I'd like to change the "required" property of an InputText that is located within an ui:repeat, but I'm not able to access to 开发者_如何学编程the component from the ManagedBean:

<h:selectManyCheckbox id="required" value="#{test.required}"
    layout="lineDirection" converter="javax.faces.Integer">
    <f:ajax event="change" listener="#{test.update}" />
    <f:selectItems value="#{test.selectable}"></f:selectItems>
</h:selectManyCheckbox>
<ui:repeat value="#{test.names}" var="name" id="repeat">
    <h:panelGrid columns="3">
        <h:outputLabel id="nameLabel">name:</h:outputLabel>
        <h:inputText id="name" value="#{name}"
            validator="#{test.validateName}" />
        <h:message for="name"></h:message>
    </h:panelGrid>
</ui:repeat>

I'm trying to use the findComponent method, but it does not work:

public void update(AjaxBehaviorEvent event) {
    for(Integer i: selectable) {
        UIViewRoot vr = FacesContext.getCurrentInstance().getViewRoot();
        HtmlInputText input = (HtmlInputText)vr.findComponent("form:repeat:"+i+":name");
        input.setRequired(required.contains(i));
    }
}


The ui:repeat doesn't repeat the components in the view root, it repeats the component's output in the rendered HTML output.

There are several ways to achieve this properly. One of them is to use a value object instead and set the requireness there. E.g. a List<Item> wherein Item has the properties String name and boolean required.

<ui:repeat value="#{test.items}" var="item" id="repeat">
    <h:panelGrid columns="3">
         <h:outputLabel id="nameLabel">name:</h:outputLabel>
         <h:inputText id="name" value="#{item.name}" required="#{item.required}" validator="#{test.validateName}" />
         <h:message for="name"></h:message>
    </h:panelGrid>
</ui:repeat>

There are more ways, but since the JSF version you're using and the functional requirement is unclear, it's only guessing which way is the most applicable in your case.

0

精彩评论

暂无评论...
验证码 换一张
取 消