5

I am using MVC3 and displaying my data in a webgrid. I would like to display loading indicator (loading image) displayed when I filter/search. What is the best approach?

My search filter (code):

@using (Html.BeginForm())
{
     <fieldset  id="fieldset1" class="coolfieldset">

        <legend>Search for Towers Watson Subscribers/Contacts</legend>
        <div class="div-table">
        <div class="div-table-row">
            <div class="div-table-col">Reg Date:</div>
            <div class="div-table-col"><input id="regDateFrom" class="datepicker" name="regDateFrom" value="@regDateFrom" type="text" /> to <input id="regDateEnd" class="datepicker" value="@regDateEnd" name="regDateEnd" type="text" /></div>
        </div>
        <div class="div-table-row">
            <div class="div-table-col">Profile Mod Date:</div>
            <div class="div-table-col"><input type="text" id="profileModDateFrom" class="datepicker" value="@profileModDateFrom"  name="profileModDateFrom" /> to <input id="profileModDateEnd" class="datepicker" value="@profileModDateEnd" name="profileModDateEnd" type="text" /></div>
        </div>
        <div class="div-table-row">
            <div class="div-table-col">Last Name:</div>
            <div class="div-table-col"><input type="text" id="lastName" name="lastName" value="@lastName" /></div>
        </div>
          <div class="div-table-row">
            <div class="div-table-col"><input id="search" name="search" type="submit" value="Search" /></div>
            <div class="div-table-col"></div>
        </div>
        </div>      
    </fieldset>
}
{@Html.Partial("List_Ajax", Model)}
Martin Buberl
  • 45,844
  • 25
  • 100
  • 144
Chaka
  • 1,709
  • 11
  • 33
  • 58

1 Answers1

6

http://www.ajaxload.info/ lets you create a nice loading gif. Create an image, and place it in a div as below. Then bind the search button with jQuery to display the hidden div when clicked.

Place the following div where you would like the loading icon to appear.

<div id="loadingDiv" style="display:none"><img src="loading.gif"></div>

Then this in your Javascript file

$(document).ready(){
    $('#search').click(function(){
        $('#loadingDiv').show();
    });
});

Then when you're done loading, simply:

function SomeCallBackEvent(){
    $('#loadingDiv').hide();
};
Ryan Amies
  • 4,902
  • 1
  • 21
  • 36
  • Thank you, exactly what I needed! – Chaka Nov 29 '11 at 05:50
  • @RyanAmies For us n00bs here, how does one generally know when loading is complete given that items are being added to the grid? Could you provide a simple example to extend this answer? Thank you. – Rachael Nov 06 '14 at 17:59
  • It really depends on your code. If you're loading it with jQuery/Ajax then you will have a callback for `Complete` which you can leverage. If you're stuck I'd suggest posting a new question specific to your situation. – Ryan Amies Nov 07 '14 at 12:53