【问题标题】:How to Populate primefaces data table after searching database搜索数据库后如何填充primefaces数据表
【发布时间】: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


【解决方案1】:

我相信我已经明白了。 您输入了您的 MR NO(一个数字)。当用户提交该输入时,您希望您的数据表由您从 SQL 查询中获得的数据填充。

您应该使用&lt;p:commandButton&gt; 提交您的 MRNO。它应该调用一个运行您的查询的方法,并像这样更新您的&lt;p:dataTable&gt;

<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 获取其数据。

但是,您的代码中似乎有很多事情需要改进,例如命名。在您的init() 中调用getGetPatientBills() 不会执行任何操作。

【讨论】:

  • 嗨,你同意我对这个问题的第一条评论。与您在其上执行过滤、拆分或其他操作的非数据库相关数组相同。
  • 确实,无论数据是如何获得的,对dataTable的任何更新都会用当前数据填充它。
猜你喜欢
  • 2013-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-25
  • 1970-01-01
  • 2015-01-27
相关资源
最近更新 更多