I have the following table with editable fields (columns).
Any change I make to the sellprice or casecost column fields, I would like the GP column field to update as the user types or changes values. My code is as follows.
function calculate() {
$('tr #item_q_sellprice').change(function calculate() {
//$('.productTable tr').on('change','#item_q_sellprice,#item_q_casecost',function calculate() {
// $("input[type=text]").change(function () {
//fields used for calculations
var newPrice = parseFloat($('tr #item_q_sellprice').val());
var casecost = parseFloat($('tr #item_q_casecost').val());
var casesize = parseFloat($('tr #item_q_casesize').val());
var vatrate = parseFloat($('tr #item_productVat').val());
//formulae
var netprice = newPrice / (1 + vatrate / 100);
var unitcost = casecost / casesize;
var profit = (newPrice / (vatrate / 100 + 1)) - unitcost;
var grossprofit = (profit / netprice) * 100
//alert("change price: " + price);
//Update GP field
$('#item_grossProfit').val(grossprofit.toFixed(1));
});
}
My Html for the rows is as follows
@foreach (var item in Model)
{
<tr>
<td onclick="location.href = '@(Url.Action("Index", "Product", new { id = item.q_guid }))'">
@Html.DisplayFor(modelItem => item.q_description)
</td>
<td onclick="location.href = '@(Url.Action("Index", "Product", new { id = item.q_guid }))'">
@Html.DisplayFor(modelItem => item.q_barcode)
</td>
<td>
€ @Html.TextBoxFor(modelItem => item.q_sellprice, "{0:0.00}", new { @class = "calc-list" })
</td>
<td>
€ @Html.TextBoxFor(modelItem => item.q_casecost, "{0:0.00}", new { @class = "calc-list" })
</td>
<td>
@Html.TextBoxFor(modelItem => item.grossProfit, new { @class = "calc-list" })
</td>
<td onclick="location.href = '@(Url.Action("Index", "Product", new { id = item.q_guid }))'">
@Html.DisplayFor(modelItem => item.productDepartment)
</td>
<td onclick="location.href = '@(Url.Action("Index", "Product", new { id = item.q_guid }))'">
@Html.TextBoxFor(modelItem => item.productVat, "{0:0.0}",new { @class = "calc-list", @readonly = "readonly" })
</td>
<td onclick="location.href = '@(Url.Action("Index", "Product", new { id = item.q_guid }))'">
@Html.DisplayFor(modelItem => item.q_stocklevel)
</td>
<td onclick="location.href = '@(Url.Action("Index", "Product", new { id = item.q_guid }))'">
@Html.TextBoxFor(modelItem => item.q_casesize, new { @class = "calc-list", @readonly = "readonly" })
</td>
</tr>
}
Now this currently only works for the top row, and not the rest of the table rows. I guess the error will be on my variable lines
var newPrice = parseFloat($('tr #item_q_sellprice').val());
How do I make it so any row where these fields (sellprice/casecost) are changed/updated also updates the corresponding GP column?