I'm working on a project that requires a dataTable for each item of a collection, and each of those dataTables has dynamic columns.
Here's an example of what I want to do..
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>
And here's the output: http://picpaste.com/Captura_de_pantalla_2015-12-12_a_las_19.11.27_1-0Fd7lEtY.png
I'm using wildfly 9.0.1, primefaces-5.2
Can anybody help me out with a solution or an alternative?
Thank you all!