2013-01-23 53 views
0

我在開發我的第一個JSF2 RichFaces的應用程序,現在我面臨着以下問題:動態標籤添加到富人:一個tabpanel

我有以下菜單

<rich:panelMenu > 
      <rich:panelMenuGroup > 
       <rich:panelMenuItem label="Users" name="Users " /> 
       <rich:panelMenuItem label="Orders" name="Orders" /> 
      </rich:panelMenuGroup> 

</rich:panelMenu> 

我想,當點擊panelMenuItem創建一個新的選項卡,此新選項卡內必須插入包含我的應用程序

我看到這種類型的幾個例子中的所有用戶表

<rich:tabPanel switchType="client" id="tabPanel"> 
    <c:forEach items="#{handlerTab.tabsName}" var="tabName"> 
     <rich:tab name = ... > 

    </rich:tab> 
</c:foreach> 

,但不知道在我的新標籤

插入表格我該怎麼辦?


坦克您的回覆,但我不想申報視圖中的所有選項卡,我要動態地添加和刪除選項卡,現在我已經成功地做到這一點

<rich:panelMenu > 
<rich:panelMenuGroup > 
    <rich:panelMenuItem label="Users" name="Users" action="#{tabsBean.createTabs()}" render="tabs" /> 
    <rich:panelMenuItem label="Orders" name="Orders" action="#{tabsBean.createTabs()}" render="tabs" /> 
</rich:panelMenuGroup>     

<h:panelGrid id="tabs" binding="#{tabsBean.panelGrid}"/> 

然後我有管理的標籤

@ManagedBean 
@SessionScoped 
public class TabsBean { 

    private HtmlPanelGrid panelGrid; 
    private Integer numOfTabs; 

    @PostConstruct 
    public void init(){numOfTabs=1;} 


    public Integer getNumOfTabs() { 
    return numOfTabs; 
    } 

    public void setNumOfTabs(Integer numOfTabs) { 
    this.numOfTabs = numOfTabs; 
    } 

    public TabsBean() { 
    } 

    public HtmlPanelGrid getPanelGrid() { 
    return panelGrid; 
    } 

    public void setPanelGrid(HtmlPanelGrid panelGrid) { 
    this.panelGrid = panelGrid; 
    } 

    public void createTabs(){  


    FacesContext context = FacesContext.getCurrentInstance(); 
    Application application = context.getApplication(); 
    UITabPanel tabPanel = (UITabPanel)application.createComponent(UITabPanel.COMPONENT_TYPE); 
    tabPanel.setSwitchType(SwitchType.ajax); 

    for (int i=0; i<numOfTabs; i++){ 
     UITab tab = new UITab(); 
     tab = (UITab)application.createComponent(UITab.COMPONENT_TYPE); 
     tab.setName("User Count "+i); 
     tabPanel.getChildren().add(tab); 

    } 
    numOfTabs++; 
    panelGrid.getChildren().clear(); 
    panelGrid.getChildren().add(tabPanel); 
    } 


} 

現在我的問題是,我必須添加組件這個選項卡(數據表包含所有用戶,插入用戶和其他形式)

我該怎麼辦?

+0

你可以發佈你的'HandlerTab'類的實現? – kauedg

回答

2

如果你想在菜單打開特定內容的標籤是更好,如果你預先創建選項卡並隱藏它,根據請求顯示它(呈現)。要創建Users做到以下幾點:

創建User類(如果你沒有一個)

public class User {  

    private String name; 
    private String country; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getCountry() { 
     return country; 
    } 

    public void setCountry(String country) { 
     this.country = country; 
    } 
} 

創建UserTab託管bean

import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 

@ManagedBean 
@ViewScoped 
public class UserTab implements Serializable { 

    private List<User> users; 
    private boolean rendered; 

    public UserTab() { 
     //Initialize list 
     users = new ArrayList(); 

     //Initialize rendered attribute 
     rendered = false; 

     //Replace this for your User data retrieving method 
     createDummyUsers(); 
    } 

    private void createDummyUsers() { 
     User user = new User(); 

     user.setName("John Doe"); 
     user.setCountry("USA"); 
     users.add(user); 

     user = new User(); 
     user.setName("Bill Doe"); 
     user.setCountry("Canada"); 
     users.add(user); 

     user = new User(); 
     user.setName("Winston Doe"); 
     user.setCountry("England"); 
     users.add(user); 
    } 

    public void showTab() { 
     rendered = true; 
    } 

    public void hideTab() { 
     rendered = false; 
    } 

    public List<User> getUsers() { 
     return users; 
    } 

    public void setUsers(List<User> users) { 
     this.users = users; 
    } 

    public boolean isRendered() { 
     return rendered; 
    } 

    public void setRendered(boolean rendered) { 
     this.rendered = rendered; 
    } 
} 

創建一個標籤處理程序:

import java.io.Serializable; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 

@ManagedBean 
@ViewScoped 
public class TabHandler implements Serializable { 

    private String activeTab; 

    public TabHandler() { 
     activeTab = "none"; 
    } 

    public String getActiveTab() { 
     return activeTab; 
    } 

    public void setActiveTab(String activeTab) { 
     this.activeTab = activeTab; 
    } 
} 

創建視圖

<h:body> 
     <h:form> 
      <rich:panelMenu > 
       <rich:panelMenuGroup > 
        <rich:panelMenuItem label="Users" name="Users" 
             actionListener="#{userTab.showTab()}"> 
         <f:ajax event="select" 
           execute="@this" 
           render="tabPanel" 
           listener="#{tabHandler.setActiveTab('usersTab')}"/> 
        </rich:panelMenuItem> 
        <rich:panelMenuItem label="Orders" name="Orders" /> 
       </rich:panelMenuGroup> 
      </rich:panelMenu> 


      <rich:tabPanel id="tabPanel" 
          switchType="client" 
          activeItem="#{tabHandler.activeTab}"> 

       <rich:tab id="mainTab"> 


       </rich:tab> 

       <rich:tab id="usersTab" 
          rendered="#{userTab.rendered}"> 
        <f:facet name="header"> 
         <h:outputLabel value="Users"/> 
         <h:commandLink value=" X" actionListener="#{userTab.hideTab()}"/> 
        </f:facet> 
        <rich:dataTable value="#{userTab.users}" 
            var="user"> 

         <rich:column> 
          #{user.name} 
         </rich:column> 

         <rich:column> 
          #{user.country} 
         </rich:column> 
        </rich:dataTable> 

       </rich:tab> 

      </rich:tabPanel> 
     </h:form> 
    </h:body> 

執行相同的步驟併爲Order類添加所需的標記。

參見: RichFaces dynamic TabPanel