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

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

问题描述

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

 < h:form id =foodTableForm> 
< p:dataTable id =foodTableIdvar =vvalue =#{dashboardBean.myFoodList}editable =true>
< p:ajax event =rowEditlistener =#{dashboardBean.onEdit}/>
< p:ajax event =rowEditCancellistener =#{dashboardBean.onCancel}/>
< p:column sortBy =#{v.project}headerText =项目名称>
< 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 =deleteFoodPromotionactionListener =#{dashboardBean.deleteFoodPromotion(v)}update =@ form/>
< / p:column>
< / p:dataTable>
< / h:form>

< h:form id =dialogInputForm>
< p:dialog widgetVar =dlg>
< p:inputText id =firstnamevalue =#{dashboardBean.foodPromoDTO.project}required =true/>
< p:calendar value =#{dashboardBean.foodPromoDTO.promoDate}id =startDaterequired =true/>
< h:selectOneMenu value =#{dashboardBean.foodPromoDTO.action}required =true>
< f:selectItem itemLabel =Promo StartitemValue =Promo Start/>
< f:selectItem itemLabel =Promo EnditemValue =Promo End/>
< / h:selectOneMenu>
< p:commandLink id =submitButtonvalue =保存action =#{dashboardBean.addFoodPromotion}update =@ formoncomplete =PF('dlg')hide() />
< / p:dialog>
< / h:form>


解决方案

不仅需要在对话框之前重新创建模型被打开,但您也需要在打开对话框的内容之前先对ajax进行更新。



这是一个开始示例:

 < h:form id =form> 
< p:dataTable id =tablevalue =#{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 =Addaction =#{bean.add}
update =:dialogoncomplete =w_dialog.show()/>
< / h:form>

< p:dialog id =dialogwidgetVar =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 =保存action =#{bean.save}
update =:form:tableoncomplete =w_dialog.hide )/>
< / h:form>
< / p:dialog>

(注意:表单必须进入对话框,不在外面!) / p>

与此bean:

 私人列表<实体>实体; // + getter 
private Entity entity; // + getter

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

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

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



另请参见:




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>

解决方案

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.

Here's a kickoff example:

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

(note: form must go inside dialog, not outside!)

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

See also:

这篇关于添加新项目时如何清除并重新使用p:对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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