具有多个动态列的嵌套DataTable为每行

时间:2017-03-02 07:54:38

标签: jsf primefaces

我想显示一些数据,我需要使用两个嵌套的DataTables进行迭代(使用Mojarra 2.2.13和Primefaces 6.0)。内表具有动态数量的列。由于我需要为列迭代的对象是两个不同的业务对象,我需要两个单独的<p:columns/>标记。

BackingBean.java

@Named
@ViewScoped
public class BackingBean implements Serializable {
    private static final long serialVersionUID = 1L;

    private List<Family> families;
    private List<String> countries;

    public void generateFamilies() {
        this.families = new ArrayList<Family>();

        Family f = new Family("Beta");
        f.getCars().add("Mercedes");
        f.getCars().add("Porsche");
        f.getCars().add("VW");
        f.getCars().add("Dodge");
        f.getPersons().add("he");
        f.getPersons().add("she");
        families.add(f);

        f = new Family("Alpha");
        f.getCars().add("BMW");
        f.getPersons().add("he");
        f.getPersons().add("she");
        families.add(f);
    }

    private void generateCountries() {
        this.countries = new ArrayList<String>();
        this.countries.add("Germany");
        this.countries.add("England");
        this.countries.add("Spain");
    }

    public List<Family> getFamilies() {
        if (null == families) {
            generateFamilies();
        }
        return families;
    }

    public List<String> getCountries() {
        if (null == countries) {
            generateCountries();
        }
        return countries;
    }

    public class Family {
        private String name;
        private List<String> persons;
        private List<String> cars;

        public Family(String name) {
            this.name = name;
            this.persons = new ArrayList<String>();
            this.cars = new ArrayList<String>();
        }

        public String getName() {
            return name;
        }

        public List<String> getPersons() {
            return persons;
        }

        public List<String> getCars() {
            return cars;
        }
    }

}

index.xthml

<?xml version="1.0" encoding="UTF-8"?>
<!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:p="http://primefaces.org/ui"
    xmlns:c="http://java.sun.com/jsp/jstl/core">

<h:head>
    <title>DataTable BUG</title>
</h:head>

<body>
    <h:form id="form">
        <p:dataTable id="families" value="#{backingBean.families}" var="family">
            <p:column headerText="Families">
                <h3>#{family.name}</h3>
                <p:dataTable id="persons" value="#{family.persons}" var="person">
                    <p:column headerText="Name">
                        Does #{person} ...
                    </p:column>
                    <p:columns value="#{family.cars}" var="car" headerText="#{car}">
                        ... have insurance for the #{car}? 
                    </p:columns>
                    <p:columns value="#{backingBean.countries}" var="country" headerText="#{country}">
                        ... have insurance for #{country}?
                    </p:columns>
                </p:dataTable>
            </p:column>    
       </p:dataTable>
    </h:form>   
</body>
</html>

我得到的结果是所有内部表(id =人)看起来都一样(见图1)。第一个内表指示列的数量(本例中为8)。外表的第二行(id =系列)应该只显示内表的5列(仅列为BMW列)。但仍显示第一行中的所有列。如果我删除一个p:columns,一切都按预期工作(见图2)。

我查找了p:dataTable的文档,似乎没有任何限制,每个p:columns只允许一个p:dataTable。我错过了什么吗?

Two dynamic column blocks One dynamic column block

1 个答案:

答案 0 :(得分:0)

你是对的,我可以重现这个场景。也许它不是PrimeFaces错误或多个p:列。作为一种解决方法,您可以使用c:forEach作为国家/地区列表:

  <c:forEach items="#{backingBean.countries}" var="country">
     <p:column headerText="#{country}">
        ... have insurance for #{country}
     </p:column>
  </c:forEach>