gpt4 book ai didi

jsf - 如何清除并重新使用p :dialog when adding new items

转载 作者:行者123 更新时间:2023-12-02 02:42:36 26 4
gpt4 key购买 nike

我正在使用<p:dialog>将新行添加到 <p:dataTable>接受用户的输入,但我无法重置它。每次它都会显示先前输入的数据,而不是添加它,而是编辑当前行。如何清除字段?

<h:form id="foodTableForm">
<p:dataTable id="foodTableId" var="v" value="#{dashboardBean.myFoodList}" editable="true">
<p:ajax event="rowEdit" listener="#{dashboardBean.onEdit}" />
<p:ajax event="rowEditCancel" listener="#{dashboardBean.onCancel}" />
<p:column sortBy="#{v.project}" headerText="Project Name">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{v.project}" />
</f:facet>
<f:facet name="input">
<p:inputTextvalue="#{v.project}"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="#{msg['product.label.edit']}">
<p:rowEditor />
</p:column>
<p:column headerText="#{msg['product.label.delete']}">
<p:commandLink id="deleteFoodPromotion" actionListener="#{dashboardBean.deleteFoodPromotion(v)}" update="@form" />
</p:column>
</p:dataTable>
</h:form>

<h:form id="dialogInputForm">
<p:dialog widgetVar="dlg">
<p:inputText id="firstname" value="#{dashboardBean.foodPromoDTO.project}" required="true" />
<p:calendar value="#{dashboardBean.foodPromoDTO.promoDate}" id="startDate" required="true" />
<h:selectOneMenu value="#{dashboardBean.foodPromoDTO.action}" required="true">
<f:selectItem itemLabel="Promo Start" itemValue="Promo Start" />
<f:selectItem itemLabel="Promo End" itemValue="Promo End" />
</h:selectOneMenu>
<p:commandLink id="submitButton" value="Save" action="#{dashboardBean.addFoodPromotion}" update="@form" oncomplete="PF('dlg').hide();" />
</p:dialog>
</h:form>

最佳答案

您不仅需要在打开对话框之前重新创建模型,还需要在打开对话框之前通过 ajax 更新对话框的内容。

这是一个启动示例:

<h:form id="form">
<p:dataTable id="table" value="#{bean.entities}" var="entity">
<p:column>#{entity.property1}</p:column>
<p:column>#{entity.property2}</p:column>
<p:column>#{entity.property3}</p:column>
...
</p:dataTable>
<p:commandButton value="Add" action="#{bean.add}"
update=":dialog" oncomplete="w_dialog.show()" />
</h:form>

<p:dialog id="dialog" widgetVar="w_dialog">
<h:form>
<p:inputText value="#{bean.entity.property1}" />
<p:inputText value="#{bean.entity.property2}" />
<p:inputText value="#{bean.entity.property3}" />
...
<p:commandButton value="Save" action="#{bean.save}"
update=":form:table" oncomplete="w_dialog.hide()" />
</h:form>
</p:dialog>

(注意:表单必须位于对话框内部,而不是外部!)

用这个bean:

private List<Entity> entities; // +getter
private Entity entity; // +getter

@PostConstruct
public void init() {
entities = new ArrayList<>();
}

public void add() {
entity = new Entity();
}

public void save() {
entities.add(entity);
}

另请参阅:

关于jsf - 如何清除并重新使用p :dialog when adding new items,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20285512/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com