Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select2 allowClear not enabled when options set dynamically

When I create select2 dropdowns that are dynamically driven by the selection in another select2 dropdown, the allowClear button for the updated dropdown becomes disabled.

It doesn't seem to matter if I build select2 on a select, destroy it, update the html, and rebuild it:

var enableSelect2 = function () {
        $(this).select2({
            width: '200px',
            allowClear: true,
            minimumResultsForSearch: 7,
            formatResult: function (result, container, query, escapeMarkup) {
                var markup = [];
                markMatchedSelect2Text(result.text, query.term, markup, escapeMarkup);
                return markup.join('');
            }
        });
    },
    populateDropdown = function () {
        var filterBy = this.id,
            t = $(this);
        $.ajax({
            type: 'post',
            url: '/search/get' + (filterBy === 'panel_id' ? 'Isps' : 'Packages') + '/' + t.val(),
            success: function (data) {
                var toRebuild,
                    target;
                if (filterBy === 'panel_id') {
                    toRebuild = $('#isp_id, #package_id');
                    target =  $('#isp_id');
                } else {
                    toRebuild = $('#package_id');
                    target = $('#package_id');
                }
                toRebuild.each(function () {
                    $(this).select2('destroy');
                });
                target.html(data);
                if (filterBy === 'panel_id') {
                    $('#package_id').html($(document.createElement('option')).attr('value', 0).text('Select ISP first\u2026'));
                }
                toRebuild.each(enableSelect2);
            }
        });
    };

$('body').on('change', '#searchForm #isp_id, #searchForm #panel_id', populateDropdown);

Or if I use JSON with a hidden input:

$(function() {
    var data = [
        [{id:0,text:'black'},{id:1,text:'blue'}],
        [{id:0,text:'9'},{id:1,text:'10'}]
    ];

    $('#attribute').select2({allowClear: true}).on('change', function() {
        $('#value').removeClass('select2-offscreen').select2({data:data[$(this).val()],allowClear: true});
    }).trigger('change');
});

http://jsfiddle.net/eGXPe/116/

Any ideas why the clear button disappears?

Edit:

Apologies that I did not clarify my html. In my code, every select has a data-placeholder attribute. This is not in the fiddle I provided, as it was not originally my fiddle but borrowed from another SO question. I have now updated that fiddle with data-placeholder and it works: http://jsfiddle.net/eGXPe/119/.

Here is the twig code for my html which I did not previously include:

<li>
    <label for="edit[panel_id]" class="hidden">Edit Panel ID?</label>
    <input type="checkbox" id="edit[panel_id]" name="edit[panel_id]" />
    <label for="panel_id">Panel:</label>
    <select id="panel_id" name="panel_id" data-placeholder="Select a panel">
        <option></option>
        {% for panel in related.panel_id %}
            <option value="{{ panel.value }}">{{ panel.name }}</option>
        {% endfor %}
    </select>
</li>
<li>
    <label for="edit[isp_id]" class="hidden">Edit ISP ID?</label>
    <input type="checkbox" id="edit[isp_id]" name="edit[isp_id]" />
    <label for="isp_id">ISP:</label>
    <select id="isp_id" name="isp_id" data-placeholder="Select an ISP">
        <option></option>
        {% for isp in related.isp_id %}
            <option value="{{ isp.value }}">{{ isp.name }}</option>
        {% endfor %}
    </select>
</li>
<li>
    <label for="edit[package_id]" class="hidden">Edit Package ID?</label>
    <input type="checkbox" id="edit[package_id]" name="edit[package_id]" />
    <label for="package_id">Package:</label>
    <select id="package_id" name="package_id" data-placeholder="Select a package">
        <option></option>
        <option value="0">Select ISP first&hellip;</option>
    </select>
</li>
like image 482
Derek Henderson Avatar asked Nov 04 '13 10:11

Derek Henderson


2 Answers

As stated in the doc, allowClear needs a placeholder and placeholder need a corresponding option value (which cannot be an empty string, but can be a single space).

allowClear

This option only works when the placeholder is specified.

--

placeholder

Note that because browsers assume the first option element is selected in non-multi-value select boxes an empty first option element must be provided () for the placeholder to work.

So your code should be something like this :

$('#attribute').select2({
    allowClear: true,
    placeholder: "Select an attribute"
}).on('change', function() {
    $('#value')
        .removeClass('select2-offscreen')
        .select2({
            data:data[$(this).val()],
            allowClear: true,
            placeholder: "Select a value"
        });
}).trigger('change'); 

http://jsfiddle.net/eGXPe/118/

like image 135
Shikiryu Avatar answered Sep 27 '22 20:09

Shikiryu


'Select2: The allowClear option should be used in combination ' + 'with the placeholder option.'

like image 33
Lundrim Rexhepi Avatar answered Sep 27 '22 20:09

Lundrim Rexhepi