0

I have a very simple VF page that calls a method in my controller to populate a selectoptions. The method in my controller calls an external API so it takes a bit of time to populate selectoptions. The page and controller works great. I am however having trouble showing loading gif.

I want to show loading gif as soon as onchange happens on my inputField Type__c and have it go away when the selectedoptions has been populated.

I've tried to use actionsupport and also tried setting a boolean in my controller as soon as makecall fires to use that to render the gif image.

<apex:page standardController="myObject__c" extensions="myController" >
    <apex:pageMessages />
    <apex:form >   
        <apex:actionFunction action="{!makeCall}" name="makeCall" rerender="editsection"/>
        <apex:pageBlock title="New Manifest Item" mode="edit">

        <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>

                <apex:image id="loadinggif" value="/img/loading.gif" />
            </apex:pageBlockButtons>

            <apex:pageBlockSection id="editsection" title="New" columns="1">       
                <apex:inputField value="{!myObject.Type__c}" onChange="makeCall()" />  
                <apex:pageBlockSectionItem >
                <apex:outputLabel value="Item Name"/>
                    <apex:selectList value="{!selectedVal}" size="1" multiselect="false">
                       <apex:selectOptions value="{!optns}"/>
                    </apex:selectList>
                    </apex:pageBlockSectionItem>
                <apex:inputField value="{!myObject.Action__c}"/>  
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

makecall() in controller looks like this. Note showgif is public gloabl variable with getter and setter

public void makecall(){
  showgif = true;
  //handle selectoptions
  showgif = false;
}

2 Answers 2

0

Apex is rendered "server-side", so only the results of the method called will be visible. In other words, you can't toggle the Boolean value twice and have it update in the browser in real-time while making your callout.

You can use the actionStatus element, and actionSupport to actually make the render the image. I've modified your page as an example (you may need to tweak it slightly). Notice the use of apex:actionStatus to show something while loading, and the status attribute of your actionFunction to associate that loading status with the actionFunction.

You could have also used actionSupport on the inputField itself to achieve a similar result.

<apex:page standardController="myObject__c" extensions="myController" >
    <apex:pageMessages />
    <apex:form >   
        <apex:actionFunction action="{!makeCall}" name="makeCall" rerender="editsection" status="loadingGif" />
        <apex:pageBlock title="New Manifest Item" mode="edit">

        <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:actionStatus id="loadingGif">
                <apex:facet name="start">
                <apex:image id="loadinggif" value="/img/loading.gif" />
                </apex:facet>
                </apex:actionStatus>
            </apex:pageBlockButtons>

            <apex:pageBlockSection id="editsection" title="New" columns="1">       
                <apex:inputField value="{!myObject.Type__c}" onChange="makeCall()" />  
                <apex:pageBlockSectionItem >
                <apex:outputLabel value="Item Name"/>
                    <apex:selectList value="{!selectedVal}" size="1" multiselect="false">
                       <apex:selectOptions value="{!optns}"/>
                    </apex:selectList>
                    </apex:pageBlockSectionItem>
                <apex:inputField value="{!myObject.Action__c}"/>  
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
0

You can try actionstatus in the following way, style as per your need and use any loading imahe that you prefer

<apex:actionstatus id="loadingGif">
                <apex:facet name="start">
                    <div id="salesforceSource_blurybackground" style="background-color:black;opacity: 0.20;filter: alpha(opacity = 20);position: absolute;width: 100%;height: 100%;top: 0;left: 0;z-index: 9998;padding-bottom:2000px;"></div>
                    <div id="ManageMembersViewport_loading" class="waitingSearchDiv" style="width: 100%; height: 80%; display: '';">
                        <div style="width: 144px;vertical-align: middle; margin-top: 60%; margin-left: 40%;" class="waitingHolder">
                            <img src="{!URLFOR($Resource.LoadingImage, '/LoadingImage/loader_large_blue100x100.gif')}"/>

                        </div>
                    </div>
                    <div id="ManageMembersViewport_loading" class="waitingSearchDiv" style="width: 100%; height: 100%; display: ''; "></div>
                </apex:facet>
            </apex:actionstatus>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .