未调用Autocomplete formatItem,firebug中也没有任何错误

Autocomplete formatItem not called, neither any error in firebug

本文关键字:任何 错误 firebug 调用 Autocomplete formatItem      更新时间:2023-09-26

基本上我想显示一个图像,除此之外还有文本。这是我的自动完成代码:

 $("#tagBox").autocomplete({
                source: '/Friends/FriendsTagHandler.aspx?FileID=<%=Request.QueryString["FileID"] %>',
                scroll: true,
                scrollHeight: 300,
                formatItem: function (data, i, n, value) {
                    console.log(values);
                    var values = value.split(".");
                    return "<img src='/images/ProfileAvatar/ProfileImage.aspx?AccountID=" + values[0] + "'/> " + values[1];
                },
                formatResult: function (data, value) {
                    console.log(value);
                    return value.split(".")[1];
                }
            });

然而,我的formatItem或formatResult既没有被调用,也没有在firebug控制台中得到任何错误。

更新:我在SO本身的某个地方读到formatItem被弃用,我们应该从服务器本身返回格式化的数据。所以我从我的服务器返回了格式化数据:

代码段

 foreach (var item in friends)
            {
                sb.Append("<img src='/images/ProfileAvatar/ProfileImage.aspx?AccountID=" + item.AccountID.ToString() + "'/>" + item.FirstName + " " + item.LastName).
                        Append(Environment.NewLine);
            }
            //context.Response.ContentType = "text/plain";
            context.Response.Write(sb.ToString());

当我在浏览器中点击url时,我可以正确地看到图像和除此之外的名称。然而,自动完成框中并没有真正显示任何内容。

您得到的是jQueryUI自动完成,而它的祖先jQuery自动完成混淆了。jQueryUI自动完成没有formatItemformatResultscrollscrollHeight选项。

为了完成你想要的,你需要覆盖_renderItem函数,比如这个例子:

$("#tagBox").autocomplete({ ... })
    ._renderItem = function (ul, item) {
        // Custom item display logic here.
    };

此外,您的source数据应该是一个返回数据的函数、数据本身或以以下格式返回数据的URL:

  • 字符串数组:['option1', 'option2', 'option3'],或
  • 属性为labelvalue(或两者兼有(的对象数组:[{ label: 'option1', value: 'option1'}, { ... }]

首先,我在Jquery UI中没有看到任何formatItem或"formatResult",下面是我所做的操作,并确保您返回JSON对象

When a String is used, the Autocomplete plugin expects that string to point to a URL resource that will return JSON data. It can be on the same host or on a different one (must provide JSONP). The request parameter "term" gets added to that URLhttp://jqueryui.com/demos/autocomplete/#remote-jsonp

  $("#tagBox").autocomplete({
                source: "@Url.Action("Search", "Person")",//I'm using asp.net MVC here 
                select: function (event, ui) {
                    $(this).val( ui.item.Name );
                    return false;
                }
            }).data("autocomplete")._renderItem = function (ul, item) {
                var term=$(#tagBox).val();
                return $("<li style='"background-color:Yellow'" ></li>")
                .data("item.autocomplete", item)
                .append("<a>"+ "<img src=/images/ProfileAvatar/ProfileImage.aspx?thumbnailId="+item.ImageId +"'></img>" + item.Name.replace(new RegExp('(' + item.Term + ')', 'gi'), "<b>$1</b>")  + "</a>").appendTo(ul);
            };

我的控制器代码

 IList<Person> people= new List<Person>();
// people.Add() here
 return Json(people);

还有我的个人课

public class Person 
    {
        public int Id{ get; set; }
        public string Name { get; set; }
        public int ImageId 
        {
            get; 
            set;
        }
}