2013-04-01 46 views
1

我正在使用Primefaces 3.4和JSF 2.1,我試圖讓一個p:面板項目出現並消除關於p:oneselectmenu的選擇。我在面板內有一個p:datatable,我想要顯示並動態填充,這就是我使用面板的原因。數據表的工作很好,但面板沒有。我嘗試將面板上的「渲染」選項與隨菜單的每個選項更改而變化的變量進行鏈接,但沒有發生任何事情。我也嘗試過p:outputpanel,但沒有發生任何事情。所以我試着用同樣的動作按鈕,但它失敗了。這是我的代碼:如何渲染一個p:面板,由選擇p:selectonemenu觸發?

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui" 
    xmlns:f="http://java.sun.com/jsf/core"> 
<h:body> 
    <ui:composition template="/template.xhtml"> 
     <ui:define name="title"> 
      #{msg.teaching}: #{msg.socialSecurity} 
     </ui:define> 
     <ui:define name="body"> 
      <h:form id="form"> 
       <p:messages id="messages" autoUpdate="true"/>     
       <p:panel id="selectSocialSecurityPanel" rendered="true"> 
        <p:panelGrid> 
         <p:row> 
          <p:column> 
           <p:selectOneMenu value="#{selectOneMenuSocialSecurityTeachingBean.choice}"> 
            <p:ajax update="socialSecurityDataTablePanel, socialSecurityDataTable, toUpdate" listener="#{dataTableSocialSecurityBean.updateTable()}"/> 
            <f:selectItem itemLabel="#{msg.select}" itemValue=""/> 
            <f:selectItems value="#{selectOneMenuSocialSecurityTeachingBean.socialSecurityTeachingList}"/> 
           </p:selectOneMenu> 
          </p:column> 
          <p:column> 
           <p:commandButton value="#{msg.find}" actionListener="#{dataTableSocialSecurityBean.updateTable()}" update="socialSecurityDataTablePanel, socialSecurityDataTable, toUpdate" id="button"> 
            <f:ajax render="toUpdate"/> 
           </p:commandButton> 
          </p:column> 
         </p:row> 
        </p:panelGrid> 
       </p:panel>      
       <p:outputPanel id="toUpdate" rendered="#{dataTableSocialSecurityBean.rendered}" autoUpdate="true"> 
        <p:panel id="socialSecurityDataTablePanel"> 
         <p:dataTable id="socialSecurityDataTable" var="unit" value="#{dataTableSocialSecurityBean.socialSecurityList}">      
          <p:columns value="#{dataTableSocialSecurityBean.columns}" var="column" columnIndexVar="colIndex"> 
           <f:facet name="header"> 
            #{column.header} 
           </f:facet> 
           #{unit[column.property]} 
          </p:columns> 
         </p:dataTable>      
        </p:panel> 
       </p:outputPanel> 
      </h:form> 
     </ui:define> 
    </ui:composition> 
</h:body> 

這:

@ManagedBean 
@ViewScoped 
public final class DataTableSocialSecurityBean implements Serializable { 

    private String choice; 
    private List<Unit> socialSecurityList; 
    private boolean rendered; 
    private List<ColumnModel> columns = new ArrayList<ColumnModel>(); 

    public boolean getRendered(){ 
     System.out.println("DataTableSocialSecurityBean getRendered"); 
     System.out.println("DataTableSocialSecurityBean getRendered="+rendered); 
     return rendered; 
    } 

    public void updateTable() { 
     rendered=true; 
     socialSecurityList = new ArrayList<Unit>(); 
     createDynamicColumns(); 
     populateDataTable(socialSecurityList); 

    } 
} 

任何想法?

回答

5

您的rendered字段最初是false,因此面板不會在第一個視圖中呈現。在最初的HTML中,這個視圖中沒有標籤,其ID爲toUpdate,因此無法呈現。我建議你把這個panel一些h:panelGroup內重新呈現此組件:

<h:panelGroup id="myContainer" layout="block"> 
    <p:outputPanel id="toUpdate" rendered="#{dataTableSocialSecurityBean.rendered}" autoUpdate="true"> 
    </p:outputPanel> 
</h:panelGroup> 

並使用render="myContainer"。我還建議您使用p:ajax而不是f:ajax作爲primefaces組件。

你可以嘗試添加特效添加p:effect成分爲p:outputPanel孩子:

<p:effect type="explode" event="load"/> 

對於事件的完整列表,請參閱PrimeFaces用戶指南。

+0

謝謝,它工作的很好。 –

+0

不客氣。 – partlov

+0

你知道是否有可能在面板出現/消失時發揮作用,而不是從無到有? –

1

爲了讓呈現的事件像你想要的那樣工作,組件應該是可見的(存在於dom中)。最知名的黑客是呈現屬性應用到內部組件(不是outputpanel,但它裏面的面板)

<p:outputPanel id="toUpdate" autoUpdate="true"> 
    <p:panel id="socialSecurityDataTablePanel" rendered="#{dataTableSocialSecurityBean.rendered}"> 
</p:panel> 
</p:outputPanel> 

這樣,你的outputPanel是可見的,每次(DOM中的存在),可以說出它的孩子們根據條件呈現自己。

相關問題