3 Autocomplete fields reuse same AJax call.

This is a typical customized request, which in one page, they want to enter same ticket number to search, and same time can search 3 fields, and do 3 different functions. So like below code showed, we reused same search function in autocomplete source.


$(document).ready(function(){
	var cache = {};
	$( "#lookup_ticketnum_1" ).autocomplete({
	  source: function(request, response){
		  search_msi(request, response)
		  },
  	  minLength: 3,  	  
    });
	$( "#lookup_ticketnum_2" ).autocomplete({
	  source: function(request, response){
		  search_msi(request, response)
		  },
  	  minLength: 3,  	  
    });
	$( "#lookup_ticketnum_3" ).autocomplete({
	  source: function(request, response){
		  search_msi(request, response)
		  },
  	  minLength: 3,  	  
    });
    function search_msi(request, response){
    	var term = request.term;
	    if ( term in cache ) {
	      response( cache[ term ] );
	      return;
	    }
	 
	    $.getJSON( "/matrix/ajax_search_msi.php", request, function( data, status, xhr ) {	    	
	      cache[ term ] = data;
	      response( data );
	    });
    }										
});

Advertisements

About qianggan

Sr. Software Engineer
This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s