Friday, March 18, 2011

Loading animation on Visualforce

Maybe its just be but even if you build the most amazing system in the world, it won't be awesome if it is not visually pleasing or beautiful UI. No font should not be uneven, or the images should not haywire. Everything should be in order. Perfect.

A simple script can save you a long email, informing you that a million dollar system is broken because it does nothing when users click the buttons. If you have a long processing happening in the background, its not good to assume your users are psych to know whats happening in background (if only we could see data packets send one by one).  If you do not show them that something is happening, they will end up clicking the button again and again, sending multiple request to the server. Worst case scenario your system will be returned back with a thank you note that excel sheet was better, this is broken. Have no fear, I am here.

If you have been following this blog for some time, I hope you do, you will know that 'Please wait' animation was one of the early scripts I published on this blog. The code was big, large and complex, maybe my understanding of VF was a little less back them (it was way back in 2009) or maybe my understand of VF is much better now, I found a optimized and simple way of doing it and using it on my projects.

<apex:actionstatus id="pleasewait">
    <apex:facet name="start">
        <img src="{!$Resource.AjaxAnimation}" />
    </apex:facet>
</apex:actionstatus>


The please wait image should be loaded in static resources with name 'AjaxAnimation'. You can download some nice images I collected for my own projects. This simple code will help you prevent that unpleasant email on a Monday morning that will spoil the whole day.

The best way to do it is make a custom component and keep it handy on all the pages, just add the component to the page. Working on a generic pagination component next, download and add and you have pagination for yourself.

Update: Thanks to Tim Inman, found an amazing site which gives you different Ajax Load images in different background and colors. Do visit http://ajaxload.info/