自动完成 formatItem 未调用,firebug 中也没有任何错误
基本上我想显示图像和文本。这是我的自动完成代码:
$("#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 已被弃用,我们应该从服务器本身返回格式化数据。因此,我从服务器返回了格式化数据:
Snippet
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 时,我可以正确地看到图像及其旁边的名称。然而,自动完成框中没有真正出现任何内容。
Basically I want to show an image and besides it text. This is my autocomplete code:
$("#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];
}
});
However my my formatItem or formatResult are neither called and nor do I get any error in firebug console.
Update: I read somewhere on SO itself that formatItem is deprecated and we should return formatted data from server itself. So I returned formatted data from my server:
Snippet
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());
When I hit the url in browser I can correctly see image and the name besides it. However nothing really appears in autocomplete box.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将获得 jQueryUI 自动完成 及其祖先 jQuery 自动完成 混淆。 jQueryUI 自动完成没有
formatItem
、formatResult
、scroll
或scrollHeight
选项。要完成您想要的任务,您需要重写
_renderItem
函数,如本例所示:此外,您的
源
数据应该是返回数据的函数、数据本身或按以下格式返回数据的 URL['option1', 'option2'、'option3']
或label
或value
(或两者)的对象数组:[{ label: '选项1',值:'选项1'},{...}]
You're getting jQueryUI autocomplete and it's ancestor, jQuery autocomplete confused. jQueryUI autocomplete does not have the
formatItem
,formatResult
,scroll
, orscrollHeight
options.To accomplish what you're after, you're going to need to override the
_renderItem
function, like in this example:Additionally, your
source
data should be a function that returns data, the data itself, or a URL that returns data in the following format:['option1', 'option2', 'option3']
, orlabel
orvalue
(or both):[{ label: 'option1', value: 'option1'}, { ... }]
首先,我在 Jquery UI 中没有看到任何
formatItem
或“formatResult”,这是我所做的,并确保您返回 JSON 对象当使用字符串时,自动完成插件期望该字符串指向将返回 JSON 数据的 URL 资源。它可以位于同一主机或不同主机上(必须提供 JSONP)。请求参数“term”被添加到该 URL
http://jqueryui.com /demos/autocomplete/#remote-jsonp和我的控制器代码,
和我的 person 类,
First thing is I don't see any
formatItem
or 'formatResult' In Jquery UI, Here is what I did to do this and make sure you are returning JSON objectWhen 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 URL
http://jqueryui.com/demos/autocomplete/#remote-jsonpAnd my controller code ,
And my person class,