我有一个带有inputText的屏幕,旁边有一个(+)按钮,当用户按下那个按钮时,表单应该在它旁边添加另一个额外的inputText(或者下面,无论如何)
以下是代码:
<table>
<tr>
<td>
<p:inputText value="#{controller.x}" />
<img src="../images/ico_plus.png" />
</td>
</tr>
</table>
在Controller.java中:
private String x;
public String getX(){return x}
public void setX(String val){x = val}
我需要使用多个字段填充页面,并且控制器要将所有字段值设为'fetched
答案 0 :(得分:2)
这个问题不止一次被回答,基本上你需要为bean中的所有字段保留List
,然后用你的按钮删除或添加List
。请注意,在ViewScoped
或SessionScoped
处,您的List
将在每次操作时重置,这一点非常重要。
查看:
<h:form>
<h:dataTable id="tblFields" value="#{bean.fields}" var="field">
<h:column>
<h:inputText value="#{field.value}" />
</h:column>
<h:column>
<h:commandButton value="Remove">
<f:ajax listener="#{bean.onButtonRemoveFieldClick(field)}" immediate="true" render="@form" />
</h:commandButton>
</h:column>
</h:dataTable>
<h:commandButton value="Add">
<f:ajax listener="#{bean.onButtonAddFieldClick}" execute="@form" render="tblFields" />
</h:commandButton>
</h:form>
助手类:
public class Field implements Serializable
{
private String m_sName;
public void setName(String p_sName)
{
m_sName = p_sName;
}
public String getName()
{
return m_sName;
}
}
Bean:
@ManagedBean
@ViewScoped
public class Bean implements Serializable
{
private List<Field> m_lFields;
public Bean()
{
m_lFields = new ArrayList();
m_lFields.add(new Field());
}
public void setFields(List<Field> p_lFields)
{
m_lFields = p_lFields;
}
public List<Field> getFields()
{
return m_lFields;
}
public void onButtonRemoveFieldClick(final Field p_oField)
{
m_lFields.remove(p_oField);
}
public void onButtonAddFieldClick(AjaxBehaviorEvent p_oEvent)
{
m_lFields.add(new Field());
}
}