Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

jsf - How to clear out and reuse p:dialog when adding new items

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...