+ Start a Discussion
ThomasTTThomasTT 

Can't use pageBlockTable 'var' variable in commandButton onComplete

I'm trying to use commandButton onComplete in pageBlockTable and I'd like to use 'var' variable in the onComplete, but I can't use 'var' variable in onComplete. I can use 'var' variable in onClick, but not in onComplete.

 

For example,  I define 'var' variable "element", which is one of the elements in array "list". In onClick, I can use {!element}, but when I code {!element} in onComplete, VF editor tries to correct me to implement "OnCompleteTestControler.element" property, which means that it doesn't allow me to use 'var' variable in onComplete.

 

Is it the specification? or a bug? (or don't I understand VF at all?)

 

ThomasTT

 

[VF page]

<apex:page controller="OnCompleteTestController"><apex:form ><apex:pageBlock > <apex:pageBlockTable var="element" value="{!list}"> <apex:column> <apex:facet name="header">Name</apex:facet> <apex:outputText value="{!element}"/> </apex:column> <apex:column> <apex:facet name="header">Button for onClick</apex:facet> <apex:commandButton value="click me for {!element}" onClick="alert('{!element}');"/> </apex:column> <apex:column> <apex:facet name="header">Button for onComplete</apex:facet> <!-- Not allowed <apex:commandButton value="click me for {!element}" onComplete="alert('{!element}');"/> --> <apex:commandButton value="click me for {!element}" onComplete="alert('No var variable!');"/> </apex:column> </apex:pageBlockTable> </apex:pageBlock></apex:form></apex:page>

 

 

 [Controller]

 

public class OnCompleteTestController { public String[] getList() { return new String[]{'alpha', 'beta', 'gamma'}; } }

 

 

 

 

nnewbold-sfdcnnewbold-sfdc

Hi Thomas,

This does seem a bit buggy to me as well.  However, I suspect this is expected behavior based on the placement of the oncomplete.  Since it is part of a collection, it's possible that the reference can't be made as a child element.  If you feel this is a bug, please log a ticket with support for further investigation.

 

Below is a workaround for doing what you want to do.  The button sets a value in the controller (itemClicked), which can later be referenced via a getter at the row level.

 

<apex:page controller="OnComplete">
    <apex:form >
        <apex:pageBlock >
            <apex:pageblockTable value="{!list}" var="item">
                <apex:column >{!item}</apex:column>
                <apex:column >
                    <apex:commandbutton value="{!item}" action="{!doNothing}" rerender="nothing" oncomplete="alert('{!itemClicked}');">
                        <apex:param assignTo="{!itemClicked}" value="{!item}" />
                    </apex:commandbutton>
                </apex:column>
            </apex:pageblockTable>
        </apex:pageBlock>
        <apex:outputPanel id="nothing"></apex:outputPanel>
    </apex:form>
</apex:page>

 

public class OnComplete
{

    public String itemClicked { get; set; }
    public String[] getList()
    {
        return new String[]{ 'red', 'green', 'blue' };
    }
   
    public PageReference doNothing()
    {
        return null;
    }

}

ThomasTTThomasTT

Hi nnewbold-sfdc,

 

Thank you for your response.

Somehow, your code doesn't work properly (all buttons pupup 'alpha' or '')... maybe it's because the timing of calling Apex, reRendering page, and executing onComplete.

 

For me, I found a workaround. I used 'onClick' to call actionFucnction javascript, and I implemented onComplete in the actionFunction. I can use 'var' variable in onClick, and I can use onComplete in actionFunction.

 

Functionally, I think it's a bug, but as you said, I understand the difficulty to make it work... so I'll leave this as it is (and I don't know how to log a ticket...)

 

Thank you anyway.

 

ThomasTT

reatlimecoreatlimeco
Note that you must set the parameter "name" in the apex:param for the assignto to work.   I ran into this problem to.