ads

Wednesday, 12 April 2017

Wrapper Class : Using InnerClass Constructor In Main Class Constructor



Wrapper Class:-


public class DeleteTeacherRecords
{
    public list<Teacher__c> studentRecds{get;set;}
    public list<TeacherInner> allTechRec {get;set;}
    
    public DeleteTeacherRecords(ApexPages.StandardController controller) {
    
        allTechRec = new list<TeacherInner>();
       
        //Fetching Teacher's Records
        studentRecds = [select id,name from Teacher__c];
        
        //Creating a list of Records to show in Visualforce page
        for(integer i =0;i<studentRecds.size();i++)
        {
           //Calling InnerClass constructor
             TeacherInner conObj = new TeacherInner(false,studentRecds[i]);
             allTechRec.add(conObj);
        }        
    }

    public pagereference deleteTearcherRecords()
    {    
        for(integer i=0;i<allTechRec.size();i++)
        {         
            if(allTechRec[i].checkbox == true)
            {           
                delete allTechRec[i].techObj;
            }
        }
      
        pagereference ref = new pagereference('/apex/DeleteRecUsingWrap');
        ref.setredirect(true);
        return ref;
    }
    //Inner Class
    public class TeacherInner
    {
        public boolean checkbox{get;set;}
        public Teacher__c techObj{get;set;}
        //This Inner class constructor is used in main classs constructor to show
        //Records in visualforce page
        public TeacherInner(boolean Mainclasscheckbox,Teacher__c 
                        MainclasstechObj)
        {
            checkbox = Mainclasscheckbox;
            techObj   = MainclasstechObj;
        }
    }
}


Visualforce Page :-


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<apex:page standardController="Teacher__c" extensions="DeleteTeacherRecords">
  <apex:form >
      <apex:pageBlock >
        <apex:pageBlockTable value="{!allTechRec}" var="a">
             <apex:column headerValue="Select">
               <apex:inputCheckbox value="{!a.checkbox}"/>
            </apex:column>           
            <apex:column headerValue="Teacher Name">
               <apex:outputField value="{!a.techObj.name}"/>
            </apex:column>            
        </apex:pageBlockTable>
        <apex:pageBlockButtons >
        <apex:commandButton value="Delete Selected Records" action="{!deleteTearcherRecords}"/>
        </apex:pageBlockButtons>
      </apex:pageBlock>
  </apex:form> 
</apex:page>


Output Screenshot:


No comments:

Post a Comment