如何动态插入素数输入文本? [英] How to insert a primefaces input text dynamically?

查看:57
本文介绍了如何动态插入素数输入文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为numberOfGuests的字段,该字段取一个整数值.提交页面时,我想在下一页中动态插入与numberOfGuests值一样多的p:inputText字段. ui:repeat只允许我遍历一个集合.我想知道我该怎么做.

I have a field called numberOfGuests which takes an integer value. When I submit the page, I want to insert as many p:inputText fields in the next page as the numberOfGuests value dynamically. ui:repeat allows me only to iterate over a collection. I would like to know how I can do that.

谢谢.

推荐答案

只需创建一个大小为numberOfGuests的集合(或更简单地说,是一个数组).

Just create a collection (or, easier, an array) of size numberOfGuests.

例如在视图范围内的bean中:

E.g. in a view scoped bean:

private Integer numberOfGuests; // +getter+setter
private String[] guests; // +getter

public void submit() {
    guests = new String[numberOfGuests];
}

使用

<h:form>
    <p:inputText value="#{bean.numberOfGuests}" />
    <p:commandButton value="submit" action="#{bean.submit}" update=":guests" />
</h:form>

<h:panelGroup id="guests">
    <h:form rendered="#{not empty bean.guests}">
        <ui:repeat value="#{bean.guests}" varStatus="loop">
            <p:inputText value="#{bean.guests[loop.index]}" />
        </ui:repeat>
        <p:commandButton value="save" action="#{bean.save}" />
    </h:form>
</h:panelGroup>

结果仅显示在同一视图中,该视图使视图范围的bean实例保持活动状态,并且输入字段通过索引而不是通过var引用值,因为本示例中的String是不可变的.如果它是像Guest这样的复杂对象,则只需像value="#{guest.name}"那样以常规"方式绑定它们.

The result is just displayed in the same view as that keeps the view scoped bean instance alive and the input fields are referencing the value by index instead of by var as String in this example is immutable. If it were a complex object like Guest, then just bind them the "usual" way like value="#{guest.name}" or so.

这篇关于如何动态插入素数输入文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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