Last active
April 4, 2018 13:24
-
-
Save aleger/e5277e16047ebe31a2b56d2fb1caa420 to your computer and use it in GitHub Desktop.
Javascript helper to initialize Select2 elements and autocomplete
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
| //select2 | |
| destroySelect2: function(element) { | |
| $(element).empty(); | |
| $(element).select2('destroy'); | |
| }, | |
| initializeSelect2: function (element, options) { | |
| var defaultOptions = { | |
| placeholder: 'Select', | |
| allowClear: true | |
| }, | |
| newOptions = $.extend({}, defaultOptions, options); | |
| return $(element).select2(newOptions); | |
| }, | |
| //http://stackoverflow.com/questions/23132404/programatically-add-selected-option-into-select2 | |
| autoFillItemSelect2: function (element, item) { | |
| $(element) | |
| .append('<option selected="selected" value="' + item.id + '">' + item.text + '</option>') | |
| .trigger('change'); | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
USAGE
elementHelper.autoFillItemSelect2('#PolicySearchRequest_TwcCodes', { id: queryObject.twcCode, text: queryObject.twcCode });