我有一个带有 PrimeFaces 的 JSF 项目。
我的项目中有一个“仪表板”,其中包含一个带有 p:datatable 的小 p:panel。我的表中的数据由我的 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 查询并填充一个列表,该列表填充数据表,没有什么复杂的。
我将如何有效地制作或替换输出文本,以便它可以单击并打开一个对话框,该对话框将根据我单击的值填充数据。
我认为我可能遇到的问题是该列中的值是一个名称,而不是我的数据库中的 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>
托管 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;
}
}
人员类别
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;
}
}
输出
关于java - JSF/Primefaces : Open dialogue by click on datatable entry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20931513/