jQuery UI AutoComplete 不过滤来自服务器的响应

发布于 2024-11-18 21:48:13 字数 1093 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱殇璃 2024-11-25 21:48:13

当您使用远程自动完成功能时,小部件希望您进行过滤。从您的操作方法来看,您只是从存储库中选择项目并将它们返回到小部件,因此下拉列表包含所有结果。

如果您想使用远程数据,但希望 jQueryUI 负责过滤(这可能很好,具体取决于数据集的大小),您可以首先通过 AJAX 请求一个项目数组,然后让自动完成处理其余的事情:

$.ajax({
    type: "POST",
    url: '@Url.Action("LookupGadgets", "Quote")',
    dataType: "json",
    data: {
        type: $("#Type").val()
    },
    success: function (data) {
        var source = $.map(data, function(c) {
            return { label: c.Details, value: c.Details };
        });
        $("#Match").autocomplete({
            source: source,
            minLength: 3
        });
    }
});

如果您的数据集很大,您可能希望避免此策略并在控制器操作中执行过滤。如何进行这种过滤实际上取决于您;然而,实现它的一个简单方法是更改​​您的操作方法:

public ActionResult LookupGadgets(string type, string term)
{
    var retValue = gadgetsRepository.AvailableGadgets
        .Where(x => x.Type == type && x.Make.Contains(term))
        .OrderBy(x => x.Make)
        .Select(r => new { Details = r.Make + " " + r.Model });
    return Json(retValue);
}

并稍微更改您的 AJAX 调用:

$.ajax({
    type: "POST",
    url: '@Url.Action("LookupGadgets", "Quote")',
    dataType: "json",
    data: {
        type: $("#Type").val(),
        term: request.term
    },
    success: function (data) {
        response($.map(data, function (c) {
            return {
                label: c.Details,
                value: c.Details
            }
        }));
    }
});

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:

$.ajax({
    type: "POST",
    url: '@Url.Action("LookupGadgets", "Quote")',
    dataType: "json",
    data: {
        type: $("#Type").val()
    },
    success: function (data) {
        var source = $.map(data, function(c) {
            return { label: c.Details, value: c.Details };
        });
        $("#Match").autocomplete({
            source: source,
            minLength: 3
        });
    }
});

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:

public ActionResult LookupGadgets(string type, string term)
{
    var retValue = gadgetsRepository.AvailableGadgets
        .Where(x => x.Type == type && x.Make.Contains(term))
        .OrderBy(x => x.Make)
        .Select(r => new { Details = r.Make + " " + r.Model });
    return Json(retValue);
}

And slightly change your AJAX call:

$.ajax({
    type: "POST",
    url: '@Url.Action("LookupGadgets", "Quote")',
    dataType: "json",
    data: {
        type: $("#Type").val(),
        term: request.term
    },
    success: function (data) {
        response($.map(data, function (c) {
            return {
                label: c.Details,
                value: c.Details
            }
        }));
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文