Call controller method from tab component

How to call controller method from tab component in visualforce?

Apex:

public with sharing class tabController2 {
    public list<account> accList {get; set;}
    public list<contact> conList {get; set;}
    
    public tabController2(){
        accList = [select name from account where industry = 'energy'];    
    }
    
    public void contactRec(){
        conList = [select lastName from contact];
    }
}

 

Page:

<apex:page controller="tabController2">
     <script>
        // jsFunc is called from tab2 (referred by id in apex:tab) 
         function jsFunc(){
             actionfun();
         }
     </script>

     <apex:form >
         <apex:tabPanel id="tabP"  switchType="ajax">
             <apex:tab label="Energy Accounts">             
                 <apex:pageblock >
                    <apex:pageblockTable value="{!accList}" var="a">
                        <apex:column value="{!a.name}"/>
                    </apex:pageblockTable>                    
                 </apex:pageblock> 
             </apex:tab>
             
             <apex:tab label="Contact Records" oncomplete="jsFunc()" id="tab2">
                  <apex:pageblock >
                    <apex:pageblockTable value="{!conList}" var="c">
                        <apex:column value="{!c.lastName}"/>
                    </apex:pageblockTable>                    
                </apex:pageblock> 
             </apex:tab>             
         </apex:tabPanel>
         
         <!-- Once the actionfun is invoked from javascript and contactRec will be invoked -->
         <apex:actionFunction name="actionfun" action="{!contactRec}" reRender="tabP"/>
     </apex:form>
</apex:page>

 

Related posts

Leave a Comment