mike chambers | about

Adding custome query argument support to jQuery Autocomplete plugin

Wednesday, September 3, 2008

I have been playing around with the jQuery Autocomplete plugin to get it working with the ActionScript 3 documentation API that I posted about earlier today.

The Autocomplete API is really nice, and pretty flexible, but one issue I ran into is that the query argument containing the user input that is passed to the server is hard coded to “q”. For my api, I needed this argument to be “api”.

The fix is actually pretty simple, although it does require a slight modification to the jquery.autocomplete.js file.

Here is a diff that shows the change for the AutoComplete library version 1.0.2:

340a341,343
> 			var o = {limit:options.max};
> 			o[(options.queryArgument)?options.queryArgument:"q"] = lastWord(term);			
> 			
348,351c351
< 				data: $.extend({
< 					q: lastWord(term),
< 					limit: options.max
< 				}, extraParams),
---
> 				data: $.extend(o, extraParams),

You can then pass in the value that should be use for the query term like so:

var url = "http://as3reference.appspot.com/getapi?version=flex3";
$("#example").autocomplete(url, {
	matchCase:true,
	formatItem:formatItem,
	parse:parse,
	queryArgument:"api"
});

If the queryArgument is not specified, then it defaults back to “q”.

It looks like this feature will be added in the next release.

Ill post the complete example working with the API shortly.

twitter github flickr behance