You need to sign in to do that
Don't have an account?
shrey.tyagi88@tcs.com
Add N numbers in a visuaforce page!!!Plz help!!!
Hi,
I have a requirement that is explained below:
I have one input field on visualforce page. Along with this there is a button "Get Sum". Now I want that when user enters a number and clicks the button , an outpu field should show the sum of all numbers entered earlier and the present number. for eg:
Input Output
1 0+1=1
2 1+2=3
3 3+3=6
4 4+6=10
5 5+10=15
6 15+6=21
7 21+7=28
8 28+8=36
.
.
.
.
.
N numbers
Please suggest a suitable approach.
Try this hope this will help..
<apex:page controller="Add">
<apex:form >
Input
<apex:inputtext value="{!Input}"/>
Total
<apex:outputText value="{!Total}"/>
<apex:commandButton value="Get Sum" action="{!add}"/>
</apex:form>
</apex:page>
___________________________________________________
public class Add {
public Add()
{
Total=0;
}
public decimal Total{get;set;}
public decimal userValue=0;
public decimal getInput() {
return userValue;
}
public void setInput(decimal userValue) {
this.userValue= userValue ;
}
public void add() {
Total+=userValue;
}
}
All Answers
Try this hope this will help..
<apex:page controller="Add">
<apex:form >
Input
<apex:inputtext value="{!Input}"/>
Total
<apex:outputText value="{!Total}"/>
<apex:commandButton value="Get Sum" action="{!add}"/>
</apex:form>
</apex:page>
___________________________________________________
public class Add {
public Add()
{
Total=0;
}
public decimal Total{get;set;}
public decimal userValue=0;
public decimal getInput() {
return userValue;
}
public void setInput(decimal userValue) {
this.userValue= userValue ;
}
public void add() {
Total+=userValue;
}
}
Hi,
It worked like a charm , thanks a lot. Just a couple of more things that i wanted to ask here:
1. On click of add I wanna refresh components of input and output only , not the entire page.
2. Want to add another button "Clear Result" that resets the output as well as the input.
Can you please help me with these two requirements as well?
Thanks a lot!!!!!
Hey dude sorry for the late reply. To set Input and Total to 0 you need to use reRender in VF page.
I am posting the modified code check this out.
<apex:page controller="Add">
<apex:form >
<apex:pageBlock title="Add N Numbers" >
<apex:pageBlockSection >
Input
<apex:inputText value="{!Input}" id="a"/>
Total
<apex:outputText value="{!Total}" id="tot"/>
<apex:commandButton value="Get Sum" action="{!add}" reRender="a,tot"/>
<apex:commandButton value="Clear Result" action="{!clear}" reRender="a,tot"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
-----------------------------------------------------------------------------------------------------------------
public class Add {
public Add() {
Total=0;
}
public decimal Total{get;set;}
public decimal userValue=0;
public decimal getInput(){
return userValue;
}
public void setInput(decimal userValue) {
this.userValue= userValue ;
}
public void add(){
Total+=userValue;
}
public PageReference clear() {
userValue=0;
Total=0;
return null;
}
}