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.


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.


看代码:

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

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.

链接地址: http://www.djcxy.com/p/11196.html

上一篇: 估计复杂网络的分形维数,提供边的节点

下一篇: ui自动完成:通过回调函数设置源不起作用