简体   繁体   English

是否可以在Jquery Autocomplete中执行?

[英]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. 我正在使用JQuery插件为我的项目开发“自动完成”功能,我需要从两个不同的源(数据库表)向用户分发建议(匹配的结果)给用户,这意味着需要在两个位置发送两个URL的ajax请求时间。

Is it possible to do? 有可能吗?

Happy if i get any suggestions on this 如果我对此有任何建议,我很高兴

Use javascript function as source for autocomplete. 使用javascript函数作为自动完成的来源。 You can perform two ajax requests inside and combine responses into one. 您可以在内部执行两个ajax请求,并将响应组合为一个。

$('#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 . 我已经为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. 使用Microsoft发明的原始ajax对象,您不能同时发出多个请求。 There is many script to make this possible but you are a lucky guy and jQuery is already blessed with one. 有许多脚本可以实现这一目标,但是您很幸运,jQuery已经很幸运了。 The other question is about an autocomplete function and that can be solved with a trie data structure. 另一个问题是关于自动完成功能,可以使用trie数据结构解决。 It's efficient at storing large dictionnaries. 它可以有效地存储大型词典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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