我有一个dataTable
,每个commandButton
中都有一个row
。如果单击commandButton
,则应打开dialog
并显示更多详细信息。
<p:commandButton
update="mainForm:myDialog"
action="#{bean.fetchData(user)}"
oncomplete="PF('myDialog').show();"
title="Details"
/>
这是fetchData()
中的bean
方法:
public void fetchData(user) {
this.events = this.db.fetchDataForUser(user);
}
对话框:
<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>
想法是,当按下commandButton
时,将执行fetchData()
并设置events
列表。之后,dialog
打开并显示events
。但这不起作用。在调用fetchData
之前,getter
执行多次。然后将按预期方式调用fetchData()
并设置events
列表。但是,当再次调用getter
的events
时,events
仍然是null
。
然后,您可以在对话框中调用方法。您的方法将是这样
public void fetchData(user){this.events = this.db.fetchDataForUser(user);}
您可以在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>
我们必须用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>
在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;
}
}