h:selectOneMenu循环播放 [英] h:selectOneMenu in loop

查看:45
本文介绍了h:selectOneMenu循环播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想记住c:forEach循环中多个h:selectOneMenu组件的值.现在,仅记住h:selectOneMenu中的最后一个值.我不知道会出现多少个h:selectOneMenu.

I want to remember values from multiple h:selectOneMenu component in c:forEach loop. Now only last value from h:selectOneMenu is remember. I don't know how many h:selectOneMenu will appear.

<h:panelGrid 
                    id="wynik"
                    columns="2"
                    border="0" 
                    cellpadding="2" 
                    cellspacing="0" 
                    rowClasses="jsfcrud_odd_row,jsfcrud_even_row" 
                    rules="all" 
                    style="border:solid 1px">
                    <h:outputText id="ns" value="Numer stanowiska"/>
                    <h:outputText id="kontr" value="Kontroler"/>
                    <c:forEach 
                        var="stanowisko"
                        begin="1" 
                        end="#{stojakiController.selected.iloscstanowisk}"
                        step="1">
                        <h:column>
                            <h:outputText value="#{stanowisko}"/>
                        </h:column>
                        <h:column>
                            <h:selectOneMenu 
                                id="kontroler_#{stanowisko}" 
                                value="#{wyposazenieStojakaController.selected.kontroler}" 
                                title="#{bundle.CreateWyposazenieStojakaTitle_kontroler}" 
                                required="true" 
                                requiredMessage="#{bundle.CreateWyposazenieStojakaRequiredMessage_kontroler}">
                                <f:selectItems value="#{kontroleryController.itemsAvailableSelectOne}"/>
                            </h:selectOneMenu>
                        </h:column>
                    </c:forEach>
                </h:panelGrid>

推荐答案

这不是基于动态大小的集合呈现表的正确方法.您将每行的值绑定到一个相同的支持bean属性.每当每行需要设置该值时,此属性都会被覆盖.这就是为什么最后只显示最后一行的值的原因.

This is not the right way to render a table based on a dynamically sized collection. You're binding the value of each row to one and same backing bean property. This property would get overwritten every time whenever each row needs to set the value. That's why you end up with only the value of the last row.

您应该改用<h:dataTable>,而不是带有<c:forEach><h:panelGrid>.您应该准备一组具体的模型对象,而不仅仅是对象的大小.例如,

You should be using <h:dataTable> instead, not a <h:panelGrid> with a <c:forEach>. You should be preparing a collection of concrete model objects, not only the size of objects. For example,

public class Item {

    private String value;

    // ...
}

然后,在辅助bean的(后)构造函数中,您应该准备尽可能多的

Then, in the (post)constructor of the backing bean you should prepare as many as necessary.

public class Bean {

    private List<Item> items;

    @PostConstruct
    public void init() {
        items = new ArrayList<Item>();

        for (int i = 0; i < yourDesiredAmountOfItems; i++) {
            items.add(new Item());
        }
    }

    // ...
}

这是一个基本的启动示例,您应该如何使用它:

And here's a basic kickoff example how you should use it:

<h:dataTable value="#{bean.items}" var="item">
    <h:column>
        <h:selectOneMenu value="#{item.value}">
            <f:selectItems value="#{bean.availableValues}" />
        </h:selectOneMenu>
    </h:column>
</h:dataTable>

提交表单时,JSF会在与该行关联的每个单独的项目对象中正确设置每一行的值.

When you submit the form, JSF will set the value of each row rightly in each separate item object associated with the row.

这篇关于h:selectOneMenu循环播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆