Hi there! I am newbie in JS. I am making making class project and faced with some problems. I have found script (here) and learned. here is my problem:
1. not updating row number and row ID after deleting rows
2. not updating total sum if user change quantity
3. not updating grand total after deleting and changing quantity
Could someone help!
Here is code:
<html>
<head>
<script type="text/javascript">
function updateSum(formID, nameID, quantityID, priceID) {
var f = document.getElementById(formID)
var name = document.getElementById(nameID)
var quantity = document.getElementById(quantityID)
var price = document.getElementById(priceID)
if (f && name && quantity && price) {
var tab = f.getElementsByTagName("table")[0] // expect a table
var row = tab.insertRow(tab.rows.length - 1)
function myRowObject(one, two)
{
this.one = one; // text object
this.two = two; // input text object
}
var qtyVal = parseFloat(quantity.value)
var priceVal = parseFloat(price.value)
if (!qtyVal) { qtyVal = 0 }
if (!priceVal) { priceVal = 0 }
qtyVal = parseInt(Math.round(qtyVal*100),10)/100 // force 2 decimal
priceVal = parseInt(Math.round(priceVal*100),10)/100 // force 2 decimal
var total = qtyVal * priceVal
total = parseInt(Math.round(total*100),10)/100 // force 2 decimal
var noCell = row.insertCell(0)
var txtNo = document.createElement('input');
var id = tab.rows.length - 2
txtNo.type = 'text';
txtNo.style.textAlign = "right"
txtNo.size = 1;
txtNo.disabled = "disabled"
txtNo.value = tab.rows.length - 2
noCell.appendChild(txtNo);
var nameCell = row.insertCell(1)
var txtName = document.createElement('input');
txtName.type = 'text';
txtName.name = "name" + id;
txtName.style.textAlign = "left"
txtName.disabled = "disabled"
txtName.size = 50;
txtName.value = name.value
nameCell.appendChild(txtName);
var quanityCell = row.insertCell(2)
var txtQuanity = document.createElement('input');
txtQuanity.type = 'text';
txtQuanity.name = "quantity" + id;
txtQuanity.id = "quantity" + id;
txtQuanity.style.textAlign = "right"
txtQuanity.size = 10;
txtQuanity.value = qtyVal
quanityCell.appendChild(txtQuanity);
var priceCell = row.insertCell(3)
var txtPrice = document.createElement('input');
txtPrice.type = 'text';
txtPrice.name = "price" + id;
txtPrice.id = "price" + id;
txtPrice.style.textAlign = "right"
txtPrice.size = 10;
txtPrice.value = priceVal
priceCell.appendChild(txtPrice);
var totalCell = row.insertCell(4)
var txtTotal = document.createElement('input');
txtTotal.type = 'text';
txtTotal.name = "total" + id;
txtTotal.id = "total" + id;
txtTotal.style.textAlign = "right"
txtTotal.size = 10;
txtTotal.value = 2;
txtTotal.value = total;
totalCell.appendChild(txtTotal);
var sum = parseFloat(tab.rows[tab.rows.length-1].cells[4].innerHTML)
tab.rows[tab.rows.length-1].cells[4].innerHTML = sum+total
var cell6 = row.insertCell(5);
var btnEl = document.createElement('input');
btnEl.type = 'button';
btnEl.value = 'Delete';
btnEl.onclick = function () {deleteCurrentRow(this)};
cell6.appendChild(btnEl);
}
}
function deleteCurrentRow(rows)
{
var row = rows.parentElement.parentElement;
document.getElementById('atable').deleteRow(row.rowIndex);
var table = document.getElementById('atable')
var rows = table.getElementsByTagName('TR');
for(i=0; i<rows.length; i++)
{
row[i].txtNo[0].id = txtNo[i];
}
}
</script>
</head>
<body>
<form id="sell">
Name: <input type="text" id="name">
<br>
Quantity: <input type="text" id="quantity">
<br>
Price: <input type="text" id="price">
<br>
<input type="button" value="Add" onClick="updateSum('sell', 'name', 'quantity', 'price')">
<br><br>
<table style="width:750;empty-cells:show;border-collapse:collapse" id="atable">
<tr>
<th width="50">No</th>
<th width="400">Name</th>
<th width="100">Quantity</th>
<th width="100">Price</th>
<th width="100">Total</th>
<th></th>
</tr>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td style="text-align:right;font-weight:bold">Grand Total </td>
<td name="allTotal" style="text-align:right;border:1px solid gray">0</td>
<td></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>