diff options
| author | Nana Nosirova <nargiza.nosirova@mail.utoronto.ca> | 2018-07-17 02:44:47 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-07-17 02:44:47 +0000 |
| commit | 62b53c0971765a4e5f5d447188eb90bd22571424 (patch) | |
| tree | d2f7aa3f3bb7b05cd5d3dee288eabf0f38d39b1a /assets/js/home_page.js | |
| parent | 541341598d5ed0e5c8ef9b2325dc6bbec958463a (diff) | |
| parent | 27f1cff089254b678bfcc695ab40b23a6a65a998 (diff) | |
Merge pull request #4 from kdam0/feat/SR-35
Feat/sr 35
Diffstat (limited to 'assets/js/home_page.js')
| -rw-r--r-- | assets/js/home_page.js | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/assets/js/home_page.js b/assets/js/home_page.js index 449461d..059444c 100644 --- a/assets/js/home_page.js +++ b/assets/js/home_page.js @@ -22,5 +22,53 @@ function main(){ console.log("Error: Click not registered"); } }); + + // TODO: move this function to a general helper class?? + // for auto-complete + var substringMatcher = function(strs) { + return function findMatches(q, cb) { + var matches, substringRegex; + // 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 + $.each(strs, function(i, str) { + if (substrRegex.test(str)) { + matches.push(str); + } + }); + + cb(matches); + }; + }; + + + // get array of all courses from controller + $.ajax({ + url: '/auto-complete-courses', + type: 'GET', + success: function(data){ + var jsonData = JSON.parse(data); + if (jsonData.success) { + var states = jsonData.data; + + $('.typeahead').typeahead({ + hint: true, + highlight: true, + minLength: 1 + }, + { + name: 'courseNames', + source: substringMatcher(states) + }); + } else { // console log the error mssg + console.log(jsonData.data); + } + } + }); } -$(document).ready(main);
\ No newline at end of file +$(document).ready(main); |
