Skip to content

Instantly share code, notes, and snippets.

@Alex1990
Created June 18, 2016 16:44
Show Gist options
  • Save Alex1990/c95cc6056c5833b1af5301777b1e0d28 to your computer and use it in GitHub Desktop.
Save Alex1990/c95cc6056c5833b1af5301777b1e0d28 to your computer and use it in GitHub Desktop.
Render the <option>s by the settings.
/**
* 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