7

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.

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i think, jquery-ui autocomplete auto filter by typed charcters by matching with label key in given source..
actually, it auto filters only for local source option
2

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.
    }
});​

Comments

0

If you want to use the callback function instead of a source array or string you have to add response($.ui.autocomplete.filter(items, request.term));

in the your function:

source : function(request, response){}

This is what autocomplete does when you define your source as an array or string, but for callbacks you have to add this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.