+ Start a Discussion
ScriptMonkeyScriptMonkey 

Passing Values to Apex using Buttons inside Repeat Loops?

I had a problem recently which was solved with the non-iterative example I provided:

 

 

    <apex:form id="frm">
    <apex:commandButton action="{!sendValue}" value="Test">
        <apex:param assignTo="{!value}" value="4" />
    </apex:commandButton>
    </apex:form>

This code wasn't passing the value to the function, but adding reRender="frm" to it made it work.  I'm still not entirely sure why, but when the commandButton reRendered the form, it sent the value correctly.

 

The problem is that the values i have are in a list, not static, so my actual code will, likely, have to be one of the following:

 

    <apex:form id="repeatFrm">
	    <apex:repeat value="{!numbers}" var="number">
			<apex:commandButton action="{!sendValue}" value="TestChain" reRender="repeatFrm">
					<apex:param assignTo="{!value}" value="{!number}" />
			</apex:commandButton>
			<br/>
		</apex:repeat>
	</apex:form>

 

or

 

 

    <apex:repeat value="{!numbers}" var="number">
    	<apex:form id="repeatFrm2">
			<apex:commandButton action="{!sendValue}" value="TestChain" reRender="repeatFrm2">
					<apex:param assignTo="{!value}" value="{!number}" />
			</apex:commandButton>
			<br/>
		</apex:form>
	</apex:repeat> 

 

To save you time, the difference is the form name inside the loop, or around it.  Either way, when I click a button, it doesn't pass a value, and the integer remains Null (known via debug logs).

 

 

The method, just so you can not worry, is excessively simple, IE:

 

	public void sendValue()
	{
		System.Debug('test is: '+value);
	}

 

 

If anyone has any insight, please let me know how I could go about this?

 

The end goal is to offer a list of options, some "claimed" and some still "unclaimed".  I can make it show everything correctly, but the claim buttons are formed as above, so I want someone to be able to click one and it will know WHICH one was clicked (IE the incrementing number) but since which are available always changes, the list has to be made by apex, can't be hard-written into the VF page.

 

Thanks in advance, as I'm sure this is, again, likely simple that I'm missing.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma

Please try this :

 

 

<apex:page controller="testPage" >
    <apex:form id="repeatFrm2">
        <apex:outputPanel id="op">
            Value : {!value}
        </apex:outputPanel>
        <apex:repeat value="{!numbers}" var="number">
            <apex:commandButton action="{!sendValue}" value="TestChain" reRender="op" immediate="true" oncomplete="alert('Completed');">
                <apex:param name="param1" assignTo="{!value}" value="{!number}" />
            </apex:commandButton>
            <br/>
        </apex:repeat> 
    </apex:form>
</apex:page>

 

 

All Answers

Bhawani SharmaBhawani Sharma

Please try this :

 

 

<apex:page controller="testPage" >
    <apex:form id="repeatFrm2">
        <apex:outputPanel id="op">
            Value : {!value}
        </apex:outputPanel>
        <apex:repeat value="{!numbers}" var="number">
            <apex:commandButton action="{!sendValue}" value="TestChain" reRender="op" immediate="true" oncomplete="alert('Completed');">
                <apex:param name="param1" assignTo="{!value}" value="{!number}" />
            </apex:commandButton>
            <br/>
        </apex:repeat> 
    </apex:form>
</apex:page>

 

 

This was selected as the best answer
Bhawani SharmaBhawani Sharma

Is this work for you ?

WesNolte__cWesNolte__c

Hey,

 

Two best practices are required here:

 

1. Never use more than 1 form in a page, using ActionRegion to split a form into parts. NOTE: everything in a single form is submitted unless you use ActionRegion to split the areas apart.

 

<apex:form id="repeatFrm">
	    <apex:repeat value="{!numbers}" var="number">
<apex:actionRegion>
			<apex:commandButton action="{!sendValue}" value="TestChain" reRender="repeatFrm">
					<apex:param assignTo="{!value}" value="{!number}" />
			</apex:commandButton>
			<br/>
</apex:actionRegion>
		</apex:repeat>
	</apex:form>

2. There is a bug in commandButton where it passes NULL values for Params. Try changing the CommandButton to a CommandLink just to troubleshoot. You can make a commandlink look like a button by assigning a styleClass of "btn".

 

Wes

Bhawani SharmaBhawani Sharma

Hi,

 

I think there is no bug with the command button as you can try  code in my previous post for this issue.

You can pass the values from the <apex:Param component. it works perfect.

 

The only thing is that <apex:param used in those cases where we don't want to submit the complete form only wants to set some specific properties. So it is supposed that if you are using <apex:param with command button, , intead of refrshing the whole page try to rerender a particular section . This will work.

WesNolte__cWesNolte__c

Hello,

 

There is a bug, the "reRender" attribute is a work-around, as is the commandLink option. Go ahead and try this:

 

 

public class mine{
    public String a{get;set;}

    public PageReference m(){
        System.debug(a);
        return null;
    }

}

 

<apex:page controller="mine">

    <apex:form>
    
        <apex:commandButton value="go" action="{!m}">
            <apex:param assignTo="{!a}" value="Hi"/>
        </apex:commandButton>
    </apex:form>

</apex:page>

 

 

 

Check your debug logs and "a" = "null".

 

To submit part of a form you should be using ActionRegion. It's use is detailed here: http://www.tgerm.com/2010/09/visualforce-actionregion-deep-dive.html

 

Wes

ScriptMonkeyScriptMonkey

Sorry I didn't get back to you last night, I had to run out.

 

Again, Tech Force, your solution worked, but I am still confused as to why.

 

Is your method to solving these problems as simple as "make them re-render anything."?

 

I want to make sure I understand.  From the sounds of what you're saying when you explain it, if you don't have a re-render, it will make the entire page reload, and will not send the param right, but if you have it rerender something, it will NOT reload the page, and thus the button works?  Am I understanding you right?

WesNolte__cWesNolte__c

Hi,

 

It's not the way the button is supposed to work but there is a 3-year old bug in CommandButtons. The 2 major work-arounds (there are others) for the bug are listed by Mr T-Force and myself. Which one you use depends on your situation.

 

Pretty confusing stuff eh? :)

 

Wes

Bhawani SharmaBhawani Sharma

I can understand , but what i am saying is

The basic functionality of a command button is set all the values from the page to the controller.This will be a submit request.

In that case you don't need to set the values using apex:param.Setters will be automatically get called.

 

<apex:param  component is used when we just want to set some specific properties and wants to validate data and valiadte something on the page.

That's why it is recommended to use reRender attribute to refresh a particular area on the page instead of the complete page submission.

ScriptMonkeyScriptMonkey

 


Tech Force wrote:

I can understand , but what i am saying is

The basic functionality of a command button is set all the values from the page to the controller.This will be a submit request.

In that case you don't need to set the values using apex:param.Setters will be automatically get called.

 

<apex:param  component is used when we just want to set some specific properties and wants to validate data and valiadte something on the page.

That's why it is recommended to use reRender attribute to refresh a particular area on the page instead of the complete page submission.


 

I wanted to thank you.  I got my orginal project finished thanks to your help.  It seems there were two things I wasn't doing.  One, I needed to re-render something, and two, the params didn't work without "name" being set, even though it wasn't used anywhere.  I added the rerender and still nothing worked, so I added the Name attribute to the params, and everything, finally, works as expected.

 

Thank you so much for all your help