Search code examples
jquery-select2jquery-select2-4

select2 4.0 - TypeError: $(...).select2(...).select2(...) is undefined


Trying to finally change over to the 4.0 version of select2 and running into a problem.

All of this works perfectly fine on 3.5. On button click I open a bootstrap modal and load a remote page into it. I have tested all everything on a normal page (not in a modal) and it works correctly. When loaded in a modal as below the modal opens and everything like it always had for 3.5, but select2 is returning an error. I believe the issue here is the select2 is being loaded from a remote page... thing is this same remote loading method always worked flawlessly with 3.5.

TypeError: $(...).select2(...).select2(...) is undefined

js:

// show edit modal
$('#datatable2').on('click', '.dtEdit', function () {

    var data = {
      'filter_id': $(this).parents('tr').attr('id').replace('dtrow_', '')
    };

    $('#modal-ajax').load(
      '/modals/m_filtering_applications_filters.php',
      data,
      function() {
        $(this).modal('show');
        changeSettings();
        hourSelection();
      }
    );
});

// change filter modal confirmation
var changeSettings = function() {

    // get the default filter           
    var default_filter = $("#filter_default").val();

    //app list
    $("#vedit-filter").select2({
        placeholder: {
            id: default_filter, // or whatever the placeholder value is
            text: default_filter // the text to display as the placeholder
        },
        allowClear: true,
        multiple: false,
        tags: true,
        createTag: function (query) {
            return {
                id: query.term,
                text: query.term,
                tag: true
            }
        },
        ajax: {
            dataType: 'json',
            delay: 500,
            type: 'post',
            url: '/process/get_application_list.php',
            data: function (params) {
                return {
                    term: params.term, // search term
                    page: params.page, //page number
                    page_limit: 25, // page size
                };
            },
            results: function (data, page) {
                var more = (page * 25) < data.total; // whether or not there are more results available
                return {
                    results: data.results,
                    more: more
                };
            }
        }
    }).select2('val', [default_filter]).on('change', function() {
        $(this).valid();
    });

}

m_filtering_applications_filters.php :

Just the contents of the modal which is loaded in :

<div class="modal-dialog">
    <div class="modal-content">                             
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
            <h3 class="modal-title">Change these settings?</h3>
        </div>
        <form id="application-filters-edit">
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-12">

                        <div class="row">

                            <div class="col-md-12 margin-bottom-30 form-group">
                                <div class="input-modal-group">
                                    <label for="vedit-filter" class="f-14"><b>Application to filter :</b></label>
                                    <select id="vedit-filter" name="settings[filter]" class="form-control select2">
                                        <option value="<?php echo htmlspecialchars($result['filter'], ENT_QUOTES, 'UTF-8'); ?>" selected=""><?php echo htmlspecialchars($result['filter'], ENT_QUOTES, 'UTF-8'); ?></option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <input type="hidden" name="settings[users][]" value="<?php echo $result['user_id']; ?>"/>
                <input id="filter_default" type="hidden" name="settings[original]" value="<?php echo htmlspecialchars($result['filter'], ENT_QUOTES, 'UTF-8'); ?>"/>
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <?php
                    if (!$result)
                    {
                        //disable the button
                        echo '<button type="button" class="btn btn-primary disabled" data-dismiss="modal"><i class="fa fa-check-circle"></i> &nbsp;Save Settings</button>';
                    }
                    else
                    {
                        // show the button
                        echo '<button class="btn btn-primary" type="submit"><i class="fa fa-check-circle"></i> &nbsp;Save Settings</button>';
                    }
                ?>

            </div>
        </form>
    </div>
</div>

UPDATE:

Okay, the error is coming from :

}).select2('val', [default_filter]).on('change', function() {
    $(this).valid();
});

attached to the end... this is for using the jquery validation script (I did not include this in the jS here), and again, worked fine in 3.5. Can you not add on to the select2() anymore with 4.0 or something?

When removing this line the error goes away, but the display of the select2 is very small in width and I cannot gain focus on the search box to enter any values so it is still unusable within the modal.

UPDATE2:

Realized the events changed with 4.0 so this seems to work :

}).on("change", function (e) {
    $(this).valid();
});

Still cannot enter anything into the search box though. Also, I notice if I click on anything other than the arrow in the input box to show the dropdown it acts as if the mouse is being held down - it highlights all the text/content within the modal.


Solution

  • Solution : All the issues I was having with the select2 in my bs3 modal were solved by adding the following in my js :

    $.fn.modal.Constructor.prototype.enforceFocus = $.noop;
    

    The highlighting of content/text is gone. The focus on the search box is gone. For now everything seems to work great. I do not have any information as to what this line actually does at the moment, but will be researching this to find out and will add it back here for future reference for anyone.