Я использую 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.
JSFIDDLE здесь
[{
"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 здесь
например: в категории «Развлечения» есть подкатегории «фильмы» и «спорт».
Надеюсь, кто-нибудь мне поможет....