Wednesday 5 September 2012

Inner Wrapper Class in Apex Class

Hi,

I have seen a lot of questions regarding inner class in a apex class in developer community and issues that can be solved by use of inner class. So here I would like to show a problem which can also be solved by inner class.

Syntax of inner class :

  1. public class myOuterClass {  
  2.    // Additional myOuterClass code here    
  3.       
  4.    class myInnerClass {  
  5.      // myInnerClass code here    
  6.       
  7.    }  
  8. }  

Problem : 
http://boards.developerforce.com/t5/General-Development/When-i-click-on-add-button-i-need-to-display-one-more-textbox/td-p/286261

When i click on add button i need to display one more textbox 

Visual Force Page :
  1. <apex:page controller="addTextrBox">  
  2.   <apex:Form>  
  3.        <apex:commandButton Value="Add Text Box" action="{!addTextBox}"/>  
  4.          
  5. </br>  
  6.        <apex:repeat value="{!listvalueOfTextBox}" var="item" rendered="{!IF(listvalueOfTextBox.size > 0 , true , false)}" >  
  7.            <apex:outPutLabel value="{!item.textBoxLabel}">  
  8.            </apex:outPutLabel>  
  9.            <apex:inputText value="{!item.textBoxValue}"/>  
  10.              
  11. </br>  
  12.        </apex:repeat>  
  13.    </apex:Form>  
  14. </apex:page>  

Apex Controller Class

  1. public class addTextrBox   
  2. {  
  3.   
  4.     public List<textBoxClass> listValueOfTextBox  
  5.         {   
  6.           get;   
  7.           set;   
  8.         }      
  9.     public addTextrBox ()  
  10.         {  
  11.             listvalueOfTextBox = new List<textBoxClass>();  
  12.         }  
  13.     public PageReference addTextBox()   
  14.         {  
  15.             try  
  16.                 {  
  17.                     listvalueOfTextBox.add(new textBoxClass('TextBox' + (listvalueOfTextBox.size() +  1)));  
  18.                 }  
  19.             catch(Exception e)  
  20.                 {  
  21.                     ApexPages.addMessages(e);  
  22.                 }  
  23.             return ApexPages.currentPage();  
  24.         }  
  25.   
  26.      public class textBoxClass  
  27.          {  
  28.              public string textBoxLabel{get;set;}  
  29.              public string textBoxValue{get;set;}  
  30.                
  31.              public textBoxClass(String textBoxLabel)  
  32.                  {  
  33.                      this.textBoxLabel = textBoxLabel;  
  34.                  }  
  35.          }  
  36. }  

Here textBoxClass is the inner class

In above problem we used iner class now any new text box is binded with a new item in the list listvalueOfTextBox which is of type textBoxClass.

inner class acan be intancitiated like this
outerClass.innerclass instanceOfinnerClass = new outerClass.innerclass();

in above case it will be like
addTextrBox.textBoxClass instaceOftextBoxClass = new addTextrBox.textBoxClass('Test text Box');

Regards,
Ashok.

No comments:

Post a Comment