DubFriend / jquery.repeater

Create a repeatable group of input elements
MIT License
389 stars 191 forks source link

Form repeater is not working with select2 #110

Open mttariqi opened 5 years ago

mttariqi commented 5 years ago

I am using form repeater and selct2 together, at start it works perfectly fine, mean when page refresh once I click on add new, slect2 field repeat its self but its layout and everything got changed.

superipey commented 5 years ago

you should reinitiate select2 in show function

bebaps commented 5 years ago
Screen Shot 2019-09-06 at 4 58 37 PM

Here is an example of how you can get that to work. Note how you need to initiate the Select2 in both the ready and show methods. The initiation within the ready method will ensure that it works for the very first repeater, the initiation within the show method will ensure that it works within the additional repeaters added to the DOM.

In my case, I had to also tell Select2 where to attach additional instances, but your usage may vary.

garbinmarcelo commented 4 years ago

I am facing problems with select2. When I add a new line with select2 the previous one disappears. This package does not seem to receive updates for a long time, is there any alternative that has solved this problem? Do you still use it?

Thanks

cesarmtz93-wit commented 4 years ago

@garbinmarcelo did you manage to solve this? facing the same issue

garbinmarcelo commented 4 years ago

@garbinmarcelo did you manage to solve this? facing the same issue

Not really, I ended up working on another project in that time, but I would like to know if there is a solution for that.

cesarmtz93-wit commented 4 years ago

@garbinmarcelo i manage to solve my issue by doing this, hope this helps you

i can add multiple items and all will work with the select2 library

$('#repeater').repeater({
    initEmpty: false,
    isFirstItemUndeletable: true,
    show: function () {
        $(this).slideDown();
        $(this).find(".select2").select2();
    },
    hide: function (deleteElement) {
        $(this).slideUp(deleteElement);
    }   
});
mtariq786 commented 4 years ago

I did work for me, I got this somewhere from StackOverflow If I remember correctly, but forget the link!

    $(".select2").select2({
        placeholder: "Select Value",
    });

    $('.repeater-button').repeater({
        show: function () {
            $(this).slideDown();
            $('.select2-container').remove();
            $('.select2').select2({
                placeholder: "Select Value",
                allowClear: true
            });
            $('.select2-container').css('width','100%');
        },
        hide: function (remove) {
            if(confirm('Confirm Question')) {
                $(this).slideUp(remove);
            }
        }
    });
cristiano-federico commented 3 years ago

I'm having issues about this because everytime I add a new row the name is messed up with square brackets, ex: group-b[0][field_name[]][]

then if I add a new row the previous one removes square brackets and becomes group-b[0][field_name][] (as I think it should be) and the new one is messed up: group-b[1][field_name[]][]

jigapate commented 3 years ago

Is anyone able to work select2 with form repeater?

parminderjit-s commented 3 years ago

it doesn't work with select2 multiple selections.

naveedshahzadofficial commented 2 years ago

it is working, you should do this.

$('.m_repeater_section').repeater({
        initEmpty: false,

        defaultValues: {
            'text-input': 'foo'
        },

        show: function() {
            $(this).slideDown();
            $(this).find('.select2').removeClass('select2-hidden-accessible');
            $(this).find('.select2-container').remove();
             $(this).find('.select2').select2();
        },

        hide: function(deleteElement) {
            if(confirm('Are you sure you want to delete this element?')) {
                $(this).slideUp(deleteElement);
            }
        },
       isFirstItemUndeletable: true
    });
HasanMu commented 2 years ago

$(this).find('.select2').removeClass('select2-hidden-accessible'); $(this).find('.select2-container').remove(); $(this).find('.select2').select2();

Thank You! it's working fine

nicks6853 commented 1 year ago

If anyone is having problems with select2 in the repeater, what happened for me is that when a repeater item was added the select tags would have the same id as the first repeater item. As I would add more items, the previous selects would stop working. In my case I had two select tags inside each repeater item so I had to iterate through them (you'll see in the code). You can remove the id from the generated select in the new repeater item to fix the issue.

show: function () {
  $(this).slideDown();

  // Edit this selector for your select tags
  $(this).find('select[data-input-field="select-dropdown"]').each(function(index) {
    $(this).removeAttr('id');
    $(this).select2();
  });
},
htetzawphyo commented 1 year ago

how to remove search in select2

uberfresh commented 8 months ago

The solutions work with a normal select input but not with multiple=True ones. For example, if you create select2 input with multiple attribute and repeat them, only the last one will be initialized.