【发布时间】:2014-10-14 17:04:25
【问题描述】:
我正在使用twitter typeahead 来显示适合我的类别建议。
问题在于子类别的显示。如果category has sub-categories
then expand/collapse icon 与建议一起显示,如果用户选择其中一个子类别,则必须在其下方显示子类别,我在显示部分成功,但 cannot update the dataset 提前输入,这样当用户选择一个子类别时类别它应该显示在输入中。 Please use keyboard on Jsfiddle because i have tried only in keypress。以下是我的json数组
JSFIDDLEhere
[{
"id": "38",
"value": "Entertaintment",
"parent_id": "27",
"children": "1",
"childCategories": [{
"id": "28",
"value": "Movies",
"parent_id": "38"
}, {
"id": "29",
"value": "Sports",
"parent_id": "38"
}]
}, {
"id": "40",
"value": "eeen",
"parent_id": "27",
"children": "1",
"childCategories": [{
"id": "41",
"value": "een child 1",
"parent_id": "40"
}]
}, {
"id": "41",
"value": "een child 1",
"parent_id": "40",
"children": "0",
"childCategories": false
}]
我尝试了以下方法:
_onEnterKeyed: function onEnterKeyed(type, $e) {
var cursorDatum, topSuggestionDatum;
cursorDatum = this.dropdown.getDatumForCursor();
topSuggestionDatum = this.dropdown.getDatumForTopSuggestion();
//My custom condition, if category has children show sub-categories below
if (cursorDatum.raw && cursorDatum.raw.children == "1") {
//below line is my failed try
//this.dropdown.updateChild('',cursorDatum.raw.childCategories,false);
var that = this, onSuggestionClick, onSuggestionMouseEnter, onSuggestionMouseLeave;
var childHTML='';
$('.ttchild').remove();
//adding the sub-categories to dropdown
$.each(cursorDatum.raw.childCategories,function(i,v){
childHTML += '<div class="tt-suggestion ttchild"><div class="suggestions"><span>'+this.value+'</span></div></div>';
});
$(childHTML).insertAfter('.tt-cursor');
return;
}
if (cursorDatum) {
this._select(cursorDatum);
$e.preventDefault();
}
else if (this.autoselect && topSuggestionDatum) {
this._select(topSuggestionDatum);
$e.preventDefault();
}
},
//This is my custom function trying to update the dataset, but i know its totally wrong.
updateChild: function updateChild(query,childs) {
var that = this;
this.query = query;
this.canceled = false;
this.source(query, render);
function render(childs) {
if (!that.canceled && query === that.query) {
that._render(query, childs);
}
}
},
JSFIDDLE here
例如:娱乐类别包含电影和体育子类别。
希望有人能帮助我......
【问题讨论】:
-
我对它进行了一点修改,但它没有正确保存值。看起来在生成 html 之后添加了“haschildren”按钮,因此由于索引错误,因此添加了额外的按钮。希望这在某种程度上对您有所帮助,但是现在挖掘您一直在破解的整个typeahead.bundle file 是一座太远的桥梁。 jsfiddle.net/82hmLprb/1
-
所有数据都是通过bloodhound添加的。它创建令牌、哈希和缓存。我认为如果我们可以更新寻血猎犬对象中的项目,一切都会好起来的
-
@Nouphal.M 嘿,我怎么还没拿到赏金呢,我解决了你的问题!
标签: javascript jquery twitter-bootstrap autocomplete bootstrap-typeahead