repeat中更改输入的属性

repeat中更改输入的属性

本文介绍了在ui:repeat中更改输入的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改位于ui:repeat内的InputText的"required"属性,但无法从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>

我正在尝试使用findComponent方法,但是它不起作用:

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));
    }
}

推荐答案

ui:repeat不在视图根目录中重复组件,而是在呈现的HTML输出中重复组件的输出.

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

有几种方法可以正确实现这一目标.其中之一是改用值对象并在那里设置需求.例如. List<Item>,其中Item具有属性String nameboolean required.

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>

还有更多方法,但是由于您正在使用的JSF版本以及功能要求尚不清楚,因此只能猜测哪种方法最适合您的情况.

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.

这篇关于在ui:repeat中更改输入的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 14:14