Define the Visualforce component
Let’s create a Visualforce component that will hold the loading icon on top of a div that will hide the page when loading is triggered. You don’t need any static resource for the icon since we are going to use standard Salesforce icons. Go to Setup -> Develop -> Component -> New. Name it “LoadingBox” and copy-paste this code:
<apex:actionStatus onstart="startLoading();" onstop="endLoading();" id="loadStatus"/>
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=30)";
filter: alpha(opacity=30);
background: url('/img/loading32.gif') scroll no-repeat 0 0;
<div id="load_scrl" class="loadingBox loader" style="display:none"> </div>
<div class="loadingBox overlay"> </div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$('#load_scrl').css('top', $(document).scrollTop() + 200);
Integrate the component to your page
Now let’s say you have a Visualforce page with an ajax action, here is 2 ways to use this component but first add this code to the root of your visualforce page:
<c:loadingbox />
The first and simplest option is to use the “status” attribute with the value “loadStatus”, for example if you have a commandbutton it will look like that:
<apex:commandButton action="!save}" value="AjaxButton" status="loadStatus" rerender="myForm"/>
It will start the loading animation with the Salesforce icon and then on finish the animation will stop and disappear.
The second option if you’re not using standard Visualforce components is to launch and stop the animation from the javascript. It is 2 simple calls:
|
startLoading();
endLoading()
|
The advantage of using those options is that you are improving the user experience, your loading animation will look like a Salesforce one and finally it is really easy to use.
Comments
Post a Comment