primefaces tabView activeIndex issue

Roberto de Santis picture Roberto de Santis · Mar 7, 2011 · Viewed 31.6k times · Source

I have Primefaces TabView with two Tab like:

<p:tabView dynamic="true" cache="false"
           onTabShow="scrollBottom(#{stanzaBean.activeIndex})"
           tabChangeListener="#{messaggioBean.onTabChange}"
           activeIndex="#{stanzaBean.activeIndex}" >

it works fine, except that when I change the Tab the activeIndex isn't updated on the Server and it returns always the default value. I'm using primefaces 2.2.1.

Thank you.

Answer

Mark picture Mark · Mar 7, 2011

Going by the PrimeFaces ShowCase example, if you give each tab an id:

<p:tabView tabChangeListener="#{indexBean.onTabChange}" >
    <p:tab title="tab 0" id="tab0"></p:tab>
    <p:tab title="tab 1" id="tab1" ></p:tab>
    <p:tab title="tab 2" id="tab2"></p:tab>               
</p:tabView>

you can get that tab id in the tabChangeListener.

public void onTabChange(TabChangeEvent event) {       
    System.out.println("tab id = " + event.getTab().getId());
}

Then you'll know which tab was selected.


Edit:

There is an open PrimeFaces issue 1640 TabView: Wrong activeIndex in TabChangeListener, always 0 on the problem you are having.


Edit 2:

With PrimeFaces 5.0 and up the tabChangeListener is no longer available on the tabView element but should be used via an explicit ajax tag with a tabChange event.

 <p:tabView id="analysisSections" value="#{analysisBean.analysis.sections}" var="section" activeIndex="#{analysisBean.activeIndex}">
      <p:ajax event="tabChange" listener="#{analysisBean.onTabChange}"/>

Also you can directly get index of tab:

public void onTabChange(TabChangeEvent event) {
    activeIndex = ((TabView) event.getSource()).getIndex();
}

with all these changes, activeIndex works properly.