繁体   English   中英

如何从ManagedBean填充JSF / Primefaces数据表?

[英]How to fill a JSF/Primefaces DataTable from a ManagedBean?

我正在使用JSF2和Primefaces 5实现我的第一个应用程序。 我希望当用户按下按钮时,我的托管bean创建一个新的DataTable并用一些数据填充它。 我该如何从托管bean中做到这一点? 这是我的托管bean的代码的一部分。

...
//This is the managed bean's method executed when the user presses the button.  
public void execute() {
    ...
    //Get the data from database and put the entries in a list of java beans called myList.
    //Here I create a ListDataModel to convert myList in a DataModel to pass to the datatable
    ListDataModel<myJavaBean> tableData = new ListDataModel<myJavaBean>(myList);
    //Then I create a new datatable..
    DataTable dataTable = new DataTable();
    //.. and I set the data
    dataTable.setValue(tableData);
    //I define the columns
    Column idColumn = new Column();
    idColumn.setHeaderText("id");
    Column dateColumn = new Column();
    dateColumn.setHeaderText("date");
    Column timeColumn = new Column();
    timeColumn.setHeaderText("time");
    Column stateColumn = new Column();
    stateColumn.setHeaderText("state");
    //Here I add the columns to the table
    dataTable.getChildren().add(idColumn);
    dataTable.getChildren().add(dateColumn);
    dataTable.getChildren().add(timeColumn);
    dataTable.getChildren().add(stateColumn);
    ...         

当我按下按钮时,这部分代码将创建一个具有正确列数和正确标题的新DataTable。 同样,行数是正确的,但行完全为空,我想这是由于我没有将Columns与列表的各个Java bean属性绑定在一起。 我怎样才能做到这一点?

因此,您想从托管bean创建UI组件?

也许我在这里丢失了一些东西,但是实现这一目标的一种正确而简单的方法是必须使用<p:datatable>在facelet(your_page.hxtml)中声明Datatable并将其链接到包含数据(即value="#{yourbean.yourlist}"的后备bean。 value="#{yourbean.yourlist}" )。

启动时,您的支持bean可以为空。 无论是否加载数据,您都只需要以不同的方式呈现UI(请参见datatable组件上的“ rendered”标记)。

只需在需要时将数据填充到Bean中即可(按钮/执行)。

并且不要忘记更新UI部件(请参见操作按钮上的“ update”标签)以刷新更改的页面区域(至少是数据表)。

例:

 <p:datatable id="mytable" var="child" value="#{mybean.children}" rendered="#{not empty mybean.children}"> 
 ...
 </p:datatable>
 ...
 <p:button update="mytable">
 ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM