我正在开发一个项目,该项目需要为集合的每个项目提供dataTable,并且每个dataTable都有动态列。
以下是我想要做的一个例子..
DocsDescriptor.java
package test;
public class DocsDescriptor {
private long id;
private String name;
public DocsDescriptor(long id, String name){
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
DocsData.java
package test;
public class DocsData {
private long id;
private String value;
private DocsDescriptor descriptor;
public DocsData(long id, String value, DocsDescriptor descriptor){
this.id = id;
this.value = value;
this.descriptor = descriptor;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public DocsDescriptor getDescriptor() {
return descriptor;
}
public void setDescriptor(DocsDescriptor descriptor) {
this.descriptor = descriptor;
}
}
DocsDocument.java
package test;
import java.util.ArrayList;
import java.util.List;
public class DocsDocument {
private long id;
private List<DocsData> datas;
public DocsDocument(long id){
this.id = id;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public List<DocsData> getDatas() {
return datas;
}
public void setDatas(List<DocsData> datas) {
this.datas = datas;
}
public void add(DocsData data){
if(this.datas == null) this.datas = new ArrayList<DocsData>();
this.datas.add(data);
}
}
DocsDocumentType.java
package test;
import java.util.ArrayList;
import java.util.List;
public class DocsDocumentType {
private long id;
private String name;
private List<DocsDocument> documents;
public DocsDocumentType(long id, String name){
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<DocsDocument> getDocuments() {
return documents;
}
public void setDocuments(List<DocsDocument> documents) {
this.documents = documents;
}
public void add(DocsDocument document){
if(this.documents == null) this.documents = new ArrayList<DocsDocument>();
this.documents.add(document);
}
}
TestController.java
package test;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
@ConversationScoped
@Named("test")
public class TestController implements Serializable{
private static final long serialVersionUID = 2433550537340132027L;
@Inject
protected Conversation conversation;
private List<DocsDocumentType> documentTypes;
public void start(){
if(this.conversation.isTransient())
this.conversation.begin();
if(this.conversation != null)
this.conversation.setTimeout(10800000);
}
@PostConstruct
public void init(){
DocsDescriptor dePhone = new DocsDescriptor(1,"Number");
DocsDescriptor deName = new DocsDescriptor(2,"Name");
DocsDescriptor deLastName = new DocsDescriptor(3,"Last Name");
DocsDescriptor dePrice = new DocsDescriptor(4,"Product Price");
DocsDescriptor deCode = new DocsDescriptor(5,"Product Code");
DocsDescriptor deProdName = new DocsDescriptor(6,"Product Name");
DocsDocument jl = new DocsDocument(1);
jl.add(new DocsData(1,"514237797", dePhone));
jl.add(new DocsData(2,"John", deName));
jl.add(new DocsData(3,"Lennon", deLastName));
DocsDocument pm = new DocsDocument(2);
pm.add(new DocsData(4,"45312342", dePhone));
pm.add(new DocsData(5,"Paul", deName));
pm.add(new DocsData(6,"McCartney", deLastName));
DocsDocument rs = new DocsDocument(3);
rs.add(new DocsData(7,"567523534", dePhone));
rs.add(new DocsData(8,"Richard", deName));
rs.add(new DocsData(9,"Starkey", deLastName));
DocsDocument gh = new DocsDocument(3);
gh.add(new DocsData(10,"454623243", dePhone));
gh.add(new DocsData(11,"George", deName));
gh.add(new DocsData(12,"Harrison", deLastName));
DocsDocumentType identity = new DocsDocumentType(1,"Beatles");
identity.add(jl);
identity.add(pm);
identity.add(gh);
identity.add(rs);
DocsDocument iPhone = new DocsDocument(4);
iPhone.add( new DocsData(13,"iPhone 6S",deProdName));
iPhone.add( new DocsData(15,"23452340",deCode));
iPhone.add( new DocsData(16,"$650",dePrice));
DocsDocument nexus = new DocsDocument(5);
nexus.add( new DocsData(13,"Nexus 6P",deProdName));
nexus.add( new DocsData(15,"786338675",deCode));
nexus.add( new DocsData(16,"$600",dePrice));
DocsDocumentType product = new DocsDocumentType(1,"Product");
product.add(iPhone);
product.add(nexus);
this.documentTypes = new ArrayList<DocsDocumentType>();
this.documentTypes.add(identity);
this.documentTypes.add(product);
}
public List<DocsDocumentType> getDocumentTypes() {
return documentTypes;
}
public void setDocumentTypes(List<DocsDocumentType> documentTypes) {
this.documentTypes = documentTypes;
}
}
test.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>TEST</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</h:head>
<h:body>
<f:event type="preRenderView" listener="#{test.start}"/>
<h:panelGroup id="bigArea" style="width : 100%">
<ui:repeat value="#{test.documentTypes}" var="dt">
<p:panelGrid style="width : 750px">
<f:facet name="header">
<p:row>
<p:column style="width : 750px; text-align: left"><h:outputText value="#{dt.name}" /></p:column>
</p:row>
</f:facet>
<p:row>
<p:column>
<p:dataTable value="#{dt.documents}" var="doc" emptyMessage="...">
<p:columns value="#{doc.datas}" var="data">
<f:facet name="header"><h:outputText value="#{data.descriptor.name}"/></f:facet>
<h:outputText value="#{data.value}" />
</p:columns>
</p:dataTable>
</p:column>
</p:row>
</p:panelGrid>
</ui:repeat>
</h:panelGroup>
</h:body>
</html>
这是输出: http://picpaste.com/Captura_de_pantalla_2015-12-12_a_las_19.11.27_1-0Fd7lEtY.png
我正在使用wildfly 9.0.1,primefaces-5.2
任何人都可以帮我解决问题或替代方案吗?
谢谢大家!
答案 0 :(得分:0)
p:列的值不能引用父dataTable的var。文档http://www.primefaces.org/showcase/ui/data/datatable/columns.xhtml显示了这一时刻。实际上在您的模型中,每行可以包含不同数量的列。你需要改变模型:
的TableModel:
package test;
import java.util.ArrayList;
import java.util.List;
/**
* Created by kit on 14.12.2015.
*
* @author kit
*/
public class TableModel {
private String name;
private List<RowModel> rows = new ArrayList<>();
private List<ColumnModel> columns = new ArrayList<>();
/**
* Getters, Setters
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<RowModel> getRows() {
return rows;
}
public List<ColumnModel> getColumns() {
return columns;
}
}
RowModel:
package test;
import java.util.HashMap;
import java.util.Map;
/**
* Created by kit on 14.12.2015.
*
* @author kit
*/
public class RowModel<T> {
private String name;
private Map<ColumnModel, T> data = new HashMap<>();
/**
* Getters, Setters
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<ColumnModel, T> getData() {
return data;
}
}
ColumnModel:
package test;
/**
* Created by kit on 14.12.2015.
*
* @author kit
*/
public class ColumnModel<T> {
private T data;
/**
* Getters, Setters
*/
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
修改TestController:
package test;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@ConversationScoped
@Named("test")
public class TestController implements Serializable {
private static final long serialVersionUID = 2433550537340132027L;
@Inject
protected Conversation conversation;
private List<TableModel> tables = new ArrayList<>();
private List<DocsDocumentType> documentTypes;
public void start() {
if (this.conversation.isTransient())
this.conversation.begin();
if (this.conversation != null)
this.conversation.setTimeout(10800000);
}
@PostConstruct
public void init() {
DocsDescriptor dePhone = new DocsDescriptor(1, "Number");
DocsDescriptor deName = new DocsDescriptor(2, "Name");
DocsDescriptor deLastName = new DocsDescriptor(3, "Last Name");
DocsDescriptor dePrice = new DocsDescriptor(4, "Product Price");
DocsDescriptor deCode = new DocsDescriptor(5, "Product Code");
DocsDescriptor deProdName = new DocsDescriptor(6, "Product Name");
DocsDocument jl = new DocsDocument(1);
jl.add(new DocsData(1, "514237797", dePhone));
jl.add(new DocsData(2, "John", deName));
jl.add(new DocsData(3, "Lennon", deLastName));
DocsDocument pm = new DocsDocument(2);
pm.add(new DocsData(4, "45312342", dePhone));
pm.add(new DocsData(5, "Paul", deName));
pm.add(new DocsData(6, "McCartney", deLastName));
DocsDocument rs = new DocsDocument(3);
rs.add(new DocsData(7, "567523534", dePhone));
rs.add(new DocsData(8, "Richard", deName));
rs.add(new DocsData(9, "Starkey", deLastName));
DocsDocument gh = new DocsDocument(3);
gh.add(new DocsData(10, "454623243", dePhone));
gh.add(new DocsData(11, "George", deName));
gh.add(new DocsData(12, "Harrison", deLastName));
DocsDocumentType identity = new DocsDocumentType(1, "Beatles");
identity.add(jl);
identity.add(pm);
identity.add(gh);
identity.add(rs);
DocsDocument iPhone = new DocsDocument(4);
iPhone.add(new DocsData(13, "iPhone 6S", deProdName));
iPhone.add(new DocsData(15, "23452340", deCode));
iPhone.add(new DocsData(16, "$650", dePrice));
DocsDocument nexus = new DocsDocument(5);
nexus.add(new DocsData(13, "Nexus 6P", deProdName));
nexus.add(new DocsData(15, "786338675", deCode));
nexus.add(new DocsData(16, "$600", dePrice));
DocsDocumentType product = new DocsDocumentType(1, "Product");
product.add(iPhone);
product.add(nexus);
this.documentTypes = new ArrayList<DocsDocumentType>();
this.documentTypes.add(identity);
this.documentTypes.add(product);
// Populating tableModel
for (DocsDocumentType docsDocumentType : documentTypes) {
TableModel tableModel = new TableModel();
tableModel.setName(docsDocumentType.getName());
for (DocsDocument docsDocument : docsDocumentType.getDocuments()) {
RowModel<String> rowModel = new RowModel<>();
rowModel.setName(String.valueOf(docsDocument.getId()));
tableModel.getRows().add(rowModel);
for (DocsData docsData : docsDocument.getDatas()) {
ColumnModel<DocsDescriptor> columnModel = findColumn(tableModel, docsData.getDescriptor());
if (columnModel == null) {
columnModel = new ColumnModel<>();
columnModel.setData(docsData.getDescriptor());
tableModel.getColumns().add(columnModel);
}
rowModel.getData().put(columnModel, docsData.getValue());
}
}
tables.add(tableModel);
}
}
private ColumnModel findColumn(TableModel tableModel, DocsDescriptor descriptor) {
for (ColumnModel columnModel : tableModel.getColumns()) {
if (descriptor.equals(columnModel.getData())) {
return columnModel;
}
}
return null;
}
public List<TableModel> getTables() {
return tables;
}
}
并查看:
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>TEST</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</h:head>
<h:body>
<f:event type="preRenderView" listener="#{test.start}"/>
<h:panelGroup id="bigArea" style="width : 100%">
<ui:repeat value="#{test.tables}" var="dt">
<p:panelGrid style="width : 750px">
<f:facet name="header">
<p:row>
<p:column style="width : 750px; text-align: left"><h:outputText value="#{dt.name}" /></p:column>
</p:row>
</f:facet>
<p:row>
<p:column>
<p:dataTable value="#{dt.rows}" var="row" emptyMessage="...">
<p:columns value="#{dt.columns}" var="col">
<f:facet name="header"><h:outputText value="#{col.data.name}"/></f:facet>
<h:outputText value="#{row.data[col]}" />
</p:columns>
</p:dataTable>
</p:column>
</p:row>
</p:panelGrid>
</ui:repeat>
</h:panelGroup>
</h:body>
</html>