[英]JSF/Primefaces: Open dialogue by click on datatable entry
我有PrimeFaces的JSF項目。
我的項目中有一個“儀表板”,其中包含一個帶有ap: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調用以打開對話框並獲取數據嗎?
您可以使用primefaces commandLink。 以下是完整的工作示例
XHTML檔案
<!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>
托管豆
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;
}
}
人類
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;
}
}
產量
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.