tags:

views:

428

answers:

1

I wanted to implement an ajax based autocomplete feature for my searchbox, and i came across, http://stackoverflow.com/questions/1118782/implementing-autocomplete-in-my-website.

Now what i wanted to know was that i attach a datasource to the control, but so far i have seen that the datasource requires a textbase schema, can't i like it to a query, where it control calls the query and it returns the records on which the filter of the control must apply.

Hope my question is clear

+1  A: 

How do you think you can link it to a query on client-side?? You can link it to an AJAX call to the server, which returns the option-list. The control's filter will do the rest filtering on that option-list.

The best practice would be, to fire an AJAX on page load, to a server function, which will query to the database (MySQL in your case) and fetch the options-list in json format. Assign the option-list to as an input for autocomplete. (Its obviously better than to fire a ajax-request everytime user starts to type-in the search box.)

If you use jquery it can be something like this.

$(function(){  //runs on page load

$.ajax({
  type: "POST",
  url: "/searchlist/", //server function that returns the search list
  data: '',
  dataType: "json",
  success: function(json){
    search_choices = json.list; // search option list
    $("input#searchbox").autocomplete(search_choices, {
                               max: 4,
                               scroll: false,
                               autoFill: true,
                               multiple: true,
                               matchContains: true,
                               multipleSeparator: " ",
                               width: 180
                             });

  }
});

});

I can provide you with example in libraries other than jquery, but i hope this can make you find your way.

Edit: No, your database needn't to have sorted choices. It is your server function, that should be doing all the sorting. Use,

autocomplete( url_to_server_function, options)

and your server function will get search term (keyword user types in search-box), as get request. Filter your database there, and this is the place where you can hook related words along with the results. Just make a list of everything you want to show as suggestion, and return in serialize json format and let autocomplete to take care of matching and sorting the data.

simplyharsh
i like your idea, and i'm doing something similar only. However i have a new doubt now.. Does my database have to contain the data in the way i want it to be shown? I mean when i type say "com" now it will query my db for all keywords starting with "com" and display them like computer, compartment etc.., however what if i want it more sophisticated and combine it with the related words too, like google suggest, so "com" can mean "come here", "computer course" etc.. Does yahoo YUI Widget or any other provide that functionality as well?
Anirudh Goel
hey harsh..am still not clear with the idea..can i reach you on gtalk? reach me on [email protected]
Anirudh Goel
making sense of "autocomplete( url_to_server_function, options)"url_to_server_function takes in the keyword as input and returns the matching keywords which satisfy a certain criteria, by default (all words starting with the keyword).. this output inturn is provided to autocomplete function, which does matching and sorting. my question iswhat do you mean by matching there at autocomplete?second you said to filter the database, that means the query which returns the keywords right?
Anirudh Goel