gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 04:11:33 24 4
gpt4 key购买 nike

我正在尝试使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com