简体   繁体   English

JSF/Primefaces:更新对话框不起作用

[英]JSF/Primefaces: Update Dialog not working

I have a dataTable with a commandButton in each row .我有一个dataTable ,每row都有一个commandButton If I click the commandButton a dialog should open with some more details one the selected row.如果我点击commandButton一个dialog应该多用一些细节一个选定行打开。

<p:commandButton
                        update="mainForm:myDialog"
                        action="#{bean.fetchData(user)}"
                        oncomplete="PF('myDialog').show();"
                        title="Details"
                    />

This is the fetchData() method in the bean :这是beanfetchData()方法:

 public void fetchData(user) {
        this.events = this.db.fetchDataForUser(user);
    }

The dialog:对话框:

<p:dialog
    header="Details"
    id="myDialog"
    modal="true"
    resizable="false"
    width="1000px"
    height="700px"
>
    <p:dataTable
        value="#{bean.events}"
        var="event"
    >
        <p:column
            headerText="Event"
            style="vertical-align: top; text-align: center;"
            width="auto"
        >
            <h:outputText
                value="#{event.eventType}"
            />
        </p:column>


       ....


    </p:dataTable>
</p:dialog>

The idea is that, when the commandButton is pressed, fetchData() gets executed and sets the events list.这个想法是,当按下commandButton时, fetchData()被执行并设置events列表。 After that the dialog opens and displays the events .之后, dialog打开并显示events But it's not working.但它不起作用。 Before fetchData is called, the getter executes multiple times.fetchData之前, getter执行多次。 Then fetchData() is getting called as expected and sets the events list.然后按预期调用fetchData()并设置events列表。 But when the getter for the events is getting called again, events is still null .但是当eventsgetter再次被调用时, events仍然是null

follow these steps and examine example from PrimeFaces showcase按照以下步骤检查 PrimeFaces 展示中的示例

  • First, put an object in your bean as selectedUser.首先,将一个对象作为 selectedUser 放入 bean 中。
  • Then, You have to set selectedUser.然后,您必须设置 selectedUser。 You can use setPropertyActionListener for it.您可以为它使用setPropertyActionListener
  • Then, you can call your method in dialog.然后,您可以在对话框中调用您的方法。 Your method will be like this你的方法会是这样

    public void fetchData() { this.events = this.db.fetchDataForUser(this.selectedUser); public void fetchData() { this.events = this.db.fetchDataForUser(this.selectedUser); } }

You can find a solution in PrimeFaces - Showcase - DataTable - Selection .您可以在PrimeFaces-Showcase-DataTable-Selection 中找到解决方案。

<p:dataTable id="basicDT" var="car" value="#{dtSelectionView.cars1}">
    <f:facet name="header">
        Basic
    </f:facet>
    <p:column headerText="Id">
        <h:outputText value="#{car.id}" />
    </p:column>
    <p:column headerText="Year">
        <h:outputText value="#{car.year}" />
    </p:column>
    <p:column headerText="Brand">
        <h:outputText value="#{car.brand}" />
    </p:column>
    <p:column headerText="Color">
        <h:outputText value="#{car.color}" />
    </p:column>
    <p:column style="width:32px;text-align: center">
         <p:commandButton update=":form:carDetail" oncomplete="PF('carDialog').show()" icon="pi pi-search" title="View">
            <f:setPropertyActionListener value="#{car}" target="#{dtSelectionView.selectedCar}" />
        </p:commandButton>
    </p:column>
</p:dataTable>

We have to set selected object with setPropertyActionListener , after than we can fetch data according to it in dialog.我们必须使用setPropertyActionListener设置选定的对象,然后我们可以在对话框中根据它获取数据。

<p:dialog header="Car Info" widgetVar="carDialog" modal="true" showEffect="fade" hideEffect="fade" resizable="false">
    <p:outputPanel id="carDetail" style="text-align:center;">
        <p:panelGrid  columns="2" rendered="#{not empty dtSelectionView.selectedCar}" columnClasses="label,value">
            <f:facet name="header">
                <p:graphicImage name="demo/images/car/#{dtSelectionView.selectedCar.brand}-big.gif"/> 
            </f:facet>

            <h:outputText value="Id:" />
            <h:outputText value="#{dtSelectionView.selectedCar.id}" />

            <h:outputText value="Year" />
            <h:outputText value="#{dtSelectionView.selectedCar.year}" />

            <h:outputText value="Color:" />
            <h:outputText value="#{dtSelectionView.selectedCar.color}" style="color:#{dtSelectionView.selectedCar.color}"/>

            <h:outputText value="Price" />
            <h:outputText value="$#{dtSelectionView.selectedCar.price}" />
        </p:panelGrid>
    </p:outputPanel>
</p:dialog>

In bean,在豆子里,

@Named("dtSelectionView")
@ViewScoped
public class SelectionView implements Serializable {

    private Car selectedCar;

    public Car getSelectedCar() {
        return selectedCar;
    }

    public void setSelectedCar(Car selectedCar) {
        this.selectedCar = selectedCar;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM