Well I am working on the sales and billing part of my small project. Where the left side table shows you the stocks levels and on double clicking any row the form on the left populates as shown in picture.
But the user must be able to specify the quantity. So on inserting required quantity the amt column should be auto calculated(qty*rate).
At first I implemented jQuery's 'keyup' function, but it didn't work. Then I tried implementing angularjs, I failed with it. Then I discovered this jAutoCalc plugin , its also not working.
I tested all these three with simple form, they have been working well. But when I apply the same logic with the form to be generated through jQuery, based on click event, it doesn't work at all.
I have been struggling with this since 6 hours.Please help me out. If there is any proper way of implementing angularjs or jAutoCalc please let me know.
@model IEnumerable<FYPPharmAssistant.Models.InventoryModel.Stock>
....
<script src="~/Scripts/jAutoCalc.js"></script>
@using (Html.BeginForm())
{
<table class="table table-hover" id="maintable">
<thead>
....
</thead>
@foreach (var item in Model)
{
<tr class="rows">
<td>@Html.DisplayFor(modelItem => item.ID)</td>
<td>@Html.DisplayFor(modelItem => item.Item.Name)</td>
<td>@Html.DisplayFor(modelItem => item.Item.DrugGenericName.GenericName)</td>
<td>@Html.DisplayFor(modelItem => item.Qty)</td>
<td>@Html.DisplayFor(modelItem => item.SellingPrice)</td>
</tr>
}
</table>
}
<!-- Rows to be appended using jQuery-->
<table id="tblAppendHere" name="table1" class="table table-condensed">
<thead>
....
</thead>
<tbody>
</tbody>
</table>
Scripts
<script type="text/javascript" >
<!--
function autoCalcSetup() {
$('form[name=table1]').jAutoCalc('destroy');
$('form[name=table1] tr[name=row1]').jAutoCalc({ keyEventsFire: true, decimalPlaces: 2, emptyAsZero: true });
$('form[name=table1]').jAutoCalc({ decimalPlaces: 2 });
}
autoCalcSetup();
//-->
//gets data from table row and populates in the form.
document.getElementById('maintable').ondblclick = function (event) {
event = event || window.event;
var target = event.target || event.srcElement;
while (target && target.nodeName != 'TR') {
target = target.parentElement;
}
var cells = target.cells;
if (!cells.length || target.parentNode.nodeName == 'THEAD') {
return;
}
//appends a table row with for each record dblclick
var $table = $('#tblAppendHere');
$table.append(
'<tr name="row1">' +
'<td><input type="hidden" name="ID1" value= "' + cells[0].innerHTML + '"/>#' + '</td>' +
'<td><input type="hidden" name="Name1" value= "' + cells[1].innerHTML + '"/>' + cells[1].innerHTML + '</td>' +
'<td><input type="text" id="qty1" name="Qty1" style="width:60px;"/>' + '</td>' +
'<td><input type="hidden" id="rate1" name="Rate1" value= "' + cells[4].innerHTML + '"/>' + cells[4].innerHTML + '</td>' +
'<td><input type="text" style="width:90px;" id="amt1" name="Amount1" value="" jAutoCalc="{Qty} * {Rate}" />' + '</td>' +
'<td><a href="#" class="glyphicon glyphicon-remove" onclick="removeItem(this)"></a></td>'
+'</tr>'
);
}
//removes row
function removeItem(obj) {
$obj = $(obj).parent().parent().remove();
};
</script>
At the end, on client side I want something similar to like this:
link for demo :http://c17r.github.io/jAutoCalc/sample.html