jquery.repeater
jquery.repeater copied to clipboard
Form repeater is not working with select2
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.
you should reinitiate select2 in show function

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.
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 did you manage to solve this? facing the same issue
@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 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);
}
});
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);
}
}
});
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[]][]
Is anyone able to work select2 with form repeater?
it doesn't work with select2 multiple selections.
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
});
$(this).find('.select2').removeClass('select2-hidden-accessible'); $(this).find('.select2-container').remove(); $(this).find('.select2').select2();
Thank You! it's working fine
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();
});
},
how to remove search in select2
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.