Last active
May 20, 2016 16:48
-
-
Save alexweissman/07ab1ad90c345ff9fe5a000b063dcbc7 to your computer and use it in GitHub Desktop.
load some kind of data from a JSON API into a dropdown menu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "count": 6, | |
| "rows": [ | |
| { | |
| "id": 1, | |
| "name": "Indiana University Bloomington" | |
| }, | |
| { | |
| "id": 2, | |
| "name": "Ivy Tech Bloomington" | |
| }, | |
| { | |
| "id": 5, | |
| "name": "Loyola" | |
| }, | |
| { | |
| "id": 6, | |
| "name": "Montgomery College Rockville" | |
| }, | |
| { | |
| "id": 3, | |
| "name": "University of Maryland College Park" | |
| }, | |
| { | |
| "id": 4, | |
| "name": "University of Maryland University College" | |
| } | |
| ], | |
| "count_filtered": 6 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- This is the dropdown item to populate with messages --> | |
| <li class="dropdown"> | |
| <a class="dropdown-toggle" data-toggle="dropdown" href="#"> | |
| <i class="fa fa-university fa-fw"></i> <span class="dropdown-value"></span> <i class="fa fa-caret-down"></i> | |
| </a> | |
| <ul class="dropdown-menu" id="main-menu-institutions"> | |
| </ul> | |
| <!-- /.dropdown-institutions --> | |
| </li> | |
| <script> | |
| $(document).ready( function () { | |
| /** | |
| * Institution menu - for selecting current institution context | |
| */ | |
| $('#main-menu-institutions').viewMenu({ | |
| 'institutionUrl': site.uri.public + '/api/institutions' | |
| }).on("change", function () { | |
| // Store selected institution_id in localStorage | |
| localStorage.setItem('institution_id', $(this).data('selected-id')); | |
| // Debug - remove in production | |
| console.log(localStorage.getItem('institution_id')); | |
| }).on("loaded", function () { | |
| // Add a final "all" option. This will set the context as "all institutions I have access to" | |
| $('#main-menu-institutions').viewMenu('addItem', { | |
| id: "-1", | |
| name: "All institutions", | |
| alternate: true | |
| }); | |
| // Set value on page load from localStorage | |
| var institution_id = localStorage.getItem('institution_id'); | |
| if (institution_id !== null) { | |
| $('#main-menu-institutions').viewMenu('set', institution_id); | |
| } | |
| }); | |
| }); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment