-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathjquery.select-autocomplete.js
56 lines (48 loc) · 1.74 KB
/
jquery.select-autocomplete.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
jQuery.fn.select_autocomplete = function( options ) {
return this.each(function(){
if (this.tagName.toLowerCase() != 'select') { return; }
//stick each of it's options in to an items array of objects with name and value attributes
var select = this;
var items = [];
$(select).children('option').each(function(){
var item = $(this);
if (item.val() != '') //ignore empty value options
{
var name = item.html();
var value = item.val();
items.push( {'name':name, 'value':value} );
}
});
//make a new input box next to the select list
var input = $("<input type='text' />").appendTo('body');
$(select).after(input);
//make the input box into an autocomplete for the select items
var defaults = {
data: items,
minChars: 0,
width: 310,
matchContains: false,
autoFill: false,
formatItem: function(row, i, max) {
return row.name;
},
formatMatch: function(row, i, max) {
return row.name;
},
formatResult: function(row) {
return row.name;
}
};
jQuery.extend( defaults, options );
$(input).autocomplete(items, defaults);
//make the result handler set the selected item in the select list
$(input).result(function(event, selected_item, formatted) {
var to_be_selected = $(select).find("option[value=" + selected_item.value + "]")[0];
$(to_be_selected).attr('selected', 'selected');
});
//set the initial text value of the autocomplete input box to the text node of the selected item in the select control
$(input).val($(select).find(':selected').text());
//normally, you'd hide the select list but we won't for this demo
$(select).hide();
});
};