首页 文章

JSF / Primefaces:点击数据表条目打开对话

提问于
浏览
2

我有PrimeFaces的JSF项目 .

我的项目中有一个“仪表板”,其中包含一个带有p:datatable的小型p:面板 . 我的表中的数据由我的bean动态更新 . 我希望能够单击其中一个标签以打开包含更多数据的对话框 .

这就是典型的列:

<p:column  style="text-align: left">
    <f:facet name="header">
       <h:outputText value="Name"/>
    </f:facet>
    <h:outputText value="#{t.name}" style="text-align: left"/>
</p:column>

然后我会有另一个列,如总计或时间,这取决于 .

在我的bean中,我有一个运行的SQL查询并填充一个填充数据表的列表,没有任何错综复杂的内容 .

我将如何 - 有效地 - 制作或替换outputText,以便它可以点击并打开一个对话框,该对话框将填充基于我点击的值的数据 .

我认为我可能遇到的问题是该列中的值是一个名称,而不是我的数据库中的ID(我需要将其余数据填充到对话框中)

我应该将outputText更改为链接并进行某种ajax调用以打开对话框并获取数据吗?

1 回答

  • 7

    您可以使用primefaces commandLink . 以下是完整的工作示例

    XHTML file

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <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:ui="http://java.sun.com/jsf/facelets"
        xmlns:p="http://primefaces.org/ui">
    <h:head>
    </h:head>
    <h:body>
        <h:form id="form">
            <p:dataTable value="#{mbean.personList}" var="person">
                <p:column headerText="Name">
                    <p:commandLink value="#{person.name}" oncomplete="test.show()"
                        update=":form:dialog">
                        <f:setPropertyActionListener target="#{mbean.selectedPerson}"
                            value="#{person}" />
                    </p:commandLink>
                </p:column>
                <p:column headerText="Country">
                    #{person.country}
                </p:column>
            </p:dataTable>
            <p:dialog modal="true" width="500" height="500" widgetVar="test"
                id="dialog">
                Name : #{mbean.selectedPerson.name}
            </p:dialog>
        </h:form>
    </h:body>
    </html>
    

    Managed Bean

    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ViewScoped;
    
    @ManagedBean(name = "mbean")
    @ViewScoped
    public class TestBean implements Serializable {
    
        private List<Person> personList;
        private Person selectedPerson;
    
        public TestBean() {
            personList = new ArrayList<Person>();
            personList.add(new Person("AKN", "UK"));
            personList.add(new Person("AKF", "Australia"));
            personList.add(new Person("AKH", "Asia"));
    
        }
    
        public List<Person> getPersonList() {
            return personList;
        }
    
        public void setPersonList(List<Person> personList) {
            this.personList = personList;
        }
    
        public Person getSelectedPerson() {
            return selectedPerson;
        }
    
        public void setSelectedPerson(Person selectedPerson) {
            System.out.println("selected" + selectedPerson.getName());
            this.selectedPerson = selectedPerson;
        }
    
    }
    

    Person Class

    import java.io.Serializable;
    
    public class Person implements Serializable{
    
        private String name;
        private String country;
    
    
        public Person(String name, String country) {
            super();
            this.name = name;
            this.country = country;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getCountry() {
            return country;
        }
        public void setCountry(String country) {
            this.country = country;
        }
    
    }
    

    Output

    On page load

    When clicking on name

相关问题