Skip to content
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

Closed
esbenp opened this issue Mar 16, 2015 · 11 comments
Closed

allowEmptyOption does not treat empty options as normal options #739

esbenp opened this issue Mar 16, 2015 · 11 comments

Comments

@esbenp
Copy link

esbenp commented Mar 16, 2015

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:

<div class="control-group">
    <label for="select-beast-empty">Beast:</label>
    <select id="select-beast-empty" class="demo-default" data-placeholder="Select a person...">
        <option value="">None</option>
        <option value="4">Thomas Edison</option>
        <option value="1">Nikola</option>
        <option value="3">Nikola Tesla</option>
        <option value="5">Arnold Schwarzenegger</option>
    </select>
</div>
<script>
$('#select-beast-empty').selectize({
    allowEmptyOption: true,
    create: true
});
</script>

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?

@maxhungry
Copy link

I am having the same issue here, after initialise with allowEmptyOption I was expecting either one of the option below to be selectable:

<select>
  <option value>None</option>
  <option value="">None</option>
</select>

@tramck
Copy link

tramck commented Jun 1, 2015

+1

1 similar comment
@Matheuslucena
Copy link

+1

@vala
Copy link

vala commented Sep 17, 2015

+1 too, this does not seem to work.

As a workaround, we do not use the allowEmptyOption option, and use a select option with a special value <option value="__blank__">None</option>, listen to the change event on the field and reset its value if the value is "__blank__"

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 __blank__ value manually :

$field = $('[data-selectize]').selectize()

$field.on('change', function() {
  if($field.val() === '__blank__') { $field.val(''); }
});

@smoral
Copy link

smoral commented Sep 29, 2015

+1

2 similar comments
@federico-bellucci
Copy link

+1

@ryanmortier
Copy link

+1

derekprior added a commit to MIT-IR/abet that referenced this issue Jan 5, 2016
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
@mcavalletto
Copy link

This looks like an omission in the logic added in c8f2944 to implement allowEmptyOption.

Empty options are parsed from the original <select> tag, but then lost during the call to Selectize.registerOption().

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]) : '&nbsp;' ) + '</div>';
                },
                'item': function(data, escape) {
                    return '<div class="item">' + ( $.trim(data[field_label]).length ? escape(data[field_label]) : '&nbsp;' ) + '</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.

@andrefigueira
Copy link

+1

vala added a commit to glyph-fr/simple_form_extension that referenced this issue Mar 3, 2016
@joshstrange
Copy link

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.

@joallard
Copy link
Member

joallard commented Jun 8, 2016

Tentative fix in a12d99f; please test on master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests