简体   繁体   中英

How to get the Grand Total from row total

I am new in javascript. I have a table with Product Price, Quantity, Width and Height. I have a single row in the invoice and there is a button for adding new row and delete row. I got the total of each row separately. Now I want to get the Grand Total from Totals of all products. After adding the product grand total, I want to store this invoice in Database. Kindly give me a better solution. Thanks in advance. Regards

<!DOCTYPE html>
<html>
<head>
    <title></title>
<style>
    table,tr,td,th { border: 1px black solid;}

</style>
</head>

<body>
<table>
  <thead>
    <th>Price</th>
    <th>Quantity</th>
    <th>Width</th>
    <th>Height</th>
    <th>Total</th>
    <th>Action</th>
  </thead>

  <tbody id="product_table">
    <tr>
        <td><input type="text" name="price"></td>
        <td><input type="text" name="quantity"></td>
        <td><input type="text" name="width"></td>
        <td><input type="text" name="height"></td>
        <td><input type="text" name="total" readonly></td>
        <td><input type="button" value="X" onclick="deleteRow(this)"/></td>
    </tr>

  </tbody>
    <input type="button" name="submit" value="Add Row" onclick="add_fields();">

</table>
<span>Grand Total<input type="text" name="grandtotal" readonly></span>
</body>

<script>

const table = document.getElementById('product_table');
table.addEventListener('input', ({ target }) => {
  const tr = target.closest('tr');
  const [price, quantity, width, height, total] = tr.querySelectorAll('input');

  var size = width.value * height.value;
  var rate = price.value * quantity.value;

  if (size != "") {
    total.value = size * rate;
  }else{
    total.value = rate; 
  }
});

function add_fields() {
  var row = document.createElement("tr");
  row.innerHTML =
    '<td><input type="text" name="price"></td>' +
    '<td><input type="text" name="quantity"></td>' +
    '<td><input type="text" name="width"></td>' +
    '<td><input type="text" name="height"></td>' +
    '<td><input type="text" name="total" readonly></td>' +
    '<td><input type="button" value="X" onclick="deleteRow(this)"/></td>';

  table.appendChild(row);
}

function deleteRow(btn) {
  var row = btn.parentNode.parentNode;
  row.parentNode.removeChild(row);
}

</script>
</html>

Here is your working code.

<!DOCTYPE html>
<html>
<head>
    <title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js</script>
<style>
    table,tr,td,th { border: 1px black solid;}

</style>
</head>

<body>
<table>
  <thead>
    <th>Price</th>
    <th>Quantity</th>
    <th>Width</th>
    <th>Height</th>
    <th>Total</th>
    <th>Action</th>
  </thead>

  <tbody id="product_table">
    <tr>
        <td><input type="text" name="price"></td>
        <td><input type="text" name="quantity"></td>
        <td><input type="text" name="width"></td>
        <td><input type="text" name="height"></td>
        <td><input type="text" name="total" class="totalPrice" readonly></td>
        <td><input type="button" value="X" onclick="deleteRow(this)"/></td>
    </tr>

  </tbody>
    <input type="button" name="submit" value="Add Row" onclick="add_fields();">

</table>
<span>Grand Total<input type="text" name="grandtotal" id="grandtotal" readonly></span>
</body>

<script>

const table = document.getElementById('product_table');
table.addEventListener('input', ({ target }) => {
  const tr = target.closest('tr');
  const [price, quantity, width, height, total] = tr.querySelectorAll('input');

  var size = width.value * height.value;
  var rate = price.value * quantity.value;

  if (size != "") {
    total.value = size * rate;
  }else{
    total.value = rate; 
  }
  totalPrice();
});

function add_fields() {
  var row = document.createElement("tr");
  row.innerHTML =
    '<td><input type="text" name="price"></td>' +
    '<td><input type="text" name="quantity"></td>' +
    '<td><input type="text" name="width"></td>' +
    '<td><input type="text" name="height"></td>' +
    '<td><input type="text" name="total"  class="totalPrice" readonly></td>' +
    '<td><input type="button" value="X" onclick="deleteRow(this)"/></td>';

  table.appendChild(row);
}

function deleteRow(btn) {
  var row = btn.parentNode.parentNode;
  row.parentNode.removeChild(row);
  totalPrice();
}
function totalPrice(){
var sum = 0;
 $(".totalPrice").each(function(){
 sum += parseFloat($(this).val());
});
$("#grandtotal").val(sum);
}

</script>
</html>

Hope it will work for you.

Check this demo: https://jsfiddle.net/8jo7u4ya/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM