Simple Calculator using Action Support


Like an entertainment or P.T class in our schooldays, the below calculator is created on behalf of the budding learners present all over the world.

A simple onChange event would call an action present in the apex class which intern provides the output on the visualforce page. Take a dive for yourself and surface with coding knowledge. Happy Coding !!!


Apex Class


public class ActionSupport
{
    
    public integer value1{get;set;}
    public integer value2{get;set;}
    public integer outvalue{get;set;}
    
    public String selectedvalue1{get;set;}
    
    public void callMethodinController()
    {
        if(selectedvalue1 == 'add')
        {
            outvalue = value1+value2;
        }else
        if(selectedvalue1 == 'mul')
        {
            outvalue = value1*value2;
        }else
        if(selectedvalue1 == 'div')
        {
            outvalue = value1/value2;
        }else
        if(selectedvalue1 == 'sub')
        {
            outvalue = value1-value2;
        }
        
    }
    public List<SelectOption> getAllvalues() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('--','--'));
        options.add(new SelectOption('add','ADD'));
        options.add(new SelectOption('mul','MUL'));
        options.add(new SelectOption('div','DIV'));
        options.add(new SelectOption('sub','SUB'));
        return options;
    }
}

Visualforce Page

<apex:page controller="ActionSupport">
    <apex:form id="xxx">
      <apex:pageBlock >
             <apex:inputText value="{!value1}"/>
             <apex:inputText value="{!value2}"/>
             <apex:selectList value="{!selectedvalue1}" size="1" >
                  <apex:selectOptions value="{!Allvalues}"/>
                  <apex:actionSupport event="onchange" 
                      action="{!callMethodinController}"/>
             </apex:selectList>  <br/><br/>
             
              YOUR OUTPUT VALUE IS:  &nbsp;
             <apex:outputText >{!outvalue}</apex:outputText>
      </apex:pageBlock>
    </apex:form>
</apex:page>
We need your support,Please share/Like us on Facebook ⛅
Simple Calculator using Action Support Simple Calculator using Action Support Reviewed by dasfrogpractice on 09:10 Rating: 5

1 comment:

Theme images by mariusFM77. Powered by Blogger.
Youtube Channel Image
Dasfrog Subscribe To watch more Salesforce Training
Subscribe