-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allowEmptyOption does not treat empty options as normal options #739
Comments
I am having the same issue here, after initialise with
|
+1 |
1 similar comment
+1 |
+1 too, this does not seem to work. As a workaround, we do not use the Note that this allows us to keep the placeholder and get the blank option treated as a real option. Example code : <select data-selectize>
<option value="">Select a value ...</option> <!-- Placeholder -->
<option value="__blank__">None</option> <!-- Blank option -->
<option value="1">Actual value</option> <!-- Real value -->
</select> Then initialize selectize and handle the $field = $('[data-selectize]').selectize()
$field.on('change', function() {
if($field.val() === '__blank__') { $field.val(''); }
}); |
+1 |
2 similar comments
+1 |
+1 |
There is a bug with the `allowEmptyOption` setting in 0.12.x. See: * selectize/selectize.js#749 * selectize/selectize.js#967 * selectize/selectize.js#739
This looks like an omission in the logic added in c8f2944 to implement allowEmptyOption. Empty options are parsed from the original Line 1187 of src/selectize.js should be changed: - if (!key || this.options.hasOwnProperty(key)) return false;
+ if (typeof key === 'undefined' || key === null || this.options.hasOwnProperty(key)) return false; Hopefully I'll find the time soon to set up a pull request with this change. You can monkeypatch this without modifying the original source by running this code after loading selectize.js: var hash_key = function(value) {
if (typeof value === 'undefined' || value === null) return null;
if (typeof value === 'boolean') return value ? '1' : '0';
return value + '';
};
$.extend(Selectize.prototype, {
registerOption: function(data) {
var key = hash_key(data[this.settings.valueField]);
// Line 1187 of src/selectize.js should be changed
// if (!key || this.options.hasOwnProperty(key)) return false;
if (typeof key === 'undefined' || key === null || this.options.hasOwnProperty(key)) return false;
data.$order = data.$order || ++this.order;
this.options[key] = data;
return key;
}
}); If the text of your option is also blank, you may need to modify your CSS or rendering function to ensure that the resulting data-selectable div does not collapse down to an unusable size. I overwrote the default rendering functions as shown below: var originalSetupTemplates = Selectize.prototype.setupTemplates;
$.extend(Selectize.prototype, {
setupTemplates: function(data) {
var self = this;
var field_label = self.settings.labelField;
var field_optgroup = self.settings.optgroupLabelField;
var templates = {
'option': function(data, escape) {
return '<div class="option">' + ( $.trim(data[field_label]).length ? escape(data[field_label]) : ' ' ) + '</div>';
},
'item': function(data, escape) {
return '<div class="item">' + ( $.trim(data[field_label]).length ? escape(data[field_label]) : ' ' ) + '</div>';
}
};
self.settings.render = $.extend({}, templates, self.settings.render);
originalSetupTemplates.apply(this, arguments);
}
}); I'm not sure whether there's a more general-purpose way of handling this aspect of the issue. |
+1 |
It's also ignored by the API AFAICT so passing in options with a blank string with allowEmptyOption set to true just discards it. We settled for 'none' as the value but would prefer an empty string (or null) as it's cleaner. |
Tentative fix in a12d99f; please test on master |
According to the documentation
allowEmptyOption:true
should enable empty value options being treated as normal options. However, it seems they are still not selectable.This demo is taken straight out of the examples folder:
Here none is not actually a clickable option. It disappears once the dropdown is activated, and thus seems more like a placeholder? Is this the wanted behaviour or did I miss something here?
The text was updated successfully, but these errors were encountered: