javascript - 使用 $.map 函数的 jQuery UI 自动完成缓存

标签 javascript jquery-ui caching autocomplete jquery-autocomplete

我正在尝试使用 jQuery UI 自动完成实现缓存。 我正在使用 jQuery 1.4.4 和 UI 1.8.6

这是我开始工作的基本代码:

$('#searchbox').autocomplete({
    source: function(request, response) {
            if (xhr === lastXhr) {
                response( $.map(data, function(item) {
                    return {
                        label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                        value: item.NAME
                    };
                }));
            } 
        });
    }
});

这是我通过查看示例尝试让缓存工作的尝试:

var cache = {},
    lastXhr;
$('#searchbox').autocomplete({
    source: function(request, response) {
        var term = request.term;
        if (term in cache) {
            response($.map(cache[term], function(item) {
                return {
                    label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                    value: item.NAME
                };
            }));
        }
        lastXhr = $.getJSON( "getdata.php", request, function(data, status, xhr) {
            cache[term] = $.map(data, function(item) {
                return {
                    label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                    value: item.NAME
                };
            }); 
            if (xhr === lastXhr) {
                response( $.map(data, function(item) {
                    return {
                        label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                        value: item.NAME
                    };
                }));
            } 
        });
    }
});

那里有人要吗?

最佳答案

这是我使用 cache 实现 jQuery UI 自动完成的工作示例.希望对您有所帮助:

    var cache = {};
    $("#textbox").autocomplete({
      source: function(request, response) {
       if (request.term in cache) {
        response($.map(cache[request.term].d, function(item) {
         return { value: item.value, id: item.id }
        }))
        return;
       }
       $.ajax({
        url: "/Services/AutoCompleteService.asmx/GetEmployees",  /* I use a web service */
        data: "{ 'term': '" + request.term + "' }",
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataFilter: function(data) { return data; },
        success: function(data) {
         cache[request.term] = data;
         response($.map(data.d, function(item) {
          return {
           value: item.value,
           id: item.id
          }
         }))
        },
        error: HandleAjaxError  // custom method
       });
      },
      minLength: 3,
      select: function(event, ui) {
       if (ui.item) {
        formatAutoComplete(ui.item);   // custom method
       }
      }
     });

如果您现在还没有这样做,请获取 Firebug 。它是 Web 开发的宝贵工具。您可以在此 JavaScript 上设置一个断点,看看会发生什么。

关于javascript - 使用 $.map 函数的 jQuery UI 自动完成缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4403491/

相关文章:

javascript - jqueryUI - 将项目拖到多个列表

apache - CORS Access-Control-Allow-Origin 缓存问题

javascript - 使用 jquery 操作选择框

jquery - Liftweb WiringUI 和 JQueryUI 冲突

javascript - 如何结合execute_scipt和WebdriverWait

javascript - ui-sortable 不工作(对于预定义的 DOM 位置)

validation - Symfony2 主页 HTTP 缓存验证和独立 ESI

javascript - 如何在客户端避免过多的ajax调用和缓存json数据

javascript - 返回存在于全局范围内的变量的最佳实践?

javascript - Bootstrap Nav Collapse(导航)滚动 - 需要 overflow hidden