Skip to content

Commit e103503

Browse files
committed
Merge pull request marcj#150 from bogdanRada/master
Small fix for when selectbox destroyed
2 parents 42d2a14 + 93e2444 commit e103503

File tree

1 file changed

+55
-41
lines changed

1 file changed

+55
-41
lines changed

jquery.selectBox.js

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*
88
* https://github.com/marcj/jquery-selectBox
99
*/
10+
11+
1012
;(function ($) {
1113

1214
/**
@@ -984,44 +986,7 @@
984986
}
985987
};
986988

987-
/**
988-
* Sets the option elements.
989-
*
990-
* @param {String|Object} options
991-
*/
992-
SelectBox.prototype.setOptions = function (options) {
993-
var select = $(this.selectElement)
994-
, control = select.data('selectBox-control');
995-
996-
switch (typeof(options)) {
997-
case 'string':
998-
select.html(options);
999-
break;
1000-
case 'object':
1001-
select.html('');
1002-
for (var i in options) {
1003-
if (options[i] === null) {
1004-
continue;
1005-
}
1006-
if (typeof(options[i]) === 'object') {
1007-
var optgroup = $('<optgroup label="' + i + '" />');
1008-
for (var j in options[i]) {
1009-
optgroup.append('<option value="' + j + '">' + options[i][j] + '</option>');
1010-
}
1011-
select.append(optgroup);
1012-
} else {
1013-
var option = $('<option value="' + i + '">' + options[i] + '</option>');
1014-
select.append(option);
1015-
}
1016-
}
1017-
break;
1018-
}
1019989

1020-
if (control) {
1021-
// Refresh the control
1022-
this.refresh();
1023-
}
1024-
};
1025990

1026991
/**
1027992
* Disables the selection.
@@ -1059,7 +1024,52 @@
10591024
* Extends the jQuery.fn object.
10601025
*/
10611026
$.extend($.fn, {
1062-
selectBox: function (method, options) {
1027+
1028+
/**
1029+
* Sets the option elements.
1030+
*
1031+
* @param {String|Object} options
1032+
*/
1033+
setOptions : function (options) {
1034+
var select = $(this)
1035+
, control = select.data('selectBox-control');
1036+
1037+
1038+
switch (typeof(options)) {
1039+
case 'string':
1040+
select.html(options);
1041+
break;
1042+
case 'object':
1043+
select.html('');
1044+
for (var i in options) {
1045+
if (options[i] === null) {
1046+
continue;
1047+
}
1048+
if (typeof(options[i]) === 'object') {
1049+
var optgroup = $('<optgroup label="' + i + '" />');
1050+
for (var j in options[i]) {
1051+
optgroup.append('<option value="' + j + '">' + options[i][j] + '</option>');
1052+
}
1053+
select.append(optgroup);
1054+
} else {
1055+
var option = $('<option value="' + i + '">' + options[i] + '</option>');
1056+
select.append(option);
1057+
}
1058+
}
1059+
break;
1060+
}
1061+
1062+
if (control) {
1063+
// Refresh the control
1064+
$(this).selectBox('refresh');
1065+
// Remove old options
1066+
1067+
}
1068+
},
1069+
1070+
1071+
1072+
selectBox: function (method, options) {
10631073
var selectBox;
10641074

10651075
switch (method) {
@@ -1075,14 +1085,18 @@
10751085
break;
10761086
case 'options':
10771087
// Getter
1088+
10781089
if (undefined === options) {
10791090
return $(this).data('selectBox-control').data('selectBox-options');
10801091
}
1092+
10811093
// Setter
10821094
$(this).each(function () {
1083-
if (selectBox = $(this).data('selectBox')) {
1084-
selectBox.setOptions(options);
1085-
}
1095+
1096+
// if (selectBox = $(this).data('selectBox')) {
1097+
$(this).setOptions(options);
1098+
// }
1099+
10861100
});
10871101
break;
10881102
case 'value':

0 commit comments

Comments
 (0)