【发布时间】:2019-06-30 10:57:15
【问题描述】:
我正在开发一个应用程序,我想按行编辑我的数据表。我面临的问题是,如果我在getter 方法中填充我的ArrayList,那么rowEdit 操作不起作用,但是当我使用@ViewScoped 在@PostConstruct 方法中填充列表时,rowEdit 就可以了。所以,现在我有一个字段可以从数据库中搜索值并显示在数据表中。在创建managedbean 时,我无法在数据表中填充值,因为为了进行搜索,我必须将字段值发送到搜索方法,但在@postConstruct 中我不能。请帮帮我。
这是我的代码片段:
<p:dataTable id="patients" rendered="#{viewBills.visible}"
editable="true" value="#{viewBills.getPatientBills}" var="patient"
style="margin-bottom:20px">
<p:ajax event="rowEdit" Listener="#{viewBills.updateEdited}" update="@form"/>
<p:ajax event="rowEditCancel" Listener="#{viewBills.onRowCancel}" update="@form"/>
<p:column headerText="Name">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{patient.patName}" />
</f:facet>
<f:facet name="input">
<p:inputText id="modelInput" value="#{patient.patName}"
style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Department">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{patient.department}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{patient.department}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Amount">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{patient.amount}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{patient.amount}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Implant">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{patient.implant}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{patient.implant}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Ceramic Lab">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{patient.ceramicLab}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{patient.ceramicLab}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Comments">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{patient.comments}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{patient.comments}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Practical Charges">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{patient.practicalCharges}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{patient.practicalCharges}"
style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Material Charges">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{patient.materialCharges}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{patient.materialCharges}"
style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Fixed Ortho">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{patient.fixedOrtho}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{patient.fixedOrtho}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="MF Kit">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{patient.mfKit}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{patient.mfKit}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Army No">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{patient.armyNo}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{patient.armyNo}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Unit">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{patient.unit}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{patient.unit}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Armed Forces">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{patient.armedForces}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{patient.armedForces}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Options" style="width:50px">
<p:rowEditor />
</p:column>
</p:dataTable>
这是我的托管 Bean
@ManagedBean(name = "viewBills")
@ViewScoped
public class ManageViewBIllsBean {
public String mrNo;
public boolean visible = false;
public ArrayList<Patient> getPatientBills = new ArrayList<>();
public ArrayList<Patient> getAllPatientBills = new ArrayList<>();
public ArrayList<Patient> search() {
getPatientBills = DatabaseHandler.searchBillByMrNo(mrNo);
if (!getPatientBills.isEmpty()) {
setVisible(true);
return getPatientBills;
} else {
setVisible(false);
return getPatientBills;
}
}
@PostConstruct
public void init() {
getGetPatientBills();
setVisible(true);
}
public ArrayList<Patient> searchAllBills() {
return DatabaseHandler.searchAllBills();
}
public void executeViewAllBills() throws IOException {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(ec.getRequestContextPath() + "/viewAllBills.xhtml");
}
public void executeViewBillsByMRNo() throws IOException {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(ec.getRequestContextPath() + "/viewBillsByMRNo.xhtml");
}
public void updateEdited(RowEditEvent event) {
System.out.println(" i am in edit");
FacesMessage msg = new FacesMessage("Car Edited", ((Patient) event.getObject()).getPatName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onRowCancel(RowEditEvent event) {
FacesMessage msg = new FacesMessage("Edit Cancelled", ((Patient) event.getObject()).getPatName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public String getMrNo() {
return mrNo;
}
public void setMrNo(String mrNo) {
this.mrNo = mrNo;
}
public ArrayList<Patient> getGetPatientBills() {
return getPatientBills;
}
public ArrayList<Patient> getGetAllPatientBills() {
return getAllPatientBills;
}
public boolean isVisible() {
return visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
}
【问题讨论】:
-
就像使用固定数组而不使用搜索或数据库时一样。 PrimeFaces不知道您如何获取数据。
-
我可以这样做,但问题是我有参数“MR NO”,它将被设置为 SQL 查询中的 WHERE 子句,并且用户将输入 MR 编号,因此创建 bean 我不能这样做
-
那么你的问题对我来说完全不清楚,对不起。搜索有问题吗?从数据表还是不是?请尽量把你的问题说清楚。就目前的问题而言,正确的答案是我的第一条评论中的内容。或者你需要stackoverflow.com/questions/28559089/…
标签: java jsf primefaces datatable