jquery.repeater icon indicating copy to clipboard operation
jquery.repeater copied to clipboard

Form repeater is not working with select2

Open mttariqi opened this issue 5 years ago • 16 comments

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.

mttariqi avatar Aug 08 '19 13:08 mttariqi

you should reinitiate select2 in show function

superipey avatar Sep 02 '19 01:09 superipey

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.

bebaps avatar Sep 07 '19 00:09 bebaps

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

garbinmarcelo avatar Mar 05 '20 19:03 garbinmarcelo

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

cesarmtz93-wit avatar Jul 20 '20 20:07 cesarmtz93-wit

@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.

garbinmarcelo avatar Jul 20 '20 20:07 garbinmarcelo

@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);
	}   
});

cesarmtz93-wit avatar Jul 28 '20 20:07 cesarmtz93-wit

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);
            }
        }
    });

mtariq786 avatar Jul 29 '20 04:07 mtariq786

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[]][]

cristiano-federico avatar Sep 18 '20 07:09 cristiano-federico

Is anyone able to work select2 with form repeater?

jigapate avatar Oct 19 '20 08:10 jigapate

it doesn't work with select2 multiple selections.

parminderjit-s avatar Jul 09 '21 10:07 parminderjit-s

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
    });
    

naveedshahzadofficial avatar Jan 13 '22 13:01 naveedshahzadofficial

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

Thank You! it's working fine

HasanMu avatar Mar 11 '22 03:03 HasanMu

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();
  });
},

nicks6853 avatar Dec 05 '22 14:12 nicks6853

how to remove search in select2

htetzawphyo avatar Apr 01 '23 08:04 htetzawphyo

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.

uberfresh avatar Dec 14 '23 09:12 uberfresh