Skip to content

Commit 6272ce2

Browse files
committed
implemented valueKey
1 parent a074f50 commit 6272ce2

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/jquery.autocomplete.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
serviceUrl: null,
5959
lookup: null,
6060
keyPath: "suggestions",
61+
valueKey: "value",
6162
onSelect: null,
6263
width: 'auto',
6364
minChars: 1,
@@ -118,10 +119,10 @@
118119

119120
$.Autocomplete = Autocomplete;
120121

121-
Autocomplete.formatResult = function (suggestion, currentValue) {
122+
Autocomplete.formatResult = function (suggestion, currentValue, context) {
122123
var pattern = '(' + utils.escapeRegExChars(currentValue) + ')';
123124

124-
return suggestion.value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>');
125+
return suggestion[context.options.valueKey].replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>');
125126
};
126127

127128
Autocomplete.prototype = {
@@ -429,7 +430,7 @@
429430
queryLowerCase = query.toLowerCase();
430431

431432
$.each(that.suggestions, function (i, suggestion) {
432-
if (suggestion.value.toLowerCase() === queryLowerCase) {
433+
if (suggestion[that.options.valueKey].toLowerCase() === queryLowerCase) {
433434
index = i;
434435
return false;
435436
}
@@ -571,7 +572,7 @@
571572

572573
// Build suggestions inner HTML:
573574
$.each(that.suggestions, function (i, suggestion) {
574-
html += '<div class="' + className + '" data-index="' + i + '">' + formatResult(suggestion, value) + '</div>';
575+
html += '<div class="' + className + '" data-index="' + i + '">' + formatResult(suggestion, value, that) + '</div>';
575576
});
576577

577578
// If width is auto, adjust width before displaying suggestions,
@@ -611,7 +612,7 @@
611612
}
612613

613614
$.each(that.suggestions, function (i, suggestion) {
614-
var foundMatch = suggestion.value.toLowerCase().indexOf(value) === 0;
615+
var foundMatch = suggestion[that.options.valueKey].toLowerCase().indexOf(value) === 0;
615616
if (foundMatch) {
616617
bestMatch = suggestion;
617618
}
@@ -625,7 +626,7 @@
625626
var hintValue = '',
626627
that = this;
627628
if (suggestion) {
628-
hintValue = that.currentValue + suggestion.value.substr(that.currentValue.length);
629+
hintValue = that.currentValue + suggestion[that.options.valueKey].substr(that.currentValue.length);
629630
}
630631
if (that.hintValue !== hintValue) {
631632
that.hintValue = hintValue;
@@ -751,7 +752,7 @@
751752
$(that.suggestionsContainer).scrollTop(offsetTop - that.options.maxHeight + heightDelta);
752753
}
753754

754-
that.el.val(that.getValue(that.suggestions[index].value));
755+
that.el.val(that.getValue(that.suggestions[index].this[valueKey]));
755756
that.signalHint(null);
756757
},
757758

@@ -760,7 +761,7 @@
760761
onSelectCallback = that.options.onSelect,
761762
suggestion = that.suggestions[index];
762763

763-
that.currentValue = that.getValue(suggestion.value);
764+
that.currentValue = that.getValue(suggestion[that.valueKey]);
764765
that.el.val(that.currentValue);
765766
that.signalHint(null);
766767
that.suggestions = [];

0 commit comments

Comments
 (0)