Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery-ui autocomplete: set source by callback function not working

link to fiddle : http://jsfiddle.net/nEapJ/ (working)

var items = [{
   label : 'a',
   value : 'a',
},{
   label : 'b',
   value : 'b',
},{
   label : 'c',
   value : 'c',
}];

$('input').autocomplete({
    source : items
});​

This code works, but when i want to set source by callback function then It's not working

link to fiddle : http://jsfiddle.net/B3RWj/ (not working)

$('input').autocomplete({
    source : function(request, response){
            response(items);
          }
});​

when i type, 'a' then its give a,b,c as result.

So, what am I missing?

thanks, in advance.

like image 923
Array out of bound Avatar asked Sep 14 '12 13:09

Array out of bound


2 Answers

In the callback function it's up to you to do the filtering..

Extract from documentation:

The third variation, the callback, provides the most flexibility, and can be used to connect any data source to Autocomplete. The callback gets two arguments:

A request object, with a single property called "term", which refers to the value currently in the text input. For example, when the user entered "new yo" in a city field, the Autocomplete term will equal "new yo". A response callback, which expects a single argument to contain the data to suggest to the user. This data should be filtered based on the provided term, and can be in any of the formats described above for simple local data (String-Array or Object-Array with label/value/both properties). It's important when providing a custom source callback to handle errors during the request. You must always call the response callback even if you encounter an error. This ensures that the widget always has the correct state.

like image 135
Michal Klouda Avatar answered Oct 27 '22 00:10

Michal Klouda


see the code:

$('input').autocomplete({
    source : function(request, response){
        var term = request.term;
        var result = [];

        //make your code here to filter the item by term. 
        //put them into the result array such as.

        response(result);//this will show in the selection box.
    }
});​
like image 42
fantaxy025025 Avatar answered Oct 26 '22 23:10

fantaxy025025