当前位置: 首页 > 知识库问答 >
问题:

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

翟兴邦
2023-03-14

我正在处理一个项目,该项目要求集合中的每个项都有一个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;
    }

}

文件文件。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);
    }

}

测试控制器。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

有谁能帮我找到解决方案或替代方案吗?

谢谢大家!

共有1个答案

吴浩博
2023-03-14

p的值:列不能引用父数据表的var。文档http://www.primefaces.org/showcase/ui/data/datatable/columns.xhtml显示了这个时刻。实际上,在您的模型中,每一行可以包含不同数量的列。您需要更改模型:

表模型:

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

行模型:

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

柱状模型:

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>
 类似资料:
  • 问题内容: 我正在尝试占用和div未使用的任何空间。为此,我将flex显示设置为列方向。然后,我将属性设置为1。似乎没有做任何事情。 注意:不确定这是否重要,但是我正在进行一些嵌套的flex显示。 HTML CSS 问题答案: 您代码中的所有内容都可以正常工作。 唯一的问题是 您的flex容器没有指定的高度 。因此,高度解析为,表示内容的高度。 该属性在容器中分配 可用 空间。容器中没有可用空间,

  • 问题内容: 我有一个字符串: 如果我使用function,那么它将返回相同的字符串,但我想获取。 问题出在哪里? 问题答案: 代码 正确打印“ ICECREAM”。但是,原始字符串c不变。Java中的字符串是不可变的,因此对该字符串的所有操作都会返回一个新副本。

  • 我对硒有问题!我试了3.6和2.7,但没有区别!我用的是win7(64位) 文件“C:\python27\lib\site-packages\selenium\webdriver\common\service.py”,第81行,在start os.path.basename(self.path),self.start_error_message中 WebDriverException:消息:“Ge

  • 问题内容: 有两个函数hello1()和hello2()。 在中,延迟3秒后打印“ hello1”。 但是在中,它会立即打印“ hello2”。 我认为是因为它必须在setTimeout中使用函数名称。 如果我想在延迟3秒后执行带有参数的函数怎么办? 因为我想将参数传递给函数,所以我不能只在setTimeout中使用函数名,例如 问题答案: 当对函数使用括号时,将立即执行。 要将函数与参数一起使用

  • 我正在使用DocuSign C#SDK创建一个JWT令牌,以使用集成商密钥冒充用户,但它会抛出一个错误,说: 请求服务器时出错,收到一个不成功的HTTP代码 我只是调用方法,如下所示: 中传递的所有参数的值为: 我已通过使用此终结点获得用户的同意: https://account-d.docusign.com/oauth/auth?响应类型=令牌 并且用户已成功允许此clientId/integr

  • 执行时出现错误,程序崩溃。 null 那有什么不对?