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
1542VeMan1542VeMan 

Need help with ActionFunction call using a table variable as the parameter

I am using a wrapper class to combine an sObject with a checkbox, then iterating over the wrappers in my list. The first column displays the checkbox, then a repeat tag iterates over a list of strings derived from the object fieldNames, allowing the table to render columns according to the object's fieldSet.

the 'fieldSetList' is a list of field Names and I'm using a dynamic APEX process so that the client can reconfigure their table by changing the value and their order in the field set on the custom object.

The table displays correctly, however I want to be able to sort the table by clicking on the column. To do this, I have tried to add an onclick call to an ActionFunction ('sortCLTable') to the column and sending the repeat tag's interation variable ('f') as a parameter: 'onclick="sortCLTable(f)"'

The ActionFunction's parameter should set the sortValue property in the controller, and run the sorttable method, but alas it does not. The table columns have no response whatsoever.
 
​<apex:page ...> 
<apex:form> 
   <apex:outputPanel id="thePanel">
       <apex:pageBlockTable value="{!CLWraps}" var="CLWrap"> 
             <apex:column headervalue="Select" > 
                  <apex:inputCheckbox value="{!CLWrap.selectbox}"/> 
             </apex:column> 
             
             <apex:repeat value="{!fieldSetList}" var="f" > 
                  <apex:column value="{!CWrap.CL[f]}" onclick="sortCLTable(f)"/> 
                     // I've also tried sortTable('f'), sortCLTable({!f}) and sortTable('{!f}')
             </apex:repeat> 

        </apex:pageBlockTable> 
    </apex:outputPanel> 
    
    <apex:actionFunction action="{!sortTable}" name="sortCLTable" reRender="thePanel">          
          <apex:param name="firstParam" assignTo="{!sortValue}" value="" /> 
    </apex:actionFunction> 
  </apex:form> 
</apex:page>

CONTROLLER contains:

public string sortValue {get; set;} 
public pageReference sortTable(){ 
    resetQueryWithSortValue(sortValue); 
    return null; 
}

Although the 'f' is working to set the column header and reference the correct field value. it doesn't appear to be passing through the onclick call as a parameter to the apex:Param value associated with the function, or to the controller's corresponding 'sortValue' property. I think I've written the page tags correctly...

Using ActionSupport instead appears to mess up the repeat function such that the column names no longer appear - not sure why. Neither do the columns sort. 

I can add more code from my example if needed, but figured this would describe the problem. Any help is much appreciated.

Thanks
jp1234jp1234
What do you see as the request on the browser's developer console when you click that column?  Also, what do you see as the value for sortValue when you put a System.debug(sortValue); statement in your sortTable() method?
1542VeMan1542VeMan
It doesn't appear that the columns are active at all. Neither does a transaction take place that allows me to check a debug log. It looks like the process is working to derive the correct fieldname, but in the Javascript function call, its meaningless. 

When I use the 'f' variable in a Visualforce reference with {!}, the variable renders correctly as the string. For example, I can use this to get the header value:
<apex:column headervalue="{!f}">
      <apex:outputField value="{!CWrap.CLM[f]}"/>
</apex:column>

However, I can't use the f value as a parameter to the sortTable function - none of these work: onclick="sortCLTable(f)", onclick="sortCLTable('f')",onclick="sortCLTable({!f})", onclick="sortCLTable('{!f}')"

So, what's the best way to get the table variable to render as the string it represents into the Javascript function calls, 
1542VeMan1542VeMan
I've also tried the actionSupport tag:
 
<apex:repeat value="{!fieldSetList}" var="f">
    <apex:column headerValue="{!f}" ><!---->
        <apex:actionSupport action="{!sortCLTable}" reRender="thePanel" event="onclick">
            <apex:param name="sortValue" value="{!f}" assignTo="{!sortValue}"/>
        </apex:actionSupport>
        <apex:outputField value="{!CWrap.CLM[f]}"/>
    </apex:column>
                        
</apex:repeat>
This saves, and the columns and field values render, but still the columns on the rendered table are inert. I've removed the need to call a specific Javascript function, and I would think that the value attribute in the apex:param should be going through, but since the column is still not live no value is getting sent to the controller - actually nothing is happening so there is no debug log to check. 

I can't help but feel I'm missing something obvious...
jp1234jp1234
Can you change the following line 
<apex:column value="{!CWrap.CL[f]}" onclick="sortCLTable(f)"/>

to
<apex:column value="{!CWrap.CL[f]}" onclick="sortCLTable(\"{!f}\")"/>

see what happens? 
1542VeMan1542VeMan

<apex:column value="{!CWrap.CL[f]}" onclick="sortCLTable(\"{!f}\")"/> caused complaints about not finding the column's end tag (/>). 

I found a way to get the sorting function to work using an ActionSupport, but its not obvious. Unfortunately, I still don't have an answer to the question about the actionFunction not being called by the onclick attribute on the column, or the provided parameter not being set in the controller property. I should say that I'm looking to use an apex:repeat variable attribute rather than a table variable, but I haven't seen any suggestion that this would be the issue. 
I hope to be able to understand the actionFunction and its limitations in situations like these when one is unable to find the correct syntax to add a variable value into the apex:param value attribute used in an actionFunction's action method, when called from another component's onclick event.
Its like you have to take a value understandable in Visualforce, convert into something a javascript method call can understand, then reconvert it back to a Visualforce param attribute value. Is this a situation where Visualforce remoting is required??

Would love for someone to suggest the right solution, but thanks @jp1234 for your kind assistance!!

Anyway, the solution to my problem (although not the answer to my question) is to use the actionSupport tag. If the question were about how to get the columns of a table to sort, this would be a good answer:
 
<apex:pageBlockTable value="{!CLMContracts}" var="CWrap"> 
   <apex:column headervalue="Select" > 
      <apex:inputCheckbox value="{!CWrap.selectbox}"/> 
   </apex:column> 
   <apex:repeat value="{!fieldSetList}" var="f"> 

      <!--apex:outputPanel--> //this isn't allowed 
      <apex:column>   
         <apex:facet name="header">//facet applies all its inner component actions to the parent column tag  
            <apex:outputPanel > 
               <apex:actionSupport action="{!sortCLTable}" reRender="thePanel" event="onclick"> 
                  <apex:param name="sortValue" value="{!f}" assignTo="{!sortValue}"/>   
                  // now, the repeat's var value is being assigned to the sortValue property 
               </apex:actionSupport> 
               {!f} //repeat's var value is applied to the column header 
            </apex:outputPanel> 
          </apex:facet> 
        <apex:outputField value="{!CWrap.CLM[f]}"/>   
      </apex:column> 

    </apex:repeat> 
</apex:pageBlockTable>

 
jp1234jp1234
Oh, right.  I just realized that you were trying to sort using onclick method on the column, which would apply to individual cells of the table EXCEPT for the header and footer, so it does make sense that you are having trouble making "f"  visible at the column level.  You are absolutely right to use the header facet for doing that in your solution.  However, I believe you don't have to use actionSupport along with header facet.  People have used commandLink, outputText with onclick action, etc. to achieve that as well.  

http://www.sundoginteractive.com/sunblog/posts/a-recipe-for-column-sorting-salesforce-visualforce-page
http://bobbuzzard.blogspot.ca/2014/09/sorting-visualforce-tables-with.html