+ Start a Discussion
B2000B2000 

Setting Field Focus with ActionFunction and $Component

I have a commandLink that adds a new item to the bottom of the table.  When I reRender the table, it refreshes to the top and I would like to set the focus to the first field on the new item.  I tried using the actionFunction in conjunction with a javascipt to set the focus but no luck as I'm unsure how to provide the last item's id to the actionFunction. I was successful in creating an inputfield below the table with the "bottom" id and set the actionFunction focus there but it is a workaround.  I appreciate any assistance.

 

    <apex:form id="PCOForm">               
    <div class="header" id="header">  
        <div class="leftcol">...</div><!-- END leftCol --> 
         <apex:outputPanel id="templatePanel" >…</apex:outputPanel>   
    </div><!-- END header -->    

    <div class="mainContent" id="mainContent">    
    <apex:outputPanel id="PCOPanel">        
        <div class="singleCol" id="singleCol">       
        <div class="titleBar">…</div><!-- END titleBar -->      
        <div class="content" id="content">
        <div class="scrollContentView" id="scrollContentView">
        <apex:actionFunction name="resetFocus"  focus="theFocus" reRender="PCOForm"/>
               
        <apex:variable var="row1Color" value="#f1f1f1"/>
        <apex:variable var="row2Color" value="#FFFFFF"/>
        <apex:variable var="row3Color" value="#8CB6D7"/>
        <apex:variable var="btblue" value="#21449c"/>

        <apex:outputPanel id="PCOTblPanel">        
        
        <table cellpadding="0" cellspacing="0" class="list"  border="0" id="PCOTbl">
            <tr id="TblHdr1">...</tr>
        
            <apex:variable var="cntRow" value="{!0}"/>
            <apex:repeat value="{!wPChangeOrderList}" var="PCO">
            <apex:variable var="cntRow" value="{!cntRow+1}"/>
            
            <tr id="TblRow1">                    
                <td style="width:1%;">.../td>      
                <td style="text-align:left;width:19%;" id="TblRow1Col2">
                    <b>
                    <apex:inputField styleClass="inputText" value="{!PCO.PChangeOrder.Name}" rendered="{!IF(pageType!='View',true,false)}" id="TblRow1Col2Name"/>
                    <script> var theFocus = document.getElementById(“{!$Component.TblRow1Col2Name}”); </script>
                    <apex:outputField value="{!PCO.PChangeOrder.Name}" rendered="{!IF(pageType='View',true,false)}"/>
                    </b>
                    <apex:outputText value="{!PCO.errorMsg}" style="color:red;text-weight:bold;" rendered="{!IF(ISNULL(errorMsg),false,true)}"/>                     
                </td>
…..
        </table>
        <apex:inputText id="bottom" style="width:0px;height:1px;"/>
…..                
        <apex:commandLink value="New Item" action="{!newChangeOrderItem}" styleClass="new" rendered="{!IF(isChangeOrderItemSelected,true,false)}" 
                    oncomplete="resetFocus();"  >
                    <apex:param assignTo="{!PropertyChangeOrderId}" value="{!PropertyChangeOrderId}" name="PropertyChangeOrderId"/>                                        
                    <apex:param assignTo="{!rowIdx}" value="{!rowIdx}" name="rowIdx"/>  
                    <apex:param value="Edit" assignTo="{!pageType}" name="pageType"/> 
                </apex:commandLink> 

 

 

 

Salesforce WizardSalesforce Wizard

This might just be a timing issue.

 

I'm guessing that the focus is updating before the re-render occurs.

 

Have you tried splittling the re-render separate from the focus?

 

Maybe have two action functions? One that does the re-render and then oncompletes calls the second action function that does the actual focus?

 

Or do the rerender from the command link and call the reset focus on complete... You'll need to remove the re-render from the resetfocus too.