jQuery UI 自动完成自定义对象作为源而不是字符串数组

发布于 2024-11-28 15:16:53 字数 2076 浏览 1 评论 0原文

我正在使用 JQueryUI 自动完成,想知道如何使用自定义对象作为我的数据源(即我想传回以下类型的列表):

public class Tag
{
    public string Name { get; set; }
    public int Count { get; set; }
}

我当前正在使用的自动完成代码(当我传回时效果很好)名称的直接字符串数组)几乎是 jQuery UI 站点的副本:

$(function () {
        function split(val) {
            return val.split(/ \s*/);
        }
        function extractLast(term) {
            return split(term).pop();
        }

        $("#tags")
        // don't navigate away from the field on tab when selecting an item
        .bind("keydown", function (event) {
            if (event.keyCode === $.ui.keyCode.TAB &&
                    $(this).data("autocomplete").menu.active) {
                event.preventDefault();
            }
        })
        .autocomplete({
            source: function (request, response) {
                $.getJSON("Home/GetTag", {
                    term: extractLast(request.term)
                }, response);
            },
            search: function () {
                // custom minLength
                var term = extractLast(this.value);
                if (term.length < 1) {
                    return false;
                }
            },
            focus: function () {
                // prevent value inserted on focus
                return false;
            },
            select: function (event, ui) {
                var terms = split(this.value);
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push(ui.item.value);
                // add placeholder to get the comma-and-space at the end
                terms.push("");
                this.value = terms.join(" ");
                return false;
            }
        });
    });

我对原始演示源的唯一更改是 Url,并且我在空格而不是逗号上进行分割(用于多个自动完成) 。

HTML 如下:

<div class="ui-widget">
    <label for="tags">Tags: </label>
    <input id="tags"/>
</div>

理想情况下,我想向用户呈现一个名称列表,并附有相应的计数。

I am using JQueryUI Autocomplete and am wondering how to use a custom object as my data source (i.e. I want to pass back a list of the following type):

public class Tag
{
    public string Name { get; set; }
    public int Count { get; set; }
}

The autocomplete code that I am currently using (and that works fine when I pass back a straight forward string array of names) is pretty much a copy off the jQuery UI site:

$(function () {
        function split(val) {
            return val.split(/ \s*/);
        }
        function extractLast(term) {
            return split(term).pop();
        }

        $("#tags")
        // don't navigate away from the field on tab when selecting an item
        .bind("keydown", function (event) {
            if (event.keyCode === $.ui.keyCode.TAB &&
                    $(this).data("autocomplete").menu.active) {
                event.preventDefault();
            }
        })
        .autocomplete({
            source: function (request, response) {
                $.getJSON("Home/GetTag", {
                    term: extractLast(request.term)
                }, response);
            },
            search: function () {
                // custom minLength
                var term = extractLast(this.value);
                if (term.length < 1) {
                    return false;
                }
            },
            focus: function () {
                // prevent value inserted on focus
                return false;
            },
            select: function (event, ui) {
                var terms = split(this.value);
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push(ui.item.value);
                // add placeholder to get the comma-and-space at the end
                terms.push("");
                this.value = terms.join(" ");
                return false;
            }
        });
    });

The only thing I've changed from the original demo source is the Url and I'm splitting on a space rather than comma (for multiple autocomplete).

Here's the HTML:

<div class="ui-widget">
    <label for="tags">Tags: </label>
    <input id="tags"/>
</div>

Ideally, I want to present the user with a list of names, with corresponding count alongside.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

虚拟世界 2024-12-05 15:16:53

如何使用自定义对象作为我的数据源

您需要以小部件期望的方式格式化数据。结果数组中的每个对象中必须具有 label 属性或 value 属性(或两者),以便小部件显示结果。您可以在对象中包含其他数据,只要它满足这些要求即可。

要格式化从服务器返回的数据,约定是使用 $.map

source: function (request, response) {
    $.getJSON("Home/GetTag", {
        term: extractLast(request.term)
    }, function (data) {
        response($.map(data, function (item) {
            return {
                value: item.Name,
                count : item.Count
            };
        });
    });
},

(未经测试)

这应该会为您填充结果。这与您问题的第二部分密切相关:

理想情况下,我想向用户呈现一个名称列表,并附有相应的计数。

按照此演示作为指导,这很容易完成:

$("#auto").autocomplete({ ... })
    .data("autocomplete")._renderItem = function(ul, item) {
        return $("<li>")
            .data("item.autocomplete", item)
            .append("<a>" + item.label + "<br />" + item.count + "</a>")
            .appendTo(ul);
    };

您可以覆盖_renderItem 函数来显示您想要的任何内容,只要它是包含 a 标记并具有数据 item.autocompleteli 即可。

将这两种策略结合起来,您就可以开展业务了。有关此操作的示例,请查看此处的示例http://jsfiddle.net/andrewwhitaker/UvegL /

此示例结合了远程数据源和自定义数据&展示。尽管 AJAX 调用有点不同,但希望它能有所帮助。

how to use a custom object as my data source

You need to format your data in a manner that the widget expects. You must have either a label property or a value property (or both) in each object in the results array in order for the widget to show your results. You can include other data in the object, as long as it meets those requirements.

To format the data coming back from the server, the convention is to use $.map:

source: function (request, response) {
    $.getJSON("Home/GetTag", {
        term: extractLast(request.term)
    }, function (data) {
        response($.map(data, function (item) {
            return {
                value: item.Name,
                count : item.Count
            };
        });
    });
},

(Untested)

This should get results populating for you. This goes hand-in-hand with the second part of your question:

Ideally, I want to present the user with a list of names, with corresponding count alongside.

This is pretty easily accomplished, following this demo as a guide:

$("#auto").autocomplete({ ... })
    .data("autocomplete")._renderItem = function(ul, item) {
        return $("<li>")
            .data("item.autocomplete", item)
            .append("<a>" + item.label + "<br />" + item.count + "</a>")
            .appendTo(ul);
    };

You can override the _renderItem function to display whatever you want, as long as it's an li that contains a tag and has data item.autocomplete.

Combine those two strategies and you should be in business. For a working example of this, check out an example here: http://jsfiddle.net/andrewwhitaker/UvegL/

This example combines a remote datasource and custom data & display. Hopefully it'll help, even though the AJAX call is a bit different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文