Primefaces tabview: set the active index on tab change

Posted by Virgi on Stack Overflow See other posts from Stack Overflow or by Virgi
Published on 2013-05-16T09:58:08Z Indexed on 2013/07/03 5:06 UTC
Read the original article Hit count: 5941

Filed under:
|
|

I've got a tab view that contains two tab.

When I switch from tab 1 to tab 2, I'm calling some code that executes a validation and updates some values. Depending on the result of this validation, I would like to stay on tab 1, or go to tab 2, and refresh the tabs' content.

My tabview:

<h:form id="form"> 
    <p:tabView id="tabview" activeIndex="#{ctrl.idx}" dynamic="true" cache="false">  
        <p:ajax event="tabChange" listener="#{ctrl.doStuff}" update=":form:tabview"/>
        <p:tab title="Tab 1" id="t1">  
            <h:panelGrid columns="1" cellpadding="10">
                <h:outputText value="#{ctrl.s1}"/>
            </h:panelGrid>  
        </p:tab>  
        <p:tab title="Tab 2" id="t2">  
            <h:panelGrid columns="1" cellpadding="10">
                <h:outputText value="#{ctrl.s2}"/>
            </h:panelGrid>  
        </p:tab>
    </p:tabView>  
</h:form>

My test code that simly changes the values:

public void doStuff() {
    s1 = String.valueOf(Math.random());
    s2 = String.valueOf(Math.random());
}

I thought that changing the active tab index in my method would be enough, like that:

public void doStuff() {
    // ...
    idx = 0;
}

On tabChange event, the method is called but the tabview components goes to the clicked tab, ignoring the idx new value.

I thought adding an update attribute to p:ajax would render the whole tabview but only the tabs and/or the tabs' content is re rendered.

And weirdest of all, if I change update=":form:tabview" to update=":form" or update="@form", I only receive the tab's content in the ajax response -> the component disappears from the page!

My bean is viewscoped, I'm using Primefaces 3.5, JSF 2.1 and Tomcat 7.

Any idea? Thanks.

© Stack Overflow or respective owner

Related posts about AJAX

Related posts about jsf-2