如何动态插入素数输入文本? [英] How to insert a primefaces input text dynamically?
问题描述
我有一个名为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屋!