Skip to content

Commit ffdd879

Browse files
Merge pull request #6246 from CybotMolay/patch-1 #6246
Fix highlight problem on second search
2 parents 2a341fa + 3463650 commit ffdd879

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

docs/knowledge-base/combobox-highlight-matched-text.md

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,44 @@ Implement the `dataBound` event handler for the widget.
4343
var items = combo.items();
4444
var inputText = $('.k-input').val().toLowerCase();
4545
46-
for (var i = 0; i < items.length; i += 1) {
46+
for (var i = 0; i < items.length; i += 1) {
4747
var item = $(items[i]);
4848
var itemHtml = item.html();
4949
var startIndex = itemHtml.toLowerCase().indexOf(inputText);
50-
var endIndex = startIndex + inputText.length;
51-
52-
var outputHtml = [
53-
itemHtml.slice(0, startIndex),
54-
'<span class="highlight">',
55-
itemHtml.slice(startIndex, endIndex),
56-
'</span>',
57-
itemHtml.slice(endIndex)
58-
].join('');
59-
60-
item.html(outputHtml);
50+
if(startIndex>=0){ // Only highlighted the items which contain the search text
51+
var endIndex = startIndex + inputText.length;
52+
var outputHtml = [
53+
itemHtml.slice(0, startIndex),
54+
'<span class="highlight">',
55+
itemHtml.slice(startIndex, endIndex),
56+
'</span>',
57+
itemHtml.slice(endIndex)
58+
].join('');
59+
60+
item.html(outputHtml);
61+
}
6162
}
6263
}
6364
65+
// Remove highlight after seleting the event. Otherwise dropdown always highlighted the item which has been selected frist time after searching text.
66+
function removeHighlight(e) {
67+
var combo = e.sender;
68+
var items = combo.items();
69+
var inputText = $('.k-input').val().toLowerCase();
70+
71+
for (var i = 0; i < items.length; i += 1) {
72+
var item = $(items[i]);
73+
var itemHtml = item.html();
74+
var startIndex = itemHtml.toLowerCase().indexOf(inputText);
75+
if (startIndex > 0) {
76+
if (item.find('span.highlight').length > 0) {
77+
var itemText = item.text();
78+
item.html(itemText);
79+
}
80+
}
81+
}
82+
}
83+
6484
$(document).ready(function() {
6585
$("#products").kendoComboBox({
6686
placeholder: "Select product",
@@ -77,6 +97,13 @@ Implement the `dataBound` event handler for the widget.
7797
url: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Products",
7898
}
7999
}
100+
},
101+
select: function (e) {
102+
var item = e.dataItem;
103+
// This checking is required. issue in kendo dropdown : the select event is triggered on blur, although it is not needed
104+
if (item !== null) {
105+
removeHighlight(e);
106+
}
80107
}
81108
});
82109
});

0 commit comments

Comments
 (0)