AJAX Search box queries twice

AJAX Search box queries twice

nin_boundnin_bound Posts: 10Questions: 6Answers: 1

So when I type in the search box, the datatable draws on the very first letter instead of waiting. I finishing typing my search and the wait finished, but the table draws again.

I'm loading from the server.

How do I reduce it to one draw instead of two? I was thinking about going into the search event and code in something where if the search box contains less than two characters, do not search.

DataTables 1.10.12

var DataTableOptionsPaged = {
deferRender: true,
dom: 'irtlp',
fixedHeader: true,
searchDelay: 5000,
serverSide: true,
stateSave: true,
pageLength: 100,
paging: true,
processing: true
};

This question has an accepted answers - jump to answer

Answers

  • bindridbindrid Posts: 730Questions: 0Answers: 119
    edited May 2017 Answer ✓

    I fixed this by removing the default event handler and put on my own.
    http://live.datatables.net/cixegedi/1/edit

    $(document).ready( function () {
      $(document).on("preInit.dt", function(){
        
           var fil = $("#example_filter input");
           // remove default event hanlder
            fil.off();
            // add one for key down
            fil.on("keydown", function(evtObj){
              
              // whatch for the return key
             var keycode = (event.keyCode ? event.keyCode : event.which);
              if(keycode == 13){
                   $('#example').DataTable().search(this.value).draw();
                
              }
            });
            // watching for the return key is not obviouse for all my users so
            // I just used a regular button below for this but  on my own work
            // I used a maginfying glass icon button.
            var btn = $("<button type='button'>Go</button>");
            fil.after(btn);
            btn.on("click", function(){
              
              $('#example').DataTable().search(fil.val()).draw();
              
            });
      });
      var table = $('#example').DataTable(
          {}
      );
      
    } );
    
    
This discussion has been closed.