Skip to content

Commit

Permalink
Merge pull request marcj#150 from bogdanRada/master
Browse files Browse the repository at this point in the history
Small fix for when selectbox destroyed
  • Loading branch information
marcj committed Jun 27, 2015
2 parents 42d2a14 + 93e2444 commit e103503
Showing 1 changed file with 55 additions and 41 deletions.
96 changes: 55 additions & 41 deletions jquery.selectBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*
* https://github.com/marcj/jquery-selectBox
*/


;(function ($) {

/**
Expand Down Expand Up @@ -984,44 +986,7 @@
}
};

/**
* Sets the option elements.
*
* @param {String|Object} options
*/
SelectBox.prototype.setOptions = function (options) {
var select = $(this.selectElement)
, control = select.data('selectBox-control');

switch (typeof(options)) {
case 'string':
select.html(options);
break;
case 'object':
select.html('');
for (var i in options) {
if (options[i] === null) {
continue;
}
if (typeof(options[i]) === 'object') {
var optgroup = $('<optgroup label="' + i + '" />');
for (var j in options[i]) {
optgroup.append('<option value="' + j + '">' + options[i][j] + '</option>');
}
select.append(optgroup);
} else {
var option = $('<option value="' + i + '">' + options[i] + '</option>');
select.append(option);
}
}
break;
}

if (control) {
// Refresh the control
this.refresh();
}
};

/**
* Disables the selection.
Expand Down Expand Up @@ -1059,7 +1024,52 @@
* Extends the jQuery.fn object.
*/
$.extend($.fn, {
selectBox: function (method, options) {

/**
* Sets the option elements.
*
* @param {String|Object} options
*/
setOptions : function (options) {
var select = $(this)
, control = select.data('selectBox-control');


switch (typeof(options)) {
case 'string':
select.html(options);
break;
case 'object':
select.html('');
for (var i in options) {
if (options[i] === null) {
continue;
}
if (typeof(options[i]) === 'object') {
var optgroup = $('<optgroup label="' + i + '" />');
for (var j in options[i]) {
optgroup.append('<option value="' + j + '">' + options[i][j] + '</option>');
}
select.append(optgroup);
} else {
var option = $('<option value="' + i + '">' + options[i] + '</option>');
select.append(option);
}
}
break;
}

if (control) {
// Refresh the control
$(this).selectBox('refresh');
// Remove old options

}
},



selectBox: function (method, options) {
var selectBox;

switch (method) {
Expand All @@ -1075,14 +1085,18 @@
break;
case 'options':
// Getter

if (undefined === options) {
return $(this).data('selectBox-control').data('selectBox-options');
}

// Setter
$(this).each(function () {
if (selectBox = $(this).data('selectBox')) {
selectBox.setOptions(options);
}

// if (selectBox = $(this).data('selectBox')) {
$(this).setOptions(options);
// }

});
break;
case 'value':
Expand Down

0 comments on commit e103503

Please sign in to comment.