如何根据<p:selectonebutton>动态更改<p:datatable>的值?</p:selectonebutton> </p:datatable>

时间:2012-09-27 14:14:30

标签: jsf primefaces

我有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>

2 个答案:

答案 0 :(得分:1)

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

E.g。

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

答案 1 :(得分:0)

p:selectOneButton添加valueChangeListener:

听众示例:

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" />