简体   繁体   中英

Is it possible to do in Jquery Autocomplete?

I am developing a "Auto complete" functionaly for my project using JQuery plugin, i need to dispaly the suggestions(matched results) to the user from two different sources(Database tables)that means ajax request needs to be send for two url's at a time.

Is it possible to do?

Happy if i get any suggestions on this

Use javascript function as source for autocomplete. You can perform two ajax requests inside and combine responses into one.

$('#myinput').autocomplete({
   source: function(data, callback) {
      $.ajax(firstUrl, { 
         ..., 
         success: function(result1) {
                $.ajax(secondUrl, {
                     success: function(result2) {
                           var mergedResults = result1.concat(result2);
                           callback(mergedResults);
                     }
                });
         },
   }
}

I've written something for jsdocu.com . But here's just one source to search in. However, it is not so hard to change it to your needs.

/** delayR by Yannick Albert | https://gist.github.com/3729753 **/
var delayR = function(a,b,c,d){c=null;return function(){d=this;clearTimeout(c);c=setTimeout(function(){a.apply(d,arguments)},b)}};
$("#s").bind('click keyup', delayR(function() {
    var term = this.value,
        url = $('#searchform').attr('action');

    if (term) {
        $.post(url, {
            s: term
        }, function(data) {
            var content = $(data).find('.hentry');
            if (content.length !== 0) {
                $("#autocomplete").fadeIn().empty().append(content);
            } else {
                $("#autocomplete").hide();
            }
        });
    } else {
        $("#autocomplete").fadeOut();
    }
}, 300));

Yes, it's possible but it's 2 questions. With the original ajax object invented by Microsoft you cannot make multiple request at the same time. There is many script to make this possible but you are a lucky guy and jQuery is already blessed with one. The other question is about an autocomplete function and that can be solved with a trie data structure. It's efficient at storing large dictionnaries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM