Created
June 18, 2016 16:44
-
-
Save Alex1990/c95cc6056c5833b1af5301777b1e0d28 to your computer and use it in GitHub Desktop.
Render the <option>s by the settings.
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
/** | |
* Redner the <option>s by the settings | |
*/ | |
function isObject(o) { | |
return Object.prototype.toString.call(o) === '[object Object]'; | |
} | |
function renderOptions(settings) { | |
var options = settings.options || []; | |
var selectedOptions = settings.selectedOptions || []; | |
var disabledOptions = settings.disabledOptions || []; | |
var html = ''; | |
if (!isObject(options[0])) { | |
options = options.map(function (option) { | |
return { | |
label: option, | |
value: option | |
}; | |
}); | |
} | |
for (var i = 0; i < options.length; i++) { | |
var option = options[i]; | |
var selected = option.selected !== undefined ? | |
option.selected : | |
selectedOptions.indexOf(option.value) > -1; | |
var disabled = option.disabled !== undefined ? | |
option.disabled : | |
disabledOptions.indexOf(option.value) > -1; | |
html += '<option value="' + option.value + '"' + | |
(selected ? 'selected' : '') + | |
(disabled ? 'disabled' : '') + | |
'>' + option.label + '</option>'; | |
} | |
return html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment