jQuery UI AutoComplete 不过滤来自服务器的响应
我正在使用 jQueryUI AutoComplete,但遇到一个小问题,即当用户在文本框中键入内容时不会发生任何过滤。
基本上发生的情况是,在我输入 3 个字符后,自动填充下拉列表将填充从服务器返回的整个项目集合,但随着输入更多字符,选择范围不会缩小。
关于为什么会发生这种情况有什么想法吗?
客户端:
$("#Make").autocomplete({
minLength: 3,
source: function (request, response) {
$.ajax({
type: "POST",
url: '@Url.Action("LookupGadgets", "Quote")',
dataType: "json",
data: {
type: $("#Type").val()
},
success: function (data) {
response($.map(data, function (c) {
return {
label: c.Details,
value: c.Details
}
}));
}
});
}
});
服务器:
public ActionResult LookupGadgets(string type)
{
var retValue = gadgetsRepository.AvailableGadgets
.Where(x => x.Type == type)
.OrderBy(x => x.Make)
.Select(r => new { Details = r.Make + " " + r.Model });
return Json(retValue);
}
I'm using jQueryUI AutoComplete but am having a small problem in that no filtering is taking place as the user types in the text box.
Basically what happens is that after I've typed 3 characters the auto-fill drop down fills with the entire collection of items returned from the server but the choice does not narrow down as more characters are typed.
Any ideas as to why this is happening?
Client:
$("#Make").autocomplete({
minLength: 3,
source: function (request, response) {
$.ajax({
type: "POST",
url: '@Url.Action("LookupGadgets", "Quote")',
dataType: "json",
data: {
type: $("#Type").val()
},
success: function (data) {
response($.map(data, function (c) {
return {
label: c.Details,
value: c.Details
}
}));
}
});
}
});
Server:
public ActionResult LookupGadgets(string type)
{
var retValue = gadgetsRepository.AvailableGadgets
.Where(x => x.Type == type)
.OrderBy(x => x.Make)
.Select(r => new { Details = r.Make + " " + r.Model });
return Json(retValue);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用远程自动完成功能时,小部件希望您进行过滤。从您的操作方法来看,您只是从存储库中选择项目并将它们返回到小部件,因此下拉列表包含所有结果。
如果您想使用远程数据,但希望 jQueryUI 负责过滤(这可能很好,具体取决于数据集的大小),您可以首先通过 AJAX 请求一个项目数组,然后让自动完成处理其余的事情:
如果您的数据集很大,您可能希望避免此策略并在控制器操作中执行过滤。如何进行这种过滤实际上取决于您;然而,实现它的一个简单方法是更改您的操作方法:
并稍微更改您的 AJAX 调用:
When you use the remote flavor of autocomplete, the widget expects you to do the filtering. Judging by your action method, you are just selecting items from your repository and returning them to the widget, so the dropdown contains all of the results.
If you want to use remote data but want jQueryUI to take care of the filtering (which might be fine depending on the size of your dataset), you could request an array of items via AJAX first and then let autocomplete take care of the rest:
If your dataset is large, you may want to avoid this strategy and perform the filtering in your controller action. How this filtering occurs is really up to you; however, a simple way to accomplish it would be to change your action method:
And slightly change your AJAX call: