jQuery几乎赢得了我 - UI自动完成“数据未定义”
jQuery almost winning me - UI Autocomplete " data undefined"
jQuery几乎赢得了我。我已经在StackOverflow答案中看到了很多,但我找不到解决问题的方法。
我在我的项目中使用 jQuery UI 自动完成功能,如果没有返回任何结果,我需要渲染默认消息。我正在使用"_renderItem",正如您在下面看到的。
var autocompleteDir = $("#search");
autocompleteDir.autocomplete({
source: function (request, response) {
populate(request.term, response);
result = $.ui.autocomplete.filter(result, request.term)
var item = {};
item.type = 'loading';
item.label = "Loading..";
item.value = "";
result.unshift(item)
response(result.slice(0, 10));
}
});
autocompleteDir.data("ui-autocomplete")._renderItem = function (ul, item) {
if (item.type === "loading") {
return $('<li></li>')
.data("ui-autocomplete-item", item)
.append("<div style='text-align: center;'>" + item.label + "</div>")
.appendTo(ul);
}
};
这是我在控制台上得到的内容:
Uncaught TypeError: Cannot read property 'type' of null
Uncaught TypeError: Cannot call method 'data' of undefined
我正在使用jQuery 1.10.2和jQuery UI 1.10.3。有人有好主意吗?
编辑 1:
如果我在"源"之前使用它:
response: function(event, ui){
if (ui.item.type === "loading"){
ui.content.push({label:"Loading",value:"Loading"})
}
}
我有以下错误:
Cannot read property 'type' of undefined
如果您帮助我解决此问题,我可以使用"响应"来格式化和样式我的"加载"和"无"结果吗?
编辑 2
var populate = function(term, response) {
$.getJSON(
'<%= my_rails_path %>.json',
{search: term},
function(json) {
var result = [];
$.each(json, function(key, value) {
var item = {};
item.type = '';
item.label = value.name;
item.value = value.name;
result.push(item);
})
if (result.length == 0) {
var item = {};
item.type = "noResult";
item.label = "Create '" + term + "'";
item.value = term;
result.push(item)
}
response(result);
}
);
};
编辑 3
现在问题解决了,但标签从字面上显示出来,但我希望渲染它。查看代码,您将了解:
response: function(event, ui){
for(var i in ui.content){
if (ui.content[i].type==="loading"){
ui.content[i] = {
label:"<div style='text-align: center;'>Loading</div>",
value:""
}
}
}
}
而是在 ui 中间呈现"加载",所有字符串都显示给用户(加载)。
你不需要
使用_renderItem
,因为 jQuery 提供了一个在搜索返回之后和显示结果之前触发的事件,如果搜索返回为空,你可以修改该事件以返回默认值。请参阅响应事件。您可以改为执行以下操作:
autocompleteDir.autocomplete({
source: function (request, response) {
populate(request.term, response);
result = $.ui.autocomplete.filter(result, request.term)
var item = {};
item.type = 'loading';
item.label = "Loading..";
item.value = "";
result.unshift(item)
response(result.slice(0, 10));
},
response: function(event, ui){
if(ui.content.length === 0){
ui.content.push({label:"default",value:"value"}); /* modify to your liking */
}
}
});
改用这种技术应该可以解决这两个问题。
编辑:
在第三次编辑中,如果要更改页面上其他位置的div 值,则需要自己在响应函数中执行此操作,而不是通过在函数中返回值来执行此操作。
response: function(event, ui){
if(ui.content.length === 0){
$('#id-of-div-to-change').text("Loading");
}
}
相关文章:
- 引用错误:在尝试使用ajax发送数据时未定义
- jquery:构造的数据是未定义的
- 尝试在组件之间传递数据时未定义
- JSON可以't访问数据返回未定义
- 传递给数据的未定义变量
- 挖空错误:数据保留未定义
- 访问数据时未定义流星用户
- Ionic-来自电子邮件框的数据返回未定义
- 当使用jQuery发出$.post请求时,返回的数据是未定义的
- 无法读取属性'childNodes'在尝试从XML文件中检索数据时未定义的
- 在replaceWith之后,jquery中的数据对象未定义
- 流星+反应”;未捕获的类型错误:无法读取属性'数据'“未定义”;
- EXT.NET:“;未捕获的类型错误:无法读取属性'数据'未定义的“;将记录添加到存储时
- 通过ajax为flot.js提供的数据显示未定义
- 使用service传递数据的未定义变量
- 异步服务调用后,nhandsontable数据流未定义
- d3.Json获取数据是未定义的
- 子行数据表-行数据值未定义
- 获取数据id“未定义”
- 读取数据属性=未定义