【问题标题】:Displaying dataTable with two nested lists显示带有两个嵌套列表的 dataTable
【发布时间】:2017-10-16 17:52:18
【问题描述】:

我有一个List<List<String>>,我正在尝试在 Primefaces/JSF dataTable 中显示数据。列表如下所示:

[["1_1", "1_2", "1_3"], ["2_1", "2_2", "2_3"], ["3_1", "3_2", "3_3"]]

结果表需要如下所示:

1_1 | 1_2 | 1_3
________________
2_1 | 2_2 | 2_3
________________
3_1 | 3_2 | 3_3

可能我需要ui:repeat 之类的东西,但不幸的是我找不到解决方案。我是 JSF 和 primefaces 的新手,希望得到您的理解。

谁能帮忙?

【问题讨论】:

  • 为什么要使用这种不规则的数据结构?如果您将内部列表转换为对象并将该单个列表设置为 p:dataTable 的值,会容易得多。
  • 喜欢List<ObjectX>,其中包含x1, x2 & x3 属性以保存"1_1", "1_2", "1_3" 值。
  • 如果您的子列表包含不同数量的项目怎么办?

标签: java jsf primefaces jsf-2 datatable


【解决方案1】:

你可以试试这个解决方案,我同时使用了 html <table> 和 JSF <h:dataTable>

@ManagedBean
public class MyBean {
    private List<List<String>> list = new ArrayList<List<String>>();

    public MyBean(){
        for(int i=1; i <=3; i++){
            List<String> newList = new ArrayList<String>();
            for(int x=1; x <=3; x++){
                newList.add(i + "_" + x);
            }
            list.add(newList);
        }
    }
    public List<List<String>> getList(){
        return list;
    }
}

使用&lt;table&gt;&lt;ui:repeat&gt;

<table>
  <ui:repeat var="list" value="#{myBean.list}">
    <tr>
    <ui:repeat var="newlist" value="#{list}">
      <td>#{newlist}</td>
    </ui:repeat>
    </tr>
  </ui:repeat>
</table>

使用&lt;h:dataTable&gt;,列表中的每个List&lt;String&gt;必须具有相同的大小,否则您将得到IndexOutOfBoundsException

<h:dataTable value="#{myBean.list}" var="list">
  <h:column>
    #{list.get(0)}
  </h:column>
  <h:column>
    #{list.get(1)}
  </h:column>
  <h:column>
    #{list.get(2)}
  </h:column>
</h:dataTable>

【讨论】:

    猜你喜欢
    • 2013-05-10
    • 1970-01-01
    • 2022-11-15
    • 2010-09-15
    • 1970-01-01
    • 2018-02-10
    • 2012-10-25
    • 2017-11-25
    • 1970-01-01
    相关资源
    最近更新 更多