添加新项目时如何清除和重用 p:dialog [英] How to clear out and reuse p:dialog when adding new items

查看:22
本文介绍了添加新项目时如何清除和重用 p:dialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 <p:dialog><p:dataTable> 添加一个新行,以获取用户的输入,但我无法重置它.每次它显示先前输入的数据,而不是添加它正在编辑当前行.如何清除字段?

I am using <p:dialog> to add a new row to <p:dataTable> taking input from user, but I am unable to reset it. Everytime it shows data of previous input and instead of adding it's editing the current row. How do I clear the fields?

<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-update.

Not only you need to recreate the model before the dialog is opened, but you also need to ajax-update the dialog's content before it's opened.

这是一个启动示例:

<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);
}

另见:

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