Want to show a data table populated with data after a button click

匿名 (未验证) 提交于 2019-12-03 02:33:02

问题:

I want to write a code in this way, that If I click on a button a data table appears populated with data. before that there should not be the data table in view.

<p:commandButton value="Go" styleClass="apply_button" actionListener="#{searchBean.getUserList}">     <f:attribute name="trigram" value="#{searchBean.trigram}"/>     <f:attribute name="firstName" value="#{searchBean.firstName}"/>     <f:attribute name="lastName" value="#{searchBean.lastName}"/> </p:commandButton> 

here the method getUserList() returns a list of data that should be populated in the data table.and it works.

<p:dataTable value="#{searchBean.listUser}" var="user">     <p:column headerText="Trigram">         <h:outputText value="#{searchBean.trigram}"/>     </p:column> </p:dataTable> 

It shows in the Data Table after I click on the button, But the Data Table appears before the button click with empty fields. How can I modify my codes to show make the Data Table appear after my button click? Codes are within the <h:form> Tag.

And the Managed Bean searchBean is in @ViewScoped.

回答1:

You can initialize a simple getter/setter :

private boolean visible = false; // getter/setter  public void getUserList(ActionEvent event) {     setVisible(true);      // Your code } 

And modify your view like this :

<p:commandButton value="Go" styleClass="apply_button" actionListener="#{searchBean.getUserList}" update="table-wrapper">     <f:attribute name="trigram" value="#{searchBean.trigram}"/>     <f:attribute name="firstName" value="#{searchBean.firstName}"/>     <f:attribute name="lastName" value="#{searchBean.lastName}"/> </p:commandButton>  <h:panelGroup id="table-wrapper">     <p:dataTable rendered="#{searchBean.visible}" value="#{searchBean.listUser}" var="user">         <p:column headerText="Trigram">             <h:outputText value="#{searchBean.trigram}"/>         </p:column>     </p:dataTable> </h:panelGroup> 

Note the update attribute on button, h:panelGroup wrapper and rendered attribute on table.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!