【问题标题】:Populate table with nested Collections使用嵌套集合填充表
【发布时间】:2014-10-31 10:02:39
【问题描述】:

我正在为我的问题寻找解决方案。

我有一张桌子需要这样。

--------------------------------------------------------------|
        |       |                   Title 3                   |
Title 1 |Title 2|---------------------------------------------|
        |       | SubTitle 1 |SubTitle 2|SubTitle 3|SubTitle 4|
--------------------------------------------------------------|
        |       |            |          |          |          |
Val A1  | ValB1 |    ValC1   |   ValC1  |   ValC1  |  ValC1   |
        |       |            |          |          |          |
        |-------|------------|----------|----------|----------|
        |       |            |          |          |          |
        | ValB2 |    ValC2   |   ValC2  |   ValC2  |  ValC2   |
        |       |            |          |          |          |
--------------------------------------------------------------|
        |       |            |          |          |          |
        |       |            |          |          |          |
Val A2  | ValB3 |    ValC3   |   ValC3  |   ValC3  |  ValC3   |
        |       |            |          |          |          |
        |       |            |          |          |          |
----------------------------------------------------------------

我的目标是让表格看起来像我上面所做的那样。

我正在使用 JSF 和 Primefaces 4.0。

我有这种班级结构。

Class Master {

  private List<ClassA> classesA

  // getter and setter

}


Class A {

      private List<ClassB> classesB

      // getter and setter and fields

}


Class B {

      private List<ClassC> classesC

      // getter and setter and fields

}


Class C {

  // getter and setter and fields

}

所以我尝试使用两个datatables 或使用ui:repeat,但我不知道什么是最好的选择。并且拥有的头衔是这样的。

Class A 有title 1,Class B 有title 2 和Class C 有title 3 和subtitle 1,2,3 and 4.

或使用binding。

为了更容易理解,我放了一张关于如何创建表格的图片。

有人可以帮我吗?

谢谢

【问题讨论】:

    标签: jsf jsf-2 primefaces datatable


    【解决方案1】:

    您可以将 dataTable 与 subTable 一起使用(如果需要,您可以在行中添加列)。

    【讨论】:

    • 你有具体的例子吗?
    • 我有自己的 RichFaces 代码和类似的表格。这是 PrimeFaces 中的简单 subtable example 或 nested tables。
    • 我的问题是,使用内部list查看它。
    【解决方案2】:

    如果您确定对象结构将来不会改变,那么简单的解决方案是使用 ui:repeat 嵌套表、行跨度和列跨度。但是如果对象结构经常改变并且你需要做不止一次然后去自定义组件。如果你需要 olap 功能那么我建议你去 pivot4j 或类似的东西。

    更新:查看下面的 xhtml,它创建了表格的主体。

    <table border="4" cellspacing="4" cellpadding="10">
            <tr>
                <th rowspan="2">A</th>
                <th rowspan="2">B</th>
                <th colspan="4">C</th>
            </tr>
            <tr>
                <th>p1</th>
                <th>p2</th>
                <th>p3</th>
                <th>p4</th>
            </tr>
            <ui:repeat var="classa" value="#{master.classesA}" varStatus="astat">
                <ui:repeat var="classb" value="#{classa.classesB}"  varStatus="bstat">
                    <ui:repeat value="#{classb.classesC}" var="classc" varStatus="cstat">
    
                        <tr>
                            <h:panelGroup rendered="#{bstat.index eq 0 and cstat.index eq 0}">
                                <td rowspan="#{classa.colSpan}" >
                                    <h:outputText value="#{classa.property}" />
                                </td>
                            </h:panelGroup>
                            <h:panelGroup rendered="#{cstat.index eq 0}">
                                <td rowspan="#{classb.classesC.size()}">                                                   
                                    <h:outputText value="#{classb.property}" />
                                </td>
                            </h:panelGroup>
    
                            <td>
                                <h:outputText value="#{classc.prop1}" />
                            </td>
                            <td>
                                <h:outputText value="#{classc.prop2}" />
                            </td>
                            <td>
                                <h:outputText value="#{classc.prop3}" />
                            </td>
                            <td>
                                <h:outputText value="#{classc.prop4}" />
                            </td>
                        </tr>  
                    </ui:repeat>
    
                </ui:repeat>
    
            </ui:repeat>
        </table>
    

    类应该是这样的

     public class A {
        private List<B> classesB;
        private String property;
    
        public List<B> getClassesB() {
            return classesB;
        }
    
        public void setClassesB(List<B> classesB) {
            this.classesB = classesB;
        }
    
        public A(String pro) {
        classesB= new ArrayList<B>();
        property=pro;
        }
    
        public void addClassB(B b){
            classesB.add(b);
        }
    
        public String getProperty() {
            return property;
        }
    
        public void setProperty(String property) {
            this.property = property;
        }
    
        public int getColSpan(){
            int size=0;
            for(B b : classesB){
                size+=b.getClassesC().size();
            }
            return size;
        }
    
    }
    
    public class B {
        private List<C> classesC;
        private String property;
    
        public B(String pro) {
            this.classesC = new ArrayList<C>();
            this.property=pro;
        }
    
    
    
        public List<C> getClassesC() {
            return classesC;
        }
    
        public void setClassesC(List<C> classesC) {
            this.classesC = classesC;
        }
    
    
        public void addClassC(C c){
            classesC.add(c);
        }
    
        public String getProperty() {
            return property;
        }
    
        public void setProperty(String property) {
            this.property = property;
        }
    
    
    }
    public class C {
        private String prop1;
        private String prop2;
        private String prop3;
        private String prop4;
    
        public C(String prop1, String prop2, String prop3, String prop4) {
            this.prop1 = prop1;
            this.prop2 = prop2;
            this.prop3 = prop3;
            this.prop4 = prop4;
        }
    
        public void setProp1(String prop1) {
            this.prop1 = prop1;
        }
    
        public void setProp2(String prop2) {
            this.prop2 = prop2;
        }
    
        public void setProp3(String prop3) {
            this.prop3 = prop3;
        }
    
        public void setProp4(String prop4) {
            this.prop4 = prop4;
        }
    
        public String getProp1() {
            return prop1;
        }
    
        public String getProp2() {
            return prop2;
        }
    
        public String getProp3() {
            return prop3;
        }
    
        public String getProp4() {
            return prop4;
        }
    
    
    }
    

    大师班的测试数据。

    private List<A> classesA;
    
    @PostConstruct
    public void init() {
        classesA = new ArrayList<A>();
        A a1 = new A("a1");
        A a2 = new A("a2");
        classesA.add(a1);
        classesA.add(a2);
    
        B b11 = new B("b11");
        B b12 = new B("b12");
        B b21 = new B("b21");
    
        a1.addClassB(b11);
        a1.addClassB(b12);
        a2.addClassB(b21);
    
        C c111 = new C("a", "b", "c", "d");
        C c112 = new C("d", "e", "f", "g");
        C c121 = new C("g", "h", "i", "j");
        C c211 = new C("k", "l", "m", "n");
        C c212 = new C("o", "p", null, null);
    
        b11.addClassC(c111);
        b11.addClassC(c112);
        b12.addClassC(c121);
        b21.addClassC(c211);
        b21.addClassC(c212);
    }
    
    public List<A> getClassesA() {
        return classesA;
    }
    
    public void setClassesA(List<A> classesA) {
        this.classesA = classesA;
    }
    

    【讨论】:

    • 感谢您的回答。但我不知道该怎么做,我尝试使用 ui:repeat 但现在不要为 de title 2 和 title 3 生成 de 行,因为 title 2 中的一个 row 可以有很多 @987654328 @在title 3。能给我举个例子吗?
    • 我放个图给你看懂
    • @user3503888 上面的代码创建了与图片中类似的输出。忽略每一件事,只关注如何使用 ui:repeat tr, td, td with rowspan 和 panelgroup。
    • 我测试了你的代码,但没有显示为表格。
    • 在 ui:repeat 上方添加 并在 ui:repaat 下方添加
      。还要加个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    相关资源
    最近更新 更多