jQuery UI 自动完成自定义对象作为源而不是字符串数组
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要以小部件期望的方式格式化数据。结果数组中的每个对象中必须具有
label
属性或value
属性(或两者),以便小部件显示结果。您可以在对象中包含其他数据,只要它满足这些要求即可。要格式化从服务器返回的数据,约定是使用
$.map
:(未经测试)
这应该会为您填充结果。这与您问题的第二部分密切相关:
按照此演示作为指导,这很容易完成:
您可以覆盖
_renderItem 函数来显示您想要的任何内容,只要它是包含
a
标记并具有数据item.autocomplete
的li
即可。将这两种策略结合起来,您就可以开展业务了。有关此操作的示例,请查看此处的示例:http://jsfiddle.net/andrewwhitaker/UvegL /
此示例结合了远程数据源和自定义数据&展示。尽管 AJAX 调用有点不同,但希望它能有所帮助。
You need to format your data in a manner that the widget expects. You must have either a
label
property or avalue
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
:(Untested)
This should get results populating for you. This goes hand-in-hand with the second part of your question:
This is pretty easily accomplished, following this demo as a guide:
You can override the
_renderItem
function to display whatever you want, as long as it's anli
that containsa
tag and has dataitem.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.