DubFriend / jquery.repeater

Create a repeatable group of input elements
MIT License
391 stars 192 forks source link

Delete confirmation triggered twice in an inner repeater #42

Open tommica opened 7 years ago

tommica commented 7 years ago

My JS looks like this:

    $('.out_repeater').repeater({
        show: function () {
            $(this).slideDown();
        },
        hide: function (deleteElement) {
            if (confirm('Are you sure you want to delete this element?')) {
                $(this).slideUp(deleteElement);
            }
        },
        isFirstItemUndeletable: true,
        repeaters: [{
            selector: '.in_repeater',
            show: function () {
                $(this).slideDown();
            },
            hide: function (deleteElement) {
                if (confirm('Are you sure you want to delete this element?')) {
                    $(this).slideUp(deleteElement);
                }
            }
        }]
    });

For some reason when the delete button for an item inside the "in_repeater", it triggers the "Are you sure" confirmation twice.

Everything else works fine!

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.repeater/1.2.1/jquery.repeater.min.js"></script>
dragosstancu commented 7 years ago

The click event is being captured at list level and, for nested repeaters, it happens as many times as deep the nesting is.

Replace that in jquery.repeater.js, towards the end.

$list.on('click', '[data-repeater-delete]', function(event) { event.stopPropagation(); // added this var self = $(this).closest('[data-repeater-item]').get(0); hide.call(self, function() { $(self).remove(); setIndexes($items(), getGroupName(), fig.repeaters); }); });

havanan commented 6 years ago

I also have a similar problem, but when I change the code of the dragosstancu, still repeated the warning: "Are you sure"

martinmurciego commented 5 years ago

Please, give a more detailed example in jsfiddle or codepen about eliminating nested repeaters, since I have this same problem. As I must respond with a different behavior to the slideUp and slideDown of each repeater I need to be able to identify each one: https://github.com/DubFriend/jquery.repeater/issues/98#issue-383246502 Some alternative or it will be that the developer can update this suggested change in a new version.

$list.on('click', '[data-repeater-delete]', function(event) {
event.stopPropagation(); // added this
var self = $(this).closest('[data-repeater-item]').get(0);
hide.call(self, function() {
$(self).remove();
setIndexes($items(), getGroupName(), fig.repeaters);
});
});
dragosstancu commented 5 years ago

Hi guys, All I can do for now is to post a link to my page that demonstrates the nested repeaters: https://demo.webfixtech.com/repeater/

martinmurciego commented 5 years ago

Cool. Thank you for sharing this.

ilic993 commented 5 years ago

@dragosstancu Thank you for the fix, I hope this gets included in the next release.

AbhishekGandhi7 commented 3 years ago

$list.on('click', '[data-repeater-delete]', function(event) { event.stopImmediatePropagation(); // add this, It works for me perfectly // event.stopPropagation(); var self = $(this).closest('[data-repeater-item]').get(0); hide.call(self, function() { $(self).remove(); setIndexes($items(), getGroupName(), fig.repeaters); }); });

Thanks @martinmurciego it's save my day.