如何填充primefaces数据表检索数据库后

问题描述 投票:0回答:1

我正在开发一个应用程序,我想聪明的编辑我的数据表行。我面临的问题是,如果我填充我在ArrayList行动getter方法rowEdit不工作,但是当我填充列表中@PostConstruct@ViewScopedrowEdit工作。所以,现在我有一个字段搜索数据表从数据库和显示值。我能不能填充上,因为为了寻找我要送字段值来搜索方法,但在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;
    }

}
java jsf primefaces datatable
1个回答
0
投票

我相信我已经明白。你必须输入你的MR NO(数字)。而当用户提交的输入,你希望你的数据表被你从你的SQL查询得到的数据来填充。

你应该做的是使用<p:commandButton>提交您MRNO。应该调用运行您的查询的方法,并更新<p:dataTable>像这样:

<p:commandButton value="Submit MR NO" 
                 action="#{viewBills.loadPatientsFromDatabase}" 
                 update="patients" />

所以,你需要在你的bean适当的方法:

public void loadPatientsFromDatabase()
{
    getPatientBills = DatabaseHandler.searchBillByMrNo(mrNo);
    setVisible = getPatientBills.isEmpty() ? false : true;
}

这应该做的伎俩,让您的dataTable中获取其数据。

然而,似乎有很多事情在你的代码事情需要改进,例如命名。并调用getGetPatientBills()init()没有做任何事情。

© www.soinside.com 2019 - 2024. All rights reserved.