Tuesday 25 May 2010

JQuery Autocomplete - How to Create Your Own Callbacks for Selecting Options

This here is a post for the hackers out there.
It involves dealing with the JQuery plugin: Autocomplete (http://docs.jquery.com/Plugins/Autocomplete).

I have been struggling these days over a problem that I think many might have.
The standard Autocomplete as of now can only suggest options and when you select one of those options it
gets written in your input that triggered the autocomplete. It works for most of the cases but at times you
may want that when you select that option multiple inputs to be affected like in this example:


Say that we have these inputs: [address], [city], [state], [zip]

Typing something in the [address] input would give you a list of
addresses and you select for example on of those:
"123 mayham ave, New Los Angeles, 90000". Now you might want
that parts of this address string go into different inputs like
"123 mayham ave" to [address], "New Los Angeles" to [city], "90000" to [zip] etc.
Currently with JQuery Autocomplete this is not really possible.

Here's how I solved this problem:

Step 1

I had to alter a single line of code in the autocomplete.js file (the main plugin file for autocomplete). That line was just plainly replacing the input's value with whatever the selector's value was.
In the function selectCurrent() i changed the line
$input.val(v);
to
options.selectOptionHandler && options.selectOptionHandler(selected) || $input.val(v);
this ensures that if you have a callback function named selectOptionHandler in the options parameter it will use that function istead of changing the input's value. This way you can change whatever you want in your callback using the supplied argument (selected) which is the selectedOption object.

Step 2

Next i declared my custom callback in the options like this:

$('#clinfo-'+addressFields[fx]).autocomplete("./?v=ajax&a=autocomplete_async", {
 cacheLength: 1,
 matchContains: true,
 selectFirst:false,
 dataType: "json",
 
 //this function parses the data supplied by the server side (in case it's a json string)
 parse: function(data) {
  var array = new Array();
  if(data.matches){
   for(var i=0;i<data.matches.length;i++){
    array[array.length] = { data: data.matches[i], value: data.matches[i]["cl_"+data.field], result: data.matches[i]["cl_"+data.field] };
   }
  }
  return array;
 },
 formatItem: autocompleteFormatAddress,
 
 // this is the callback function im talking about 
 selectOptionHandler: function(selectItem){
  $("#clinfo-street").val(selectItem.data.cl_street);
  $("#clinfo-city").val(selectItem.data.cl_city);
  $("#clinfo-zip").val(selectItem.data.cl_zip);
  $("#clinfo-state").val(selectItem.data.cl_state);
 },
 
 extraParams:{
  "field":addressFields[fx],
  client: function(){ return $("#clinfo-client").val()}
 }
 });

Of course, I am supplying the values from the server as a json array with rows
containing the values for street, city, state etc.

If you find this thing helpful let me know or perhaps also tell me if you don't find it so I can explain better.

Write me here if you need the modified version of the autocomplete.js file.


Best of luck!

Monday 11 January 2010

Love at First Throttle


Ladies and Gentlemen, I present you my new girlfriend.. (I have two, but since one of them talks on the phone right now, I'll speak about the green one)


Where was I? Yes, right - so this is my new girlfriend - the Ninja 250R. I acquired it 6 hours ago and we already managed to have some really hot moments together (So hot that the needle from the temp-gauge was hanging in between). 


This baby's purpose was to replace my earlier Honda CBR 125R which served me well for  almost 2 months. Which it will do, I think with excellence. It's got the looks of its bigger brother Z6 and from 5 meters you could even take it for a Z6 if you see it frontally.


The European edition has no choke which means that the intake is somehow digitally adjusted which is just great. Not that I am against the old-style choke things, but its place isn't on a sport bike like this one.


Amazing sound of the engine for this low displacement bike, great push and very high-rev loving.


I feel like this affair will last more than a couple of weeks. 


-----------------------------------------------------------------
SPECIFICATIONS
Engine: 249cc, inline twin cylinder, around 30 HP 
Weight: 150kg (dry, add some 20 kg for the liquids)