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
sf ostsf ost 

increment the value when button is clicked

Hi all,

When ever the button "Click" is clicked. The value should increment to 0,1,2,3,4,...etc,. Should not use JavaScript. Use only controller and vf page.

I have tried but, no value is incrementing. Once go through the below code.

VF Code:
<apex:page showHeader="false" controller="increment">
<apex:form >
 <apex:commandButton value="Click" action="{!display}"/>
</apex:form>
</apex:page>

Apex Class:
public class increment{

    integer numb;
    integer i;

    public void display(){
    
       numb = 100;
       
    for(i=0; i<numb; i++){
        i=i+1;
        System.debug(i);
        
    } 
    
    }

}

What is wrong in my code?
Tavva Sai KrishnaTavva Sai Krishna
BALAJI CHBALAJI CH
Hi sf ost,

You are already incrementing value of 'i' in for loop, there is no need to increment again.
Please find below revised code and let me know if that helps you.
public class increment{
    
    integer numb;
    integer i;
    
    public void display()
    {
        numb = 100;
        
        for(i=0; i<numb; i++)
        {
            System.debug(i); 
        } 
    }
}

Best Regards,
BALAJI
BALAJI CHBALAJI CH
You can see the values being incrementing in Debug Logs.
 
sf ostsf ost
i need to display value on vf page.. :/ @Balaji CH
BALAJI CHBALAJI CH
Please find below revised code. Let me know if that helps you.

VF Page:
<apex:page showHeader="false" controller="increment">
<apex:form id="refresh" >
 <apex:commandButton value="Click" action="{!display}" reRender="refresh" />
    
    <apex:pageBlock>
        <apex:pageBlockSection>
            <apex:pageBlockTable value="{!myList}" var="temp" rendered="{!disp}" >
                <apex:column value="{!temp}"/>
            </apex:pageBlockTable>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

Controller:
public class increment{  
    integer numb;
    integer i;
    public list<integer> myList = new list<integer>();
    public boolean disp {set;get;}
    
    
    public list<integer> display()
    {
        disp = true;
        numb = 100;
        
        for(i=0; i<numb; i++)
        {
            myList.add(i);
        }
        system.debug(myList);
        return null;
    }
    
    public list<integer> getmyList() {
        return myList;
    }
}

Best Regards,
BALAJI
BALAJI CHBALAJI CH
Hi,

Does this solve your question ?

Best Regards,
BALAJI
Ravi Dutt SharmaRavi Dutt Sharma
Page : 
 
<apex:page showHeader="false" controller="IncrementCtrl">
<apex:form >
	Number : {!numb}
	<apex:commandButton value="Click" action="{!display}"/>
</apex:form>
</apex:page>

Controller :
 
public class IncrementCtrl{

    public Integer numb{get;set;}
	
	public void IncrementCtrl(){
		numb = 100;
	}
    public void display(){
		numb = numb + 1;
    }

}