Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add simple jquery loader/spinner using ajax?

Tags:

jquery

ajax

I just need a little help here about adding a simple loader/spinner in my form. I have a search form in my jquery. I want to include a loader/spinner image while the search is on process. After the process is done the loader will be not visible again.

Here's my code, by the way I am using CodeIgniter.

Here's my code when the search button is clicked:

//search
        $('#search-btn').on('click',function(){

            var query = $("#keyword").val();

            var image = "<?php echo base_url()."/resources/loading/loading43.gif"; ?>";

            $('#loading').html(' + image + ');

            var query_url = "<?php echo site_url('item_controller/searchItem'); ?>";

            $.ajax({

                type:'POST',
                url: query_url,
                data:{query: $("#keyword").val()},
                dataType:'json',
                async: false,
                success:function(d){

                    //$('.display').dataTable().fnDestroy( true );

                    $("#example tbody").html(""); //HERE'S MY PROBLEM, DOESN'T DISPLAY LOADER WHEN SEARCHING

                    for(i in d){

                        $("#example tbody").append("<tr><td style='text-align: center; color:' data-code='TRUE'>" + d[i]['item_code'] + "</td><td style='text-align: left' data-name='TRUE'>" + d[i]['item_name'] + "</td><td><div style='text-align: center'><input type='button' value='ADD' class='k-button' id='" + d[i]['item_code'] + "' data-item=TRUE /></div></td></tr>");

                    }

                    //$("#search_result").show('blind');

                    $("[data-item]").on('click',function(){

                        var code = $(this).parents('tr').find('[data-code]').html();
                        var name = $(this).parents('tr').find('[data-name]').html();
                        // console.log(code,name);
                        $("#grid1 tbody").append("<tr><td style='text-align: center; width: 20%'><input type='text' value=" + code + " readonly style='width:50px; background-color: transparent; border-style: none' id=code" + counter_code++ +" /></td><td style='text-align: center; width: 40%'><input type='text' style='width: 90%; background-color: transparent; border-style: none' value='" + name + "' id=item"+ counter_item++ +" readonly /></td><td style='text-align: center'><input type='text' name='qty[]' id=qty"+ counter_qty++ +" style='text-align: center; width: 50px;' /></td><td style='text-align: center'><div align='center'><select style='width:100px; display: block' name='unit[]' id=unit"+ counter_unit++ +" ><option value=''>----</option><option value='pc/pcs'>PC/PCS</option><option value='BOX/BOXES'>BOX/BOXES</option></select></div></td><td style='text-align: center'><input type='text' name='price[]' id=price"+ counter_price++ +" style='text-align: right; width: 100px;' onblur='' /></td><td style='text-align: center'><input type='text' name='total[]' id=total"+ counter_total++ +" style='font-family: courier; text-align: right; background-color: lightgray; color: red; width: 100px;' readonly='readonly' value='' /></td></tr>");

                        ComputeTotal();

                    });

                    $('.display').dataTable({
                        "bPaginate": true,
                        "bLengthChange": true,
                        "bFilter": true,
                        "bSort": true,
                        "bInfo": true,
                        "bAutoWidth": false,
                        "bDestroy": true,
                        "bJQueryUI": false,
                        "bRetrieve": true,
                        "sPaginationType": "full_numbers",
                        "iDisplayLength": 25,
                        "destroy": true
                    });

                    //$('.display').dataTable().fnDestroy(true);

                    //$('.display').children().remove()

                },

            });    

        });

Here's the search form

<div id="loading"></div>

<div id="search_result" class="k-content">
     <div class="k-header" id="item-view-list" align="center">

        <table border="0" style="width: 80%; align: left" cellpadding="10" cellspacing="10" align="left">
            <tr>
                <td colspan="3">
                    <h5>SEARCH ITEM</h5>
                </td>
            </tr>
            <tr>
                <td style="width: 3%">
                    <label>Name/Description</label>
                </td>
                <td style="width: 40%">
                    <input type="text" name="keyword" id="keyword" style="width: 80%" /> <input type="button" value="SEARCH" id="search-btn" class="k-button" style="font-size: 12px" />
                </td>
            </tr>
        </table>

        <hr />

        <table cellpadding="0" cellspacing="0" border="0" class="display" id="example" style="font-size:small; width: 100%">
            <thead>
                <tr>

                </tr>
                <tr>
                    <th>CODE</th>
                    <th>NAME/DESCRIPTION</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>

            </tbody>
        </table>
        <br />

    </div>
</div>
like image 273
Jerielle Avatar asked Oct 17 '13 06:10

Jerielle


People also ask

How to add spinner in ajax call?

1). Create a spinner image or load it from ajax loader. 2). Place the image in our code where you want to show.

How to add loading spinner in jQuery?

Answer: Use the ajaxStart() and ajaxStop() MethodWhile working with Ajax, showing a loading spinner or displaying a message with some animation like "Loading... Please Wait" is popular way to indicate the user that Ajax request is in progress.

How to use loader in jQuery ajax?

The jQuery load() method is a simple, but powerful AJAX method. The load() method loads data from a server and puts the returned data into the selected element. Syntax: $(selector).load(URL,data,callback);

How to show loading image in ajax jQuery?

In not for only one ajax request, but if you want to load image on every ajax request then you can do that using bellow example. I use ajaxStart() and ajaxComplete() function for show image until All POST or GET Ajax request. this both function will execute one each $. ajax request.


2 Answers

You could do as,

HTML

<div class="loader">
   <center>
       <img class="loading-image" src="loading.jpg" alt="loading..">
   </center>
</div>

CSS

.loading-image {
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 10;
}
.loader
{
    display: none;
    width:200px;
    height: 200px;
    position: fixed;
    top: 50%;
    left: 50%;
    text-align:center;
    margin-left: -50px;
    margin-top: -100px;
    z-index:2;
    overflow: auto;
}

In you js

Before the ajax call as,

$.ajax({
  // your ajax code
  beforeSend: function(){
       $('.loader').show()
   },
  complete: function(){
       $('.loader').hide();
  }
});
like image 76
Ajith S Avatar answered Sep 30 '22 02:09

Ajith S


try this code.

$.ajax({
    type:'POST',
    url: query_url,
    data:{query: $("#keyword").val()},
    dataType:'json',
    async: false,
    success:function(d){
        // Your code.
    },
    beforeSend: function(){
        // Code to display spinner
    },
    complete: function(){
        // Code to hide spinner.
    }
});

refer this link to get more details. Ajax Events

like image 31
Sohil Desai Avatar answered Sep 30 '22 02:09

Sohil Desai