【问题标题】:PrimeFaces dialog doesn't working with datatablePrimeFaces 对话框不适用于数据表
【发布时间】:2013-12-09 12:43:01
【问题描述】:

我在这里关注Demo,但我不知道为什么不起作用。我花了 3 天时间才解决,但我无法弄清楚我的代码有什么问题。希望有人建议我。

我正在使用 Hibernate + JSF 2.0 + PrimeFaces 3.5。

xhtml

<h:form id="form">

    <p:growl id="msgs" showDetail="true" />

    <p:dataTable id="customers" var="customer" value="#{customerBean.customer}">

        <p:column headerText="Model" style="width:24%">
            <h:outputText value="#{customer.firstName}" />
        </p:column>

        <p:column headerText="Year" style="width:24%">
            <h:outputText value="#{customer.lastName}" />
        </p:column>

        <p:column headerText="Manufacturer" style="width:24%">
            <h:outputText value="#{customer.dob}" />
        </p:column>

        <p:column headerText="Color" style="width:24%">
            <h:outputText value="#{customer.email}" />
        </p:column>

        <p:column style="width:4%">
            <p:commandButton id="selectButton" update=":form:display" oncomplete="carDialog.show()" icon="ui-icon-search" title="View">
                <f:setPropertyActionListener value="#{customer}" target="#{customerBean.selectedCustomer}" />
            </p:commandButton>
        </p:column>

    </p:dataTable>

    <p:dialog header="Car Detail" widgetVar="carDialog" resizable="false" id="carDlg"
              showEffect="fade" hideEffect="explode">

        <h:panelGrid id="display" columns="2" cellpadding="4" style="margin:0 auto;">

            <h:outputText value="Model:" />
            <h:outputText value="#{customerBean.selectedCustomer.firstName}" style="font-weight:bold"/>

            <h:outputText value="Year:" />
            <h:outputText value="#{customerBean.selectedCustomer.lastName}" style="font-weight:bold"/>


            <h:outputText value="Manufacturer:" />
            <h:outputText value="#{customerBean.selectedCustomer.dob}" style="font-weight:bold"/>

            <h:outputText value="Color:" />
            <h:outputText value="#{customerBean.selectedCustomer.email}" style="font-weight:bold"/>

        </h:panelGrid>

    </p:dialog>

</h:form>

customerBean (RequestScoped)

public class customerBean {

    private List<Customer> customer;
    private Customer selectedCustomer;

    /** Creates a new instance of customerBean */
    public customerBean() {
        customer = new ArrayList<Customer>();        
    }

    public List<Customer> getCustomer() {
        CustomersDao cust_dao = new CustomersDao();
        customer = cust_dao.findAll();
        return customer;
    }

    public Customer getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(Customer selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }
}

【问题讨论】:

  • 首先,考虑有两种形式,一种用于表格本身,另一种用于对话框。完成后,请提供有关您的问题的更多详细信息。 customerBean#setSelectedCustomer 是否被正确调用? doesn't working 对你来说是什么,对话框根本没有显示?
  • @XtremeBiker 是的,兄弟,绑定值工作正常,但没有显示对话框。但 Makky 说我的代码运行良好。我花了 3 天的时间,但我无法弄清楚我的代码有什么问题。

标签: java jsf primefaces datatable


【解决方案1】:

测试了您的代码后,可以确认绝对没有任何问题(即使您可以使用单独的表单对其进行改进)。在这里你有适合我的SSCCE,你自己试试看(你应该尽量减少问题,你最好从能正常工作的东西开始,并根据你的具体情况进行调整)。

@ManagedBean
@ViewScoped
public class CustomerBean implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -6479501676353748761L;

    private Customer selectedCustomer;

    private List<Customer> customers = Arrays.asList(new Customer("Andy",
            "Brown", "A", "abrown@email.com"), new Customer("George", "Walter",
            "B", "gwalter@email.com"));

    public Customer getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(Customer selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }

    public List<Customer> getCustomers() {
        return customers;
    }

    public class Customer {

        public Customer(String firstName, String lastName, String dob,
                String email) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.dob = dob;
            this.email = email;
        }

        private String firstName;

        private String lastName;

        private String dob;

        private String email;

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public String getDob() {
            return dob;
        }

        public void setDob(String dob) {
            this.dob = dob;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }
    }

}
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<h:head />

<h:body>

    <p:growl id="msgs" showDetail="true" />
    <h:form>
        <p:dataTable id="customers" var="customer"
            value="#{customerBean.customers}">

            <p:column headerText="Model" style="width:24%">
                <h:outputText value="#{customer.firstName}" />
            </p:column>

            <p:column headerText="Year" style="width:24%">
                <h:outputText value="#{customer.lastName}" />
            </p:column>

            <p:column headerText="Manufacturer" style="width:24%">
                <h:outputText value="#{customer.dob}" />
            </p:column>

            <p:column headerText="Color" style="width:24%">
                <h:outputText value="#{customer.email}" />
            </p:column>

            <p:column style="width:4%">
                <p:commandButton id="selectButton" oncomplete="carDialog.show()"
                    icon="ui-icon-search" title="View" update=":carDlg">
                    <f:setPropertyActionListener value="#{customer}"
                        target="#{customerBean.selectedCustomer}" />
                </p:commandButton>
            </p:column>

        </p:dataTable>
    </h:form>

    <p:dialog header="Car Detail" widgetVar="carDialog" resizable="false"
        id="carDlg" showEffect="fade" hideEffect="explode">
        <h:form id="dialog_form">

            <h:panelGrid id="display" columns="2" cellpadding="4"
                style="margin:0 auto;">

                <h:outputText value="Model:" />
                <h:outputText value="#{customerBean.selectedCustomer.firstName}"
                    style="font-weight:bold" />

                <h:outputText value="Year:" />
                <h:outputText value="#{customerBean.selectedCustomer.lastName}"
                    style="font-weight:bold" />


                <h:outputText value="Manufacturer:" />
                <h:outputText value="#{customerBean.selectedCustomer.dob}"
                    style="font-weight:bold" />

                <h:outputText value="Color:" />
                <h:outputText value="#{customerBean.selectedCustomer.email}"
                    style="font-weight:bold" />

            </h:panelGrid>

        </h:form>
    </p:dialog>

</h:body>

</html>

【讨论】:

  • 只需将其复制粘贴到您的项目中,看看它是否有效,然后根据您的工作进行调整。
  • 您的代码运行良好,我刚刚实现了 Serializable 并更改了查看范围,但它仍然无法正常工作。需要我的项目来检查吗?
  • 是的,效果很好。我的代码只有绑定值有效。我可以把我的项目发给你看看吗?
  • 没有时间配置/启动/检查您的项目。但是,由于我发布的代码与您的非常相似,您为什么不使用我的代码来开始实现您的功能呢?只需使用我的作为基础并添加您需要的所有内容,而不是其他方式。
【解决方案2】:

尝试更改变量名称var="customer" value="#{customerBean.customer}"

【讨论】:

    【解决方案3】:

    我尝试了您的代码并且它有效。 DebugFireBug 看看有没有错误。

    【讨论】:

    • 真的吗?但我看不到对话框显示。我真的不知道为什么?
    • chat.stackoverflow.com/rooms/42652/myvbhelp
    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 2013-05-05
    • 2018-03-04
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    相关资源
    最近更新 更多