简体   繁体   English

p:ui:repeat内的列不起作用

[英]p:columns inside ui:repeat is not working

I'm working on a project that requires a dataTable for each item of a collection, and each of those dataTables has dynamic columns. 我正在一个项目中,该项目需要一个dataTable的集合的每个项目,并且每个dataTables都有动态列。

Here's an example of what I want to do.. 这是我想做的一个例子。

DocsDescriptor.java 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 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 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 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 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 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 这是输出: 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 我正在使用wildfly 9.0.1,primefaces-5.2

Can anybody help me out with a solution or an alternative? 有人可以帮我解决方案或替代方案吗?

Thank you all! 谢谢你们!

Value of p:columns cannot refer to var of parent dataTable. p:columns的值不能引用父dataTable的var。 Documentation http://www.primefaces.org/showcase/ui/data/datatable/columns.xhtml shows this moment. 文档http://www.primefaces.org/showcase/ui/data/datatable/columns.xhtml显示了这一刻。 Actually in your model each row can contain different number of columns. 实际上,在模型中,每一行可以包含不同数量的列。 You need to change model: 您需要更改模型:

TableModel: 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: 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: 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;
    }
}

Modify TestController: 修改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;
    }
}

And view: 并查看:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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