显示一组数据表中正确的数据表

发布于 2025-01-06 22:43:21 字数 481 浏览 0 评论 0原文

参考这篇关于primefaces的帖子论坛。
有人知道如何在同一页面上使用多个数据表但只显示正确的数据表吗?
我的问题是我有一个视图作用域 bean,其属性包含来自数据库中不同表的数据。我有多个数据表用于每个数据库表的数据。现在我想根据从 (以红色圈出)中选择的基础值显示数据表。
这个屏幕截图会进一步解释一些。

image

Referring to this post on primefaces forum.
Has someone got any idea how can we use multiple datatable on the same page but only display the correct one?
My problem is that i have a view-Scoped bean whose properties contain data from different tables from a database. I have multiple datatables for data of each database table. Now i want to display the datatable on the basis value selected from <p:selectOneMenu> (encircled in red color).
this screenshot would explain a bit further.

image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜无邪 2025-01-13 22:43:21

基本方法是让表格的 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>

这很容易实现,但是您最终会在视图中看到大量代码(当然可以将其拆分为 文件)。更高级且可重用的方法是让单个表的值取决于菜单中选定的项目并使用 动态生成列。

<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);
}

每当您想要添加更专业的列时,这只允许不太细粒度的控制。您可能想使用标签文件。

The basic approach would be to let the rendered attribute of the tables depend on the selected item of the menu.

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

This is easy to implement, but you end up with a lot of code in the view (which can of course be split over <ui:include> files). The more advanced and reuseable approach would be to let the value of the single table depend on the selected item of the menu and use <p:columns> to generate columns dynamically.

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

with something like:

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

This only allows less fine-grained control whenever you want to add more specialized columns. You'd probably want to work with tag files instead.

黄昏下泛黄的笔记 2025-01-13 22:43:21

靠着神的恩典。经过一番挣扎!我终于实现了这个
为此,我非常感谢 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

另外,如果有人发现某些内容无法解释或丢失,请发表评论。
再次向 BalusC 致敬。因为如果没有他的提示,我将无法实现这个目标。 :-)
我还要感谢 Optimus Prime 和 PrimeFaces 团队创造了如此精彩的面孔。 :-)
我还要感谢 Stackoverflow 团队,为我们提供了这样一个精彩的讨论平台! :-)
谢谢大家! :-)

By the grace of God. After lots of struggle! i finally achieved this!
For this i pay bundles of thanks to BalusC for his expert tips
So i would like to share my solution with all.
So here is what i did in my xhtml file:

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

and in the backing 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

This code snippet would work exactly as i wanted in this screenshot! :-)

image

Also if someone find something left unexplained or missing, please write a comment.
And again hats off to BalusC. Because without his hints, i won't be able to achieve this objective. :-)
I would also like to say thanks to Optimus Prime and the PrimeFaces team to create such wonderful Faces. :-)
i would also pay my thanks to the Stackoverflow team, for providing us such a wonderful platform for such discussions! :-)
thank you all! :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文