DubFriend / jquery.repeater

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

Repeater Item Delete Problem #100

Open gunaysirbudak opened 5 years ago

gunaysirbudak commented 5 years ago

Hi,

I'm making the calculation in the example I provide at the link.

http://demo.fy.tc/repeater

Every new line does the calculation. However, when I delete a row, it does not perform the calculation again.

` $(document).ready(function () {

    $('#ProposalProducts').repeater({
        isFirstItemUndeletable: true, 
        initEmpty: true,
        show: function () {
            var selfRepeaterItem = this;
            $(selfRepeaterItem).slideDown();            
        },
        hide: function (deleteElement) {
            var selfRepeaterItem = this;
            $(selfRepeaterItem).slideUp(deleteElement);

          CalcProducts();
        }        
    });

$('#product_repeat').on('click', function(){

    $('#quantity, #unit_price, #vat, #discount_type, #discount').on("change", function () { 
        CalcProducts();
    });

});

});

function CalcProducts() {

    var repeaterItems_count = $("div[data-repeater-item] > div.product_item");
    var repeaterItems = repeaterItems_count.length;

    var products_sub_total = 0;
    var discount = $("#discount").val();
    var discount_type = $("#discount_type").val();
    var products_sub_total_discount = 0;
    var products_total_vat = 0;
    var products_total = 0;

    for (var i = 0; i < repeaterItems; i++) {

        if(typeof $('input[name="products['+i+'][quantity]"]').val() !== 'undefined') {

            var quantity = $('input[name="products['+i+'][quantity]"]').val();
            var unit_price = $('input[name="products['+i+'][unit_price]"]').val();
            var vat = $('input[name="products['+i+'][vat]"]').val();

            var vat_total = (quantity * unit_price) * vat / 100;
            var sub_total = quantity * unit_price;
            var total =  vat_total + sub_total;

            $('input[name="products['+i+'][vat_total]"]').val(vat_total);
            $('input[name="products['+i+'][sub_total]"]').val(sub_total);
            $('input[name="products['+i+'][total]"]').val(total.toFixed(2));

            products_sub_total = products_sub_total + sub_total;
            products_total_vat = products_total_vat + vat_total;
            products_total = products_total + total;                
        }

    }

    if(discount != '' && discount_type != '') {
        if(discount_type == 'rate')
        {
            var products_total_discount_vat = 0;
            var rate_to_discount = products_sub_total * discount / 100;
            var discount_dispersion = rate_to_discount / repeaterItems;

            for (var d = 0; d < repeaterItems; d++) {

                if(typeof $('input[name="products['+d+'][quantity]"]').val() !== 'undefined') {

                    var quantity = $('input[name="products['+d+'][quantity]"]').val();
                    var unit_price = $('input[name="products['+d+'][unit_price]"]').val();
                    var vat = $('input[name="products['+d+'][vat]"]').val();

                    products_total_discount_vat = products_total_discount_vat + (((quantity * unit_price) - discount_dispersion) * vat / 100);
                }

            }

            products_total = (products_sub_total - rate_to_discount) + products_total_discount_vat;

            document.getElementById('products_sub_total').innerHTML = products_sub_total.toFixed(2);
            document.getElementById('products_sub_total_discount').innerHTML = '(-) '+rate_to_discount.toFixed(2);
            document.getElementById('products_total_vat').innerHTML = products_total_discount_vat.toFixed(2);
            document.getElementById('products_total').innerHTML = products_total.toFixed(2);

        }
        else if(discount_type == 'amount')
        {
            var products_total_discount_vat = 0;
            var discount_dispersion = discount / repeaterItems;

            for (var d = 0; d < repeaterItems; d++) {
                if(typeof $('input[name="products['+d+'][quantity]"]').val() !== 'undefined') {

                    var quantity = $('input[name="products['+d+'][quantity]"]').val();
                    var unit_price = $('input[name="products['+d+'][unit_price]"]').val();
                    var vat = $('input[name="products['+d+'][vat]"]').val();

                    products_total_discount_vat = products_total_discount_vat + (((quantity * unit_price) - discount_dispersion) * vat / 100);
                }

            }

            products_total = (products_sub_total - discount) + products_total_discount_vat;

            document.getElementById('products_sub_total').innerHTML = products_sub_total.toFixed(2);
            document.getElementById('products_sub_total_discount').innerHTML = '(-) '+Number(discount).toFixed(2);
            document.getElementById('products_total_vat').innerHTML = products_total_discount_vat.toFixed(2);
            document.getElementById('products_total').innerHTML = products_total.toFixed(2);                  
        }
    }
    else 
    {

        document.getElementById('products_sub_total_discount').innerHTML = '';
        document.getElementById('products_sub_total').innerHTML = products_sub_total.toFixed(2);
        document.getElementById('products_total_vat').innerHTML = products_total_vat.toFixed(2);
        document.getElementById('products_total').innerHTML = products_total.toFixed(2);            
    }

}    `

Please do help with this.

foxmedo commented 5 years ago

i have try your example and it's working well for me

gunaysirbudak commented 5 years ago

The javascript file I use is not compatible. I used the repeater javascript file and the problem was solved.

virus02 commented 4 years ago

Can we use other effects for delete like instead of slideUp() can we use other effects?

gunaysirbudak commented 4 years ago

Hi,

I solved my problems. What's your problem?

17 Ara 2019 Sal 10:02 tarihinde Sharath Nayak notifications@github.com şunu yazdı:

Can we use other effects for delete like instead of slideUp() can we use other effects?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/DubFriend/jquery.repeater/issues/100?email_source=notifications&email_token=AKVSZ5GIPJESGXVSNQDUIJ3QZB2OTA5CNFSM4GPS24U2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEHBLXIA#issuecomment-566410144, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKVSZ5G4WDZM4354FJUD2H3QZB2OTANCNFSM4GPS24UQ .

paulayo93 commented 3 years ago

@gunaysirbudak Please where able to fix the issue in the code? If you did, please how did you resolve it?

Thanks a lot

gunaysirbudak commented 3 years ago

@paulayo93 Check the javascript file you are using. Use the application's own javascript file.

paulayo93 commented 3 years ago

@paulayo93 Check the javascript file you are using. Use the application's own javascript file.

@gunaysirbudak Do you mean that I should use it from a .JS file instead of writing the code in a script tag? Will that be the case please?