2

Afternoon,

On a website I already updated lots of elements that used select2 v. 3.5.2 to the new v. 4.0 (and some of those elements are on the header of the website that is present on ALL pages)

Unfortunately on some pages of the website the X-editable jquery plugin is used, and that plugin doesn't play well with v 4.0 of select2 (please read: does not play at all)

My question is: Can I use both versions of select2 on some pages?

And if so, how? Since $(...).select2(); is blind to which version of select2 is loaded....

Example code:

    $("#search_species").select2({
        minimumInputLength: 1,
        maximumSelectionSize: 1,
        ajax: {
            url: "/php/search.php",
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return {
                    query: params.term
                };
            },
            processResults: function (data, params) {
                return { results: data };
            },
            cache: true
        },
        escapeMarkup: function (markup) { return markup; },k

        templateResult: formatarResultados,
        templateSelection: formatarSeleccao,

    }).on("select2:selecting", function(e) {
        console.log(e);

        // window.location.href = e.params.args.data.url;
        // $('.select2-drop').hide();

    });
Afonso Gomes
  • 902
  • 1
  • 14
  • 40
  • Can I edit the select2-3.2.2.min.js file and make it so that I can use something like $(...).select3(...); ? – Afonso Gomes Nov 27 '15 at 17:51
  • There's a [modified version of x-editable](https://github.com/select2/x-editable) designed to work with Select2 4.0.x. It's not heavily maintained (read: only occasionally), so YMMV. – Kevin Brown-Silva Nov 28 '15 at 02:50

1 Answers1

7

It should be possible (but not necessarily easy) to isolate Select2 4.0.0. It is nearly impossible to isolate older versions because they rely on global variables, while Select2 4.0.0 is actually pretty self-contained (with some exceptions).

I'm guessing you are running into a situation similar to the following:

$(".select2").select2({
  ajax: {}
});
<link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.css" rel="stylesheet"/>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.js"></script>

<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.css " rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.js"></script>

<select class="select2">
  <option value="AL">Alabama</option>
  <option value="AK">Alaska</option>
  <option value="AZ">Arizona</option>
</select>

You can see both Select2 4.0.0 and 3.5.2 are being loaded in the same bin, and 3.5.2 is winning the battle.

But this can be fixed by taking a reference to $.fn.select2 after you load the plugin. You can then either call this function directly (using .call()) or re-set the reference when you need it. I would personally recommend calling the function directly, so other plugins aren't breaking because of you.

myOwnSelect2.call($(".select2"), {
  ajax: {}
});

$(".select3").select2();
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>

<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.css " rel="stylesheet" />
<script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.js"></script>

<script>
  var myOwnSelect2 = $.fn.select2;
  delete $.fn.select2;
</script>

<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.css " rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.js"></script>

<select class="select2">
  <option value="AL">Alabama</option>
  <option value="AK">Alaska</option>
  <option value="AZ">Arizona</option>
</select>

<select class="select3">
  <option value="AL">Alabama</option>
  <option value="AK">Alaska</option>
  <option value="AZ">Arizona</option>
</select>

And using the function directly has the benefit of both Select2 3.x and 4.x working on the same page without any problems.

Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
  • Sorry for the incredibly late reply. I've been completely dragged to a different project and only now could get back to this one. When I first saw your proposed solution I got hopeful ... seemed there was a way. But now that I got to try it out ... I feel hopeless again! It didn't work... defaults to select2 v3.5.2. I must be doing something wrong. I set up a little test page stripped of everything except the selects to better explain: http://jbteste.utad.pt/admin/TESTE.php?idEspecie=1 select on the left is supposed to be 4.0 on the header of every pages. other one is for x-editable use. – Afonso Gomes Jan 22 '16 at 04:08
  • Btw: http://jbteste.utad.pt/admin/TESTE2.php?idEspecie=1 shows the select2 v4 working as it should (with my initial code) but in this case the x-editable doesn't work :| – Afonso Gomes Jan 22 '16 at 04:17
  • @AfonsoGomes Your first link [works for me](https://i.imgur.com/ctwsgUw.png) (left is v4, right is v3 through x-editable). Your second link doesn't because the version of x-editable you are using isn't compatible with v4, and that is the version taking priority. – Kevin Brown-Silva Jan 25 '16 at 00:35
  • Really? On the first link, by looking at it the select on the left seems to have the v3.5 applied to it. I say this because on the first link the select on the left displays images and on the second link the select on the left doesn't! – Afonso Gomes Jan 27 '16 at 00:59
  • 1
    This is **amazing** ! After a few hours of banging my head against the wall, your answer helped me fix my bug. If someone is in a situation where they need to do this fix after a select2 version is already loaded, here is how i've done it: ` ` Then you can use it `myOwnSelect2Ver.call($(".select2"), { options });` – Andrei Telteu Sep 26 '18 at 16:31