添加新项目时如何清除和重用p:对话框

时间:2013-11-29 12:11:08

标签: jsf primefaces dialog reset

我正在使用<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>

1 个答案:

答案 0 :(得分:4)

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

这是一个启动示例:

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

另见: