You need to sign in to do that
Don't have an account?
Rakesh M 20
created calculator and trying to perform all math operation
I am Trying to Perform Mathoperations in Calculator but its not working,can anyone help me to fix this issue.
Here's My code
VF Page
<apex:page controller="Caluclator">
<script type="text/javascript">
function textFunc()
{
var num1 = document.getElementById('n1').value;
var num2 = document.getElementById('n2').value;
String op;
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.Caluclator.showText}', num1,num2,op,
function(result, event)
{
debugger;
if(event.status)
{
// This is the way to get VF tag id to map result returned from call back function parameter result
document.getElementById("{!$Component.pb.pbs.pbsi.numone}").innerHTML = result
System.debug('Result--'+result);
}
{escape: true}
});
}
</script>
Number 1 :<input id="n1" type="Number" />
Number 2 :<input id="n2" type="Number" />
<button type="button" onclick="textFunc('add')">Add</button>
<button type="button" onclick="textFunc('sub')">Sub</button>
<button type="button" onclick="textFunc('mul')">Mul</button>
<button type="button" onclick="textFunc('div')">Div</button>
<button type="button" onclick="textFunc('mod')">Mod</button>
<button type="button" onclick="textFunc('perc')">Perc</button>
<apex:pageBlock id="pb">
<apex:pageBlockSection id="pbs">
<apex:pageBlockSectionItem id="pbsi">
<apex:outputText id="numone"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
Controller :
public class Caluclator {
Public Integer num1 {get;set;}
Public Integer num2 {get;set;}
Public String fun {get;set;}
@RemoteAction
public static Integer showText(Integer num1,Integer num2,String fun)
{ integer result=0;
//return num1 + num2;
if(fun == 'add')
{
result = num1 + num1;
}
else if(fun == 'sub')
{
result = num1 - num1;
}
else if(fun == 'mul')
{
result = num1 * num1;
}
else if(fun == 'div')
{
result = num1 / num1;
}
else if(fun == 'mod')
{
result = math.mod(Integer.valueOf(num1) , Integer.valueOf(num2));
}
else if(fun == 'perc')
{
result = (num1/num2) * 100;
}
return result;
}
}
Here's My code
VF Page
<apex:page controller="Caluclator">
<script type="text/javascript">
function textFunc()
{
var num1 = document.getElementById('n1').value;
var num2 = document.getElementById('n2').value;
String op;
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.Caluclator.showText}', num1,num2,op,
function(result, event)
{
debugger;
if(event.status)
{
// This is the way to get VF tag id to map result returned from call back function parameter result
document.getElementById("{!$Component.pb.pbs.pbsi.numone}").innerHTML = result
System.debug('Result--'+result);
}
{escape: true}
});
}
</script>
Number 1 :<input id="n1" type="Number" />
Number 2 :<input id="n2" type="Number" />
<button type="button" onclick="textFunc('add')">Add</button>
<button type="button" onclick="textFunc('sub')">Sub</button>
<button type="button" onclick="textFunc('mul')">Mul</button>
<button type="button" onclick="textFunc('div')">Div</button>
<button type="button" onclick="textFunc('mod')">Mod</button>
<button type="button" onclick="textFunc('perc')">Perc</button>
<apex:pageBlock id="pb">
<apex:pageBlockSection id="pbs">
<apex:pageBlockSectionItem id="pbsi">
<apex:outputText id="numone"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
Controller :
public class Caluclator {
Public Integer num1 {get;set;}
Public Integer num2 {get;set;}
Public String fun {get;set;}
@RemoteAction
public static Integer showText(Integer num1,Integer num2,String fun)
{ integer result=0;
//return num1 + num2;
if(fun == 'add')
{
result = num1 + num1;
}
else if(fun == 'sub')
{
result = num1 - num1;
}
else if(fun == 'mul')
{
result = num1 * num1;
}
else if(fun == 'div')
{
result = num1 / num1;
}
else if(fun == 'mod')
{
result = math.mod(Integer.valueOf(num1) , Integer.valueOf(num2));
}
else if(fun == 'perc')
{
result = (num1/num2) * 100;
}
return result;
}
}
Try the below code as reference:
<apex:page controller="Calculator">
<apex:form >
<apex:inputText value="{!number1}"/>
<apex:inputText value="{!number2}"/>
<apex:inputText value="{!operator}"/>
<apex:commandButton value="ShowResult" action="{!calculation}">{!result}</apex:commandbutton>
</apex:form>
</apex:page>
//////////////// Controller /////////////////
public class Calculator
{
public integer number1{get;set;}
public integer number2{get;set;}
public string operator{get;set;}
public double result{get;set;}
public void calculation()
{
if(operator.contains('+'))
result=number1+number2;
if(operator.contains('-'))
result=number1-number2;
}
}
For further reference, you can check the below,
https://developer.salesforce.com/forums/?id=906F00000009BhFIAU
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks.
Thanks for Your Respone.
I am using "VF Remoting" to perform this