DubFriend / jquery.repeater

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

custom js function after remove item #150

Open pws2016 opened 3 years ago

pws2016 commented 3 years ago

Hello, I have custom JS function which do cacul of value in repeater item but when delete item the function not return right value because i use node items to do the calcul but after click node remain exist in DOM html!! i try also to use repeaterVal() but not get all inputs for each row!! How i can fix this? my custom function code is: ` $(document).ready(function () { 'use strict';

$('.repeater').repeater({
    defaultValues: {
        'textarea-input': 'foo',
        'text-input': 'bar',
        'select-input': 'B',
        'checkbox-input': ['A', 'B'],
        'radio-input': 'B'
    },
    show: function () {
        $(this).slideDown();
    },
    hide: function (deleteElement) {
        if(confirm('Are you sure you want to delete this element?')) {
            $(this).slideUp(deleteElement);
            calculContoEco();
        }

    },
    ready: function (setIndexes) {

    }
});

function calculContoEco(){ var inv_immo_immateriali_total_year_1=0; $(".inv_immo_immateriali_amount_year_1").each(function( index ) { var v=parseFloat($(this).val()); if(isNaN(v)) v=0; inv_immo_immateriali_total_year_1=inv_immo_immateriali_total_year_1+v; }); $("#inv_immo_immateriali_total_year_1").text(inv_immo_immateriali_total_year_1); } `

sanju-jangra commented 3 years ago

Using setTimeout would resolve your issue I believe. i.e.

hide: function (deleteElement) { if(confirm('Are you sure you want to delete this element?')) { $(this).slideUp(deleteElement); setTimeout(function(){ return calculContoEco(); }, 500); } },

I hope that helps.

mbofos01 commented 1 year ago

In my case adding my function inside the slideUp call worked. Something like this: $(this).slideUp(function(){ deleteElement(); calculContoEco(); );

I hope that this will help anyone that doesn't want to alter the library or doesn't trust the timeout solution, which despite working raises some concerns. ✌🏻

willpower18 commented 1 month ago

Thanks for sharing your solution @mbofos01 , it worked fine