简体   繁体   中英

JSF/Primefaces: Open dialogue by click on datatable entry

I have JSF project with PrimeFaces.

I have a 'dashboard' in my project that contains a small p:panel with ap:datatable. The data in my table gets updated dynamically by my beans. I want to be able to click on one of the labels to open up a dialogue with more data.

This is what a typical column looks like:

<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>

Then I would have another column for something like totals or time, it depends.

In my bean I have a sql query running and populating a list which populates the datatable,nothing intricate.

How would I go about - efficiently - making or replacing the outputText so it's clickable and opening a dialogue which would be populated with data based on which value I clicked on.

The problem I think that I might encounter is that the value in that column is a name, and not an ID in my db (which I will need to get the rest of the data to populate the dialogue box)

Should I change the outputText to a link and have some sort of ajax call to open up the dialogue box and get the data?

You can use primefaces commandLink. Below is full working example

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

在页面加载点击名称时

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM