Neeraj Kumar Gupta Neeraj Kumar Gupta - 6 days ago 7
jQuery Question

How to show loading image (animated .gif file) inside autocomplete textbox using MVC3

I have implemented Autocomplete textbox in my asp.net MVC3 application, but I am unable to show loading image inside textbox during search.
My code is


Javascript


<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.3.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">

$(document).ready(function () {
$("#txtPONO").autocomplete({
source: "/Transection/GetPONO",
minLength: 2,
select: function (event, ui) {
$("#hdnPurContID").val(ui.item.id);
}
});
});


</script>



View


<div style="float: left; padding: 9px 5px 5px 5px; width: 80px; vertical-align: middle"
class="fieldHead">
Enter @Html.LabelFor(l => l.PurchaseContract.AOPLPONO)
</div>
<div style="float: left; width: 200px; padding: 5px 0px 5px 0px;">
@Html.TextBoxFor(m => m.PurchaseContract.AOPLPONO, new { @id = "txtPONO", @class = "textbox", @style = "width:100%" })
</div>
<div style="float: left; width: 50px; padding: 4px 0px 0px 5px;">
<img alt="search" src="../../Content/images/Search.png" />
@Html.HiddenFor(model => model.PurchaseContract.PurContID, new { @id = "hdnPurContID" })
</div>


I want to show a small loading image inside textbox on right side

Answer

Add a css class like this.

.loading
{
    background:url('path to your image') no-repeat right center;
}

Now trigger to the search option and open options for autocomplete

$("#txtPONO").autocomplete({
        source: "/Transection/GetPONO",
        minLength: 2,
        select: function (event, ui) {
            $("#hdnPurContID").val(ui.item.id);
        },
        search: function(){$(this).addClass('loading');},
        open: function(){$(this).removeClass('loading');}

    });

OR

Even better solution would be just using the css class

.ui-autocomplete-loading { background:url('img/loading.gif') no-repeat right center }

jQuery ui-autocomplete-loading automatically runs the loading image for the duration of loading

Hope it helps

Comments