2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2024-11-26 06:46:32 +00:00

Local suggestions should support different labels vs values.

This commit is contained in:
Adam St. John 2012-11-09 10:09:43 -05:00
parent dec9dc298e
commit 6eebf617cc

View File

@ -109,13 +109,16 @@
},
setOptions: function (options) {
var o = this.options;
var o = this.options, lookup;
this.extendOptions(options);
if (o.lookup || o.isLocal) {
this.isLocal = true;
if ($.isArray(o.lookup)) { o.lookup = { suggestions: o.lookup, data: [] }; }
lookup = o.lookup;
if (!lookup[0] || !lookup[0].label || !lookup[0].val) {
console.error("Jquery Autocomplete: Invalid 'lookup' format.")
}
}
$('#' + this.mainContainerId).css({ zIndex: o.zIndex });
@ -256,19 +259,23 @@
return $.trim(arr[arr.length - 1]);
},
// Suggestions should be an array of objects in the following
// format:
// {label: "Cows", value: "animal_id_387"}
getSuggestionsLocal: function (q) {
var ret, arr, len, val, i;
var ret, arr, len, val, label, i;
arr = this.options.lookup;
len = arr.suggestions.length;
len = arr.length;
ret = { suggestions: [], data: [] };
q = q.toLowerCase();
for (i = 0; i < len; i++) {
val = arr.suggestions[i];
if (val.toLowerCase().indexOf(q) === 0) {
ret.suggestions.push(val);
ret.data.push(arr.data[i]);
val = arr[i];
label = val.label;
if (label.toLowerCase().indexOf(q) === 0) {
ret.suggestions.push(label);
ret.data.push(val.val);
}
}