开发者

Jquery autocomplete "search" method

开发者 https://www.devze.com 2023-02-06 13:32 出处:网络
I am using Jquery UI autocomplete, this is the code below开发者_开发知识库 var opt_source = {...}

I am using Jquery UI autocomplete, this is the code below

开发者_开发知识库
var opt_source = {...}
var options = {
            minLength: 0,
            source: opt_source,
            search: "aPreDefinedString"
        };
$(".searchable_input").autocomplete(options);

My understanding is that it should now search for aPreDefinedString ; This doesn't happen, rather it searches the local source for userInput. Could someone point out where I am going wrong?


OK, this is how I had to make it work

var opt_source = {..};

var options = {
            minLength: 0,
            source: function(request, response){
                response(opt_source);
            }
        };
$(".searchable_input").autocomplete(options);

This seems to override the inbuilt search (I hope they dont break it in future versions)

From the Jquery UI 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


I think you're mixing up the search event and the search method on the autocomplete widget. You can specify an event handler for the search event in the options object (which you're doing) that's used to initialize the widget.

The way you would call the search method is like this:

$(".searchable_input").autocomplete( "search" , "aPreDefinedString" );

This would manually search the autocomplete.


my code is like this and it work.

var availableTags = ['aa','bb','cc'];

$( "#filterinput" ).autocomplete({
                      source: availableTags,
                      autoFocus: true,
                    });

$( "#filterinput" ).on( "autocompletesearch", function( event, ui ) {
                      console.log($(this).val());
                    } );
0

精彩评论

暂无评论...
验证码 换一张
取 消