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

select2 4.0 - simply way to clone select #2928

Closed
santaclaus21 opened this Issue Jan 15, 2015 · 3 comments

Comments

Projects
None yet
4 participants
@santaclaus21

santaclaus21 commented Jan 15, 2015

In case of 3.5.x the best way to clone select2 is $('.select2').destroy(); and then clone and $('.select2').select2();

Im using select2 sometimes ~40 times on single page and I have to use clone.. In that case destroy all and cloning is not fast..

So maybe there will be a simplest way to clone select2 (4.0) - preserving options and selected item?

@kevin-brown

This comment has been minimized.

Show comment
Hide comment
@kevin-brown

kevin-brown Jun 25, 2016

Member
$select.select2('destroy').clone().select2();

is still the recommended way to clone Select2 without having to worry about other elements or event handlers getting in the way.

Member

kevin-brown commented Jun 25, 2016

$select.select2('destroy').clone().select2();

is still the recommended way to clone Select2 without having to worry about other elements or event handlers getting in the way.

@hengsoheak

This comment has been minimized.

Show comment
Hide comment
@hengsoheak

hengsoheak Oct 6, 2016

I have the same issue with select2 version 2

if($(this).is('.glyphicon-plus')) {

        var tbodycopy = $(this).closest('#mbody');

        var get_clones = tbodycopy.clone();
        get_clones.children().find('select').select2();
        var selectors = get_clones.find('tr td a.glyphicon-plus')
        if(selectors.hasClass('glyphicon-plus')){
            selectors.removeClass('glyphicon-plus');
            selectors.addClass('glyphicon-minus');
        }
        $(get_clones).insertAfter(tbodycopy);
    }

After clone I will get tow selection one can select another one can not select.

hengsoheak commented Oct 6, 2016

I have the same issue with select2 version 2

if($(this).is('.glyphicon-plus')) {

        var tbodycopy = $(this).closest('#mbody');

        var get_clones = tbodycopy.clone();
        get_clones.children().find('select').select2();
        var selectors = get_clones.find('tr td a.glyphicon-plus')
        if(selectors.hasClass('glyphicon-plus')){
            selectors.removeClass('glyphicon-plus');
            selectors.addClass('glyphicon-minus');
        }
        $(get_clones).insertAfter(tbodycopy);
    }

After clone I will get tow selection one can select another one can not select.

@WindelOira

This comment has been minimized.

Show comment
Hide comment
@WindelOira

WindelOira Oct 8, 2018

This works for me :

var ul= $('ul'), li= $('ul li:last-of-type');

li.find('.is-select2').select2('destroy');
var cloned = li.clone();  

ul.append(cloned);

$.each($('.is-select2'), function(a) {
$(this).removeAttr('data-select2-id').select2();
});

Hope this helps :)

WindelOira commented Oct 8, 2018

This works for me :

var ul= $('ul'), li= $('ul li:last-of-type');

li.find('.is-select2').select2('destroy');
var cloned = li.clone();  

ul.append(cloned);

$.each($('.is-select2'), function(a) {
$(this).removeAttr('data-select2-id').select2();
});

Hope this helps :)

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