2012-02-16 88 views
0

参考primefaces论坛上的this文章。
有人知道我们怎么能在同一页上使用多个数据表,但只显示正确的一个?
我的问题是,我有一个view-scoped bean,其属性包含来自数据库不同表中的数据。我有多个数据表用于每个数据库表的数据。现在我想显示从<p:selectOneMenu>(红色包围)中选择的基准值的数据表。
这个截图会进一步解释一下。从一组数据表中显示正确的数据表

image http://s19.postimage.org/qxugwevxf/image.jpg

+1

什么是正确的数据表?条件呈现在jsf中是标准的。 – 2012-02-16 21:10:46

+0

对不起。我是JSF的新手,那就是为什么不知道如何改变所选数据表的可见性。 – 2012-02-16 21:30:30

+0

顺便说一下,我已更新我的问题 – 2012-02-16 22:01:12

回答

2

的基本方法是让表的rendered属性取决于菜单选择的项目。

<p:selectOneMenu value="#{bean.table}"> 
    <f:selectItem itemValue="players" itemLabel="Players" /> 
    <f:selectItem itemValue="jobs" itemLabel="Jobs" /> 
    <f:selectItem itemValue="business" itemLabel="Business" /> 
    ... 
    <p:ajax update="tables" /> 
</p:selectOneMenu> 

<h:panelGroup id="tables"> 
    <p:dataTable value="#{bean.players}" rendered="#{bean.table == 'players'}"> 
     ... 
    </p:dataTable> 
    <p:dataTable value="#{bean.jobs}" rendered="#{bean.table == 'jobs'}"> 
     ... 
    </p:dataTable> 
    <p:dataTable value="#{bean.business}" rendered="#{bean.table == 'business'}"> 
     ... 
    </p:dataTable> 
    ... 
</h:panelGroup> 

这很容易实现,但你最终在视图(这当然可以在<ui:include>文件被分割)大量的代码。更高级和可重复使用的方法是让单个表的value依赖于菜单的选定项并使用<p:columns>动态生成列。

<p:selectOneMenu value="#{bean.table}"> 
    <f:selectItems value="#{bean.tables}" /> 
    <p:ajax listener="#{bean.changeModel}" update="table" /> 
</p:selectOneMenu> 

<p:dataTable id="table" value="#{bean.model}" var="item"> 
    <p:columns value="#{bean.columns}" var="column"> 
     <h:outputText value="#{item[column]}" /> 
    </p:columns> 
</p:dataTable> 

的东西,如:

public void changeModel() { 
    model = populateModelBasedOn(table); 
    columns = populateColumnsBasedOn(table); 
} 

这仅允许每当你想添加更多的专门列少细粒度控制。您可能想要使用标记文件。

+0

感谢您的回复。我的实际意图是完成你所提到的一切。请告诉我哪种方法更有用?标记文件,复合组件或基于选定值更改表模型? – 2012-02-17 05:05:34

+0

在我的问题中,我在primefaces论坛上提到了一个[Post](http://forum.primefaces.org/viewtopic.php?f=3&t=18365&p=57049#p56921),其中有人对我说: “或者你使用很多数据表,但是只有一个渲染=”#{myBean.condition}“才能显示正确的数据表=) 用ui:include + ui:params混合它,你只有一个代码用于同一个数据表! “请告诉我,我怎么能做到这一点? – 2012-02-17 05:10:46

0

由上帝的恩典。经过很多的斗争!我终于实现了this
为此,我向BalusC支付专业技巧的谢谢
因此,我想与所有人分享我的解决方案。
所以这是我在XHTML文件所做的:

<p:selectOneMenu value="#{dbmBean.selectedTable}" style="height:27px" > 
    <c:forEach items="#{dbmBean.tableNames}" var="table"> 
     <f:selectItem itemLabel="#{table.value}" itemValue="#{table.key}"/> 
    </c:forEach> 
</p:selectOneMenu> 
<p:commandButton value="Go" action="#{dbmBean.goToTable}" ajax="false" /> 
... 
<p:dataTable binding="#{dbmBean.table}" var="row" rowIndexVar="index"> 
<f:facet name="header"/> 
<p:columns value="#{dbmBean.columns}" var="column" columnIndexVar="colIndex" > 
     <f:facet name="header"> 
      #{column.header} 
     </f:facet> 
     <h:outputText value="#{row[column.property]}"/> 
    </p:columns> 
</p:dataTable> 

,并在支持bean:

public class DatabaseManagerBean implements Serializable { 
    private List<ColumnModel> columns; // Column model is a simple class with two string properties: header, property 
    ...  
    public void goToTable() { 
     int tableIndex = new Integer(this.selectedTable); 
     switch (tableIndex) { 
      case 1: 
       Players tempPlayers = new Players(); //the class which get data from a database table 
       this.players = tempPlayers.getAllPlayers(); 
       this.columnNames = tempPlayers.getColumnNames(); 
       for (String colName : columnNames) { 
        columns.add(new ColumnModel(colName.toUpperCase(), colName)); 
       } 
       table.setRendered(true); 
       table.setValue(this.players); 
       break; 
       ... 
       default: 
       table.setRendered(false); 
     } //end of switch statement 
    } //end of goToTable() method 
} //end of DatabaseManagerBean 

该代码段将工作正是我在这个截图通缉! :-)

image http://s19.postimage.org/kt8xiy80z/image.png

此外,如果有人发现一些不明原因的离开或丢失,请写一个评论。
然后再次向BalusC致敬。因为没有他的提示,我将无法实现这一目标。 :-)
我还想感谢擎天柱和PrimeFaces团队创造出如此美妙的面孔。 :-)
我还要感谢Stackoverflow团队,为我们提供这样一个非常棒的平台来进行讨论! :-)
谢谢大家! :-)