views:

366

answers:

2

I have read several articles/questions/forums discussing the best auto-complete plugin for jQuery. After trying several good ones, I've realized a flaw in most.

  1. If you are looking up countries and type 'In', a couple of countries show up. If you continue typing I-n-d-i-a, this results in 5 AJAX calls (see http://www.freeimagehosting.net/uploads/6f1bcd69e1.png) its quite natural that India is a subset of In, so why call again? We need to simply filter the retrieved list client-side. Anyone knows about such an implementation?

  2. What is the status of the Jquery Autocomplete feature? I read at StackOverflow that it is no longer available with Jquery; but the Jquery website has a 'New' mark besides the link to Autocomplete.

Thanks

+6  A: 

The point you write about in 1. could be because :

  • Searching for 'In' shoudl return a lot of results
  • There is some limit in place, on the server-side, to never return more than N results
  • Which means the full list of countries contains "In" is not known, on the client-side
  • Which implies it's not possible to get (for sure) the list that corresponds to "Ind" without another Ajax request.

A half-solution that's often used is to not send an Ajax request immediatly after a keypress, but only 100 or 200 milliseconds after.

This way, if the user types "Indi" fast, and waits before typing anything else, the will only be 1 Ajax request, for "Indi" (and none for "In", "Ind")

Pascal MARTIN
Ooh yes, I've already done that to reduce the no of requests. The scenario you discussed is valid, but there can be parameters defined to avoid this suboptimal behavior.
Prasad
+3  A: 

The jQuery UI autocomplete has been added in the new version 1.8 so as far as I saw in the project activity it is still being developed. There is an example that uses a client side cache concerning the issue you explained. You can use the already mentioned delay option, too.

Daff
Good to know that they'll continue with AutoComplete. However, this example is not the best when it comes to caching. Try finding 'Nightingale' one char at a time.. When going from 'night' (5 results) to 'nighti' (2 results), it fires a call :(Guess I will have to live with the additional calls :( :( :(
Prasad
Well what they do there is just simple regex matching. So you would basically just have to match your cached list first before sending a new request. For remote sources you always have to consider, that you might not get the whole list for entering e.g. *In* , it may be limited to a maximum number of items, too. Therefore you will have to send a new event for the next letter anyway.
Daff