1

我有 3 个不同类型的数据列表,我想<p:dataTable>根据<p:selectOneButton>按下的情况将它们单独显示,如下所示:

  • 如果用户点击“按钮 1”,我想显示list1<p:dataTable>
  • 如果用户点击“按钮 2”,我想显示list2<p:dataTable>

这是视图:

<p:selectOneButton value="#{managedBean.perspective}">
    <f:selectItem itemLabel="Button 1" itemValue="1" />
    <f:selectItem itemLabel="Button 2" itemValue="2" />
    <f:selectItem itemLabel="Button 3" itemValue="3" />
    <f:ajax event="change" render="table" />
</p:selectOneButton>

<p:dataTable id="table" value="#{managedBean.list}" var="object">
    <p:column headerText="title">
        ...
    </p:column>
</p:dataTable>

这是模型:

private List<Object1> list1 = new ArrayList<Object1>();
private List<Object2> list2 = new ArrayList<Object2>();
private List<Object3> list3 = new ArrayList<Object3>();

如何将相同的对象重复<p:dataTable>用于不同的对象列表?

4

2 回答 2

1

您需要有一个对象属性的集合,以便您可以使用它<p:columns>来动态生成列。

例如

<p:selectOneButton value="#{bean.type}">
    <f:selectItem itemLabel="Button 1" itemValue="1" />
    <f:selectItem itemLabel="Button 2" itemValue="2" />
    <f:selectItem itemLabel="Button 3" itemValue="3" />
    <p:ajax listener="#{bean.change}" update="table" />
</p:selectOneButton>

<p:dataTable id="table" value="#{bean.list}" var="object">
    <p:columns value="#{bean.properties}" var="property">
        #{object[property]}
    </p:columns>
</p:dataTable>

private static final List<String> OBJECT1_PROPERTIES = Arrays.asList("property1", "property2", "property3");
private static final List<String> OBJECT2_PROPERTIES = Arrays.asList("foo", "bar");
private static final List<String> OBJECT3_PROPERTIES = Arrays.asList("p1", "p2", "p3", "p4", "p5");

private List<Object1> list1 = new ArrayList<Object1>();
private List<Object2> list2 = new ArrayList<Object2>();
private List<Object3> list3 = new ArrayList<Object3>();

private String type;
private List<?> list;
private List<String> properties;

public void change() {
    if ("1".equals(type)) {
        list = list1;
        properties = OBJECT1_PROPERTIES;
    }
    // ...
}
于 2012-09-28T11:35:50.603 回答
0

添加 valueChangeListener 为p:selectOneButton

监听器示例:

public void yourListener(ValueChangeEvent event) {
    String newValue = (String) event.getNewValue();

    if ("1".equals(newValue){
        list = list1;
    } else if ("2".equals(newValue){
        list = list2;
    }else if ("3".equals(newValue){
        list = list3;
    }
}

并设置为p:selectOneButton

   <f:ajax update="table" />
于 2012-09-28T09:52:20.420 回答