manish thakur
manish thakur

Reputation: 820

facing issue while doing calculation inside HTML table

I have a dynamic HTML tbody which I am creating on change event of a select option, so in my table, I have 3 input fields

What my issue is

Please check out fiddle I am doing like below

$(document).keypress(function (event) {

    const row = event.target.parentElement.parentElement

    var keycode = event.keyCode || event.which;
    if (keycode == '13') {

        if (event.target.matches('[name=discPercentagetd]')) {
            calcDiscount(event.target.parentElement.parentElement)
            if ($(row).parent().find('tr').length - $(row).index() === 1) {
                rowappend(event.target.parentElement.parentElement.parentElement)
            }
        }
    }
});
document.addEventListener("keydown", function (event) {

    const row = event.target.parentElement.parentElement
    if (event.target.matches('[name=discPercentagetd]')) {

        var keycode = event.keyCode || event.which;
        if (keycode == '9') {


            calcDiscount(event.target.parentElement.parentElement)
        }
    }
});

Please check out my fiddle I have commented all the lines there

Note-: after entering DIsc% please go back to Disc% after focusing out using rightShift+tab and see the unitQty in footer it auto increases

Upvotes: 0

Views: 90

Answers (1)

Shridhar Sharma
Shridhar Sharma

Reputation: 2387

You need to set the all the values to 0 and then calculate the values by iterating through all the rows,

like this,

      total = 0;
      totalDiscountAmt = 0;
      totalGstAmt = 0;
      totalUnitQty = 0;
      subtotal = 0;
      $("#tableInvoice tbody tr").each(function() {
        if (!$(event.target).val()) {
          e.preventDefault();
          return;
        }
        calc(this)
        calcDiscount(this)
      })

see the updated fiddle here https://jsfiddle.net/pLcexr6t/

Upvotes: 1

Related Questions