function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
shrey.tyagi88@tcs.comshrey.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.

 

Best Answer chosen by Admin (Salesforce Developers) 
LokendraLokendra

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

LokendraLokendra

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;
     }
}

This was selected as the best answer
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com

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!!!!!

LokendraLokendra

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;
   }
}