Est*_*ban 11 jquery-ui jquery-ui-autocomplete
我有以下代码.它不会生成js错误.无法获取自动完成功能以显示任何结果:
$(function() {
$.ajax({
url: "data.xml",
dataType: "xml",
cache: false,
success: function (xmlResponse) {
var data_results = $("Entry", xmlResponse).map(function () {
return {
var1: $.trim($("Partno", this).text()),
var2: $.trim($("Description", this).text()),
var3: $.trim($("SapCode", this).text()),
var4: $("Title", this).text(),
var5: $.trim($("File", this).text()),
var6: $.trim($("ItemID", this).text())
};
}).get();
$("#searchresults").autocomplete({
source: data_results,
minLength: 3,
select: function (event, ui) {
...
}
}).data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" ).data("item.autocomplete", item)
.append( "<a>" + item.var1 + "<br>" + item.var2 + "</a>")
.appendTo( ul );
};
}
});
Run Code Online (Sandbox Code Playgroud)
我可能会缺少什么想法?提前致谢.
Dzu*_*sir 29
现在好像.data('autocomplete')是现在.data('ui-autocomplete').
资料来源:http://jqueryui.com/upgrade-guide/1.10/#removed-data-fallbacks-for-widget-names
默认情况下,自动填充要求源数组包含具有label属性,value属性或两者的对象.
考虑到这一点,您有两种选择:
从AJAX调用处理数组时,向源对象添加标签或值属性:
var data_results = $("Entry", xmlResponse).map(function () {
return {
var1: $.trim($("Partno", this).text()),
var2: $.trim($("Description", this).text()),
var3: $.trim($("SapCode", this).text()),
var4: $("Title", this).text(),
var5: $.trim($("File", this).text()),
var6: $.trim($("ItemID", this).text()),
value: $.trim($("Description", this).text())
};
}).get();
Run Code Online (Sandbox Code Playgroud)
该value分配将用于对focus,select和搜索上.
更改source功能以执行自定义过滤逻辑:
$("#searchresults").autocomplete({
source: function (request, response) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
response($.grep(data, function (value) {
return matcher.test(value.var1) ||
matcher.test(value.var2);
/* etc., continue with whatever parts of the object you want */
}));
},
minLength: 3,
select: function (event, ui) {
event.preventDefault();
this.value = ui.var1 + ui.var2;
},
focus: function (event, ui) {
event.preventDefault();
this.value = ui.var1 + ui.var2;
}
}).data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" ).data("item.autocomplete", item)
.append( "<a>" + item.var1 + "<br>" + item.var2 + "</a>")
.appendTo( ul );
};
Run Code Online (Sandbox Code Playgroud)
请注意,使用此策略,您必须实现自定义select和focus逻辑.
| 归档时间: |
|
| 查看次数: |
34766 次 |
| 最近记录: |