结合在jQuery用户界面自动完成本地源和远程源 [英] Combining a local source and remote source in jquery ui autocomplete

查看:94
本文介绍了结合在jQuery用户界面自动完成本地源和远程源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我包括在本地的javascript的常用术语列表,然后我也想从服务器获得通过Ajax响应JSON响应。怎样才能做到呢?

I included locally in javascript a list of commonly used terms, and then I would also like to get json response from the server through ajax response. How can it be done?

var projects = ["apple", "orange"];

$('#search').autocomplete({
    source: projects
});

然后从阿贾克斯追加的结果?

then append the result from ajax?

推荐答案

你会去,这将是结合你从服务器获取与本地结果阵列后面的结果的方式。

The way you would go about this would be to combine the results you get back from the server with the local results array. You can accomplish this by passing a function to the source option of autocomplete:

有三个步骤,你将不得不执行:

There are three steps you'll have to perform:

  1. 请AJAX请求,并从服务器获取结果。
  2. 筛选本地阵列
  3. 联合的结果

这应该是pretty的简单。像这样的工作:

This should be pretty simple. Something like this would work:

$("input").autocomplete({
    source: function(request, response) { 
        /* local results: */
        var localResults = $.ui.autocomplete.filter(localArray, request.term);

        /* Remote results: */
        $.ajax({
            /* AJAX options omitted... */
            success: function(data) {
                /* Process remote data using $.map, if necessary, then concatenate local
                 * and remote results. 
                 */
                response(data.concat(localResults));
            }
        });
    }
}); 

我已经在这里工作了一个完整的例子: http://jsfiddle.net/FZ4N4/

这篇关于结合在jQuery用户界面自动完成本地源和远程源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆