-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path-
48 lines (40 loc) · 1.48 KB
/
-
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
// array if gove_name strings
//var gov_name_values = gov_names.map ( function(e){return e.gov_name;} );
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substrRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
//var counter=0;
$.each(strs, function(i, str) {
if (substrRegex.test(str.gov_name)) {
// the typeahead jQuery plugin expects suggestions to a
// JavaScript object, refer to typeahead docs for more info
matches.push(str);
if (matches.length >= 10) return false;
}
});
cb(matches);
};
};
var suggestionTemplate = Handlebars.compile('<p><span class="minwidth">{{gov_name}}</span> <span class="smaller">{{state}} {{zip}}</span></p>');
var ta = $('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
}, {
name: 'gov_name',
displayKey: 'gov_name',
source: substringMatcher(govs),
templates: {
suggestion: suggestionTemplate
}
});
// Attach initialized event to it
ta.on('typeahead:selected', function(evt, data) {
console.log(data); //selected datum object
});