2012-03-28 38 views
6

活動索引沒有自動更新。在幾篇文章中提到將tabView放在一個表單上。或者通過在tabview中包含<p:ajax event="tabChange"/>它可以工作。但似乎沒有任何工作tabview的活動索引沒有自動更新

XHTML

示例1:自動更新

<p:tabView id="categoryTabView" var="promoArticle" value="#{promotionDetailBean.artDTOs}" activeIndex="#{promotionDetailBean.activeTabIndex}"> 
      <p:tab id="categoriesTab" title="#{promoArticle.categoryName}"> 
       <p:dataTable id="promotionDetail_dataTable" var="articlePromo" value="#{promoArticle.artVO}" selection="#{promotionDetailBean.selectedArt}" rowIndexVar="rowIndex"> 
        <p:column id="select" selectionMode="multiple" /> 

        <p:column id="barCode"> 
         <h:inputText id="barCodeInputTxt" value="#{articlePromo.barCode}" 
         styleClass="inputTextStyle" onchange="onSuggestedValueChange('categoryTabView',#{promotionDetailBean.activeTabIndex}, 'promotionDetail_dataTable',#{rowIndex},'originalCostInputTxt')" /> 
        </p:column> 
       </p:dataTable> 
      </p:tab> 
     </p:tabView> 

樣品2:tabChange事件更新

<h:form id="form"> 
    <p:growl id="growlm" showDetail="true" /> 

      <p:tabView id="categoryTabView" var="promoArticle" value="#{promotionDetailBean.artDTOs}" > 
       <p:ajax event="tabChange" listener="#{promotionDetailBean.tabChanged}" update=":growlm" /> 
        <p:tab id="categoriesTab" title="#{promoArticle.categoryName}"> 
         <p:dataTable id="promotionDetail_dataTable" var="articlePromo" value="#{promoArticle.artVO}" selection="#{promotionDetailBean.selectedArt}" rowIndexVar="rowIndex"> 
          <p:column id="select" selectionMode="multiple" /> 

          <p:column id="barCode"> 
           <h:inputText id="barCodeInputTxt" value="#{articlePromo.barCode}" 
           styleClass="inputTextStyle" onchange="onSuggestedValueChange('categoryTabView',#{promotionDetailBean.activeTabIndex}, 'promotionDetail_dataTable',#{rowIndex},'originalCostInputTxt')" /> 
          </p:column> 
         </p:dataTable> 
        </p:tab> 
       </p:tabView> 

我需要識別「onChange」事件上的單元格。但activeIndex始終爲0,即初始化值。該事件沒有得到通話。

private Integer activeTabIndex = 0; 
public Integer getActiveTabIndex() { 
    return activeTabIndex; 
} 
public void setActiveTabIndex(Integer activeTabIndex) { 
    this.activeTabIndex = activeTabIndex; 
} 

public void tabChanged(TabChangeEvent event){ 
     TabView tv = (TabView) event.getComponent(); 
     this.activeTabIndex = tv.getActiveIndex(); 
    } 

但該事件沒有得到trigerred。也不會自動更新。

可能的問題是什麼?

感謝, Shikha

回答

5

以下爲我工作:

XHTML:

<p:tabView id="categoryTabView" var="promoArticle" 
     value="#{promotionDetailManagedBean.articuloPromocionDTOs}" dynamic="true"> 
    <p:ajax event="tabChange" listener="#{promotionDetailManagedBean.onTabChange}" /> 
    <p:tab> ... </p:tab> 
</p:tabView> 

豆:

public final void onTabChange(final TabChangeEvent event) { 
    TabView tv = (TabView) event.getComponent(); 
    this.activeTabIndex = tv.getActiveIndex(); 
} 
+1

這似乎只有在[活動索引不綁定到託管bean](http://code.google.com/p/primefaces/issues/detail? ID = 1640&q = activeIndex&colspec = ID%20Stars%20Module%20Type%20Status%20Priority%20TargetVersion%20Reporter%20Owner%20Summary)。我很驚訝Primefaces團隊還沒有解決它(我正在使用3.5版本)。然而,如果'tabView'本身被封裝到一個表單中,而不是每個標籤都有一個表單,它就可以工作。 – 2013-02-26 12:45:00

+2

this.activeTabIndex = tv.getActiveIndex(); 不起作用。相反,你可以使用 this.activeTabIndex = tv.getChildren()。indexOf(event.getTab()); – ahmet 2013-06-26 11:09:35

+0

索引是以0還是1開頭? – xav56883728 2015-04-10 10:34:48

0
<p:column id="barCode"> 
    <h:inputText id="barCodeInputTxt" value="#{articlePromo.barCode}" 
      styleClass="inputTextStyle" onchange="onSuggestedValueChange('categoryTabView', 
    #{promotionDetailBean.activeTabIndex}, 
    'promotionDetail_dataTable',#{rowIndex}, 
    'originalCostInputTxt')" /> **_____/>_____** 
</p:column> 
  1. 你有一個額外/>
  2. 立即真,則可能會導致問題號碼:阿賈克斯,那麼當你沒有驗證,或者使用即時真正在嘗試取消操作。

    public void tabChange(TabChangeEvent event){ 
        TabView tabView = (TabView) event.getComponent(); 
        Tab tab = (Tab) event.getTab(); 
        String tabChangedMessage = tab.getTitle() + " changed. index=" + tabView.getActiveIndex(); 
        System.out.println(tabChangedMessage); 
        FacesContext fc = FacesContext.getCurrentInstance(); 
        fc.addMessage(null, new FacesMessage("Tab changed", tabChangedMessage)); 
    } 
    
    <p:tabView> 
        <p:ajax event="tabChange" listener="#{tabChangeBean.tabChange}" update=":growlId" /> 
        <p:tab title="tab 1"> 
        tab1 inside 
        </p:tab> 
        <p:tab title="tab 2"> 
        tab1 inside 
        </p:tab> 
    </p:tabView> 
    

這工作,但活動指數爲0。

+0

額外/>是一個錯字..我沒有立即嘗試過。不工作。 – 2012-03-29 07:46:40

+0

我已經嘗試過你的例子,但activeIndex始終是0.也許我錯了某些東西,但我必須嘗試它。我也會編輯我的答案,你應該檢查它。 – utkusonmez 2012-03-29 11:04:59

0
在你的XHTML您呼叫監聽

= 「#{promotionDetailBean。onTabChange}」,但在你的bean的方法名稱爲「公void tabChanged「。所以它不能觸發。在豆(TabBean)

我在試圖在黃金展示一個例子

private Integer activeTabIndex = 1; 

// setter/getter 

public void onTabChange(TabChangeEvent event) { 
    FacesMessage msg = new FacesMessage("Tab Changed", "Active Tab: " + event.getTab().getTitle()); 

    TabView tabView = (TabView) event.getComponent(); 
    int activeTabIndex = tabView.getActiveIndex(); 
    System.out.println("--------" + activeTabIndex); 


    FacesContext context = FacesContext.getCurrentInstance(); 
    Map<String, String> params = context.getExternalContext().getRequestParameterMap(); 
    String activeIndexValue = params.get(tabView.getClientId(context) + "_activeIndex"); 
    System.out.println("--------" + activeIndexValue); 

    context.addMessage(null, msg); 
} 

在XHTML(tabviewChangeListener。XHTML):

<p:tabView id="tabView" dynamic="true" activeIndex="#{tabBean.activeTabIndex}"> 

       <p:ajax event="tabChange" listener="#{tabBean.onTabChange}" update=":form:growl"/> 

       <p:tab title="Godfather Part I" id="Godfather1"> 

結果是如預期: 在開始第二個選項卡被顯示。我點擊第一個標籤然後第三個。系統輸出是:

-------- 0

-------- 0

-------- 2

---- ---- 2

+0

另外,activeTabIndex的setter,setActiveTabIndex(),以正確的索引值被調用。 – dasgin 2012-03-29 14:47:21

+0

和xhtml你應該把tabView放在h:form組件 – dasgin 2012-03-30 06:33:07

2

這似乎只當active index is not binded to the managed bean工作。但是這對我來說毫無用處,因爲我想保留一個支持bean-view同步。我很驚訝Primefaces團隊尚未解決它(我正在使用3.5版本)。然而,如果p:tabView本身被封裝到h:form而不是每個選項卡都有其中一個,但它可以用於更改提交上下文。

11

當標籤未包含在表格內,但每個選項卡包含這工作對我自己的一個:

  1. 添加監聽器選項卡更改事件,以您的TabView的組件:

    <p:ajax event="tabChange" listener="#{userBean.onTabChange}" />
  2. 從基礎TabView組件獲取活動索引似乎沒有工作。然而,在該事件中,新選定的選項卡被正確地更新,所以我們可以通過在TabView的子女組成部分搜索getthe指數:

 
    public void onTabChange(TabChangeEvent event) 
    { 
     TabView tabView = (TabView) event.getComponent(); 
     activeTab = tabView.getChildren().indexOf(event.getTab()); 
    } 

我希望這個作品對你來說太

0

我簡單的辦法這與:

public void onSPTabChange(TabChangeEvent event) 
{ 
    TabView tabView = (TabView) event.getComponent(); 
    currentData.setSP_Index(tabView.getChildren().indexOf(event.getTab())+1); 
} 

在CURRENTDATA我有屬性SP_Index與標籤的數量(第一,第二...。)

<p:tabView id="tabView" styleClass="tabView"> 
<p:ajax event="tabChange" listener="#{dataAccess.onSPTabChange}" /> 
.... 

,並添加jQuery腳本

<script> 
    $("#tabView li:nth-child(#{currentData.SP_Index})").click(); 
</script> 
4

對於primefaces> = 5.0 請使用如下代碼:

public final void onTabChange(TabChangeEvent event) { 
    TabView tv = (TabView) event.getComponent();  
    this.ActiveIndex = tv.getChildren().indexOf(event.getTab()); 
} 
+0

似乎是Android :) – delive 2017-12-14 10:11:51

-1
public final void onTabChange(final TabChangeEvent event) { 
    TabView tv = (TabView) event.getComponent(); 
    this.activeTabIndex = tv.getIndex(); 
} 

這個工作對我來說...

+0

這與其他答案中的內容有何不同? – Kukeltje 2017-07-10 20:24:47