动态输入文本域 [英] dynamic inputtextfields
本文介绍了动态输入文本域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用primefaces创建带有标签的动态输入文本字段。
如果我点击添加按钮就应该继续添加标签和输入文本字段。
什么组件我可以用 ?谢谢。
解决方案
您可以组合使用< h:dataTable>
使用 @ViewScoped
bean。
例如
< pre class =lang-html prettyprint-override>
< h:form>
< h:dataTable id =inputsvalue =#{bean.inputs}var =input>
< h:column>
< p:outputLabel for =inputvalue =#{input.label}/>
< / h:列>
< h:column>
< p:inputText id =inputvalue =#{input.value}/>
< / h:列>
< / h:dataTable>
< p:commandButton value =Addaction =#{bean.add}update =inputs/>
< / h:form>
with
@ManagedBean
@ViewScoped
公共类Bean实现Serializable {
private List< Input>输入;
@PostConstruct
public void init(){
inputs = new ArrayList< Input>();
}
public void add(){
输入输入= new Input();
input.setLabel(Input+(inputs.size()+ 1));
inputs.add(输入);
}
公开列表<输入> getInputs(){
返回输入;
}
}
和
public class输入{
private String label;
私有字符串值;
public String getLabel(){
return label;
}
public void setLabel(String label){
this.label = label;
}
public String getValue(){
返回值;
}
public void setValue(String value){
this.value = value;
}
}
您当然也可以使用< p:dataTable>
,但这只会增加一些花哨的look'n'feel,这对于这个特定的用例可能是不必要的。
参见:
I am trying to create dynamic input textfields with labels using primefaces.
Like if I click the add button it should keep adding a label and a input textfields.
What component I can use ? thanks.
解决方案
You can use a <h:dataTable>
in combination with a @ViewScoped
bean for this.
E.g.
<h:form>
<h:dataTable id="inputs" value="#{bean.inputs}" var="input">
<h:column>
<p:outputLabel for="input" value="#{input.label}" />
</h:column>
<h:column>
<p:inputText id="input" value="#{input.value}" />
</h:column>
</h:dataTable>
<p:commandButton value="Add" action="#{bean.add}" update="inputs" />
</h:form>
with
@ManagedBean
@ViewScoped
public class Bean implements Serializable {
private List<Input> inputs;
@PostConstruct
public void init() {
inputs = new ArrayList<Input>();
}
public void add() {
Input input = new Input();
input.setLabel("Input " + (inputs.size() + 1));
inputs.add(input);
}
public List<Input> getInputs() {
return inputs;
}
}
and
public class Input {
private String label;
private String value;
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
You can of course also use a <p:dataTable>
, but that would only add some fancy look'n'feel which is likely unnecessary for this particular use case.
See also:
这篇关于动态输入文本域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文