Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

primefaces tab on click

I have a primefaces tab view like this :

<p:tabView id="tabView">  
    <p:tab id="tab1" title="Godfather Part I">  
        This tab has static content.
    </p:tab>  

    <p:tab id="tab2" title="Godfather Part II">  
        this tab has a datatable whose  
    </p:tab>  
</p:tabView>

Now, what I want to do is, fire a managedbean method when user clicks on tab2. I can add an actionlistener on tabchange event of tabView but this will fire the method on click of tab1 too. How do I fire a method on click of tab2 only?

like image 896
Eddard Stark Avatar asked Jul 18 '13 13:07

Eddard Stark


1 Answers

Action listener will be called at click of every tab of your tabView. But you can control your action Listener by conditional blocks like this. Here is an example:

<p:tabView id="tabView" dynamic="true">

    <p:ajax event="tabChange" listener="#{Bean.onTabChange}" update=":messages" />
    <p:tab id="tab1" title="#{userLbl['notification.headerIncoming']}">
    <p:tab id="tab2" title="#{userLbl['notification.headerOutgoing']}">
</p:tabView>

Action Listener:

public void onTabChange(TabChangeEvent event) {

   if (event.getTab().getId().equals("tab1")) {
       //Your actions for tab1
   } else if((event.getTab().getId().equals("tab2")) {
     //Your actions for tab2
   }
}
like image 153
V_K Avatar answered Nov 15 '22 19:11

V_K