我正在使用自动完成功能,我想在列表中添加一个标题,这让我很复杂,我遇到了一些问题: 这是我的代码:
var autocompleteCounselor = function () {
$(this.el).find('.alternateCounselorAutocomplete').autocomplete({
minLength: 0,
source: this.counselorValidDelegates,
focus: function (event, ui) {
console.log(ui);
$(".alternateCounselorAutocomplete").val(ui.item.NAME());
return false;
},
select: function (event, ui) {
onAutocompleteAlternateCounselorSelected.apply(_self, [event, ui, _self.counselorFullValidDelegates]);
}
}).data("ui-autocomplete")._renderMenu = function(ul, items){
var self = this;
ul.append("<li><span>Name</span>"+
"<span>Title</<span>" +
"<span>Phone</<span> "+
"<span>Location</<span>"+
"<span>Department</span></li>");
$.each(items, function(index, item){
/*return $("<li>")
.append("<div class='listcounsuler'><span>" + "<a>" + item.NAME() + "</a>" + "</span>" + "<span>" + item.TITLE() + "</span>" + "<span>" + item.PHONE() + "</span>" + "<span>" + item.LOCATION() + "</span>" + "<span>" + item.DEPARTMENT() + "</span>" + "</div>")
.appendTo(ul);*/
self._renderItem(ul, item);
});
/* ul.append("</tbody></table>");*/
};
$('.alternateCounselorAutocomplete').autocomplete().data("ui-autocomplete")._renderItem = function (ul, item) {
return $("<li>")
.append("<div class='listcounsuler'><span>" + "<a>" +
item.NAME() + "</a>" + "</span>" + "<span>" + item.TITLE() +
"</span>" + "<span>" + item.PHONE() + "</span>" + "<span>" +
item.LOCATION() + "</span>" + "<span>" + item.DEPARTMENT() +
"</span></div>")
.appendTo(ul);
};
$('.alternateCounselorAutocomplete').autocomplete("search", "");
$(this.el).find('.counselorEdit').on('click', function () {
$('.alternateCounselorAutocomplete').autocomplete('close');
});
};
菜单呈现完美,问题是当我把注意力集中在li上时,在控制台中我看到错误:未捕获的TypeError:无法调用未定义的方法'NAME'
我看到焦点项未定义: 对象{item:undefined}
函数渲染项也工作正常,我看到了我想要的“li” 我通过_renderItemData更改了_renderItem并且它继续发生,我尝试了几件事我无法得到解决方案。欢迎任何帮助。
此致
答案 0 :(得分:0)
我解决了从render_menu调用self._renderItemData ..
的问题