Hi all,
Please help me with my jquery Project about sales invoice. Since im a newbie in jquery im having a hard time on it. My problem is im stuck on updating Total price on keyup of item quantity id. My first row field is succesful but when i try to add another row for item quanity, price,name etc., on keyup of itemQty of the next row my total price wont update and so on when newly row added. sorry for my bad english. hope you can help me with my project and will enlighten my skills.
This is my Java Script:
$(document).ready(function(){
$("#itemQty").keyup(function(){
sumValues();
}).keyup();
function sumValues() {
total = ($('#itemQty').val() * $('#itemPrice').val());
$("#priceTotal").text(total.toFixed(2));
}
//------------------------------------Start Sa Customer AutoComplete----------------------------------------------------------
$('#customer').autocomplete({
source: 'data/customer.php',
minLength: 1,
select: function(event, ui) {
var $itemrow = $(this).closest('#itemsTables');
// Populate the input fields from the returned values
$itemrow.find('#customer').val(ui.item.companyname);
$itemrow.find('#companyname').val(ui.item.companyname);
$itemrow.find('#branch').val(ui.item.branch);
$itemrow.find('#contactperson').val(ui.item.contactperson);
$itemrow.find('#contactnumber').val(ui.item.contactnumber);
$itemrow.find('#address').val(ui.item.address);
return false;
}
// List of menu of autocomplete sa customer
}).data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.companyname + " - " + item.branch + "</a>" )
.appendTo( ul );
};
//------------------------------------End Sa Customer AutoComplete----------------------------------------------------------
//-----------------------------------Item Add Field----------------------------------------------
// Use the .autocomplete() method to compile the list based on input from user
$('#itemCode').autocomplete({
source: 'data/item-data.php',
minLength: 1,
select: function(event, ui) {
var $itemrow = $(this).closest('tr');
// Populate the input fields from the returned values
$itemrow.find('#itemCode').val(ui.item.itemCode);
$itemrow.find('#itemDesc').val(ui.item.itemDesc);
$itemrow.find('#itemPrice').val(ui.item.itemPrice);
$itemrow.find('#itemDim').val(ui.item.itemDim);
// focus to the next input field for item quanity
$('#itemQty').focus();
return false;
}
// List of menu of autocomplete sa item
}).data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.itemCode + " - " + item.itemDesc + "</a>" )
.appendTo( ul );
};
// Get the table object to use for adding a row at the end of the table
var $itemsTable = $('#itemsTable');
// Create an Array to for the table row. ** Just to make things a bit easier to read.
var rowTemp = [
'<tr class="item-row">',
'<td><a id="deleteRow"><img src="images/icon-minus.png" alt="Remove Item" title="Remove Item"></a></td>',
'<td><input name="itemCode[]" class="tInput" value="" id="itemCode" /> </td>',
'<td><input name="itemDesc[]" class="tInput" value="" id="itemDesc" readonly="readonly" /></td>',
'<td><input name="itemDim[]" value="" class="tInput" id="itemDim"/></td>',
'<td><input name="itemQty[]" class="tInput" value="" id="itemQty" /></td>',
'<td><input name="itemPrice[]" class="tInput" value="" id="itemPrice" /></td>',
'<td><div id="priceTotal"></total> </td>',
'</tr>'
].join('');
// Add row to list and allow user to use autocomplete to find items.
$("#addRow").bind('click',function(){
var $row = $(rowTemp);
// save reference to inputs within row
var $itemCode = $row.find('#itemCode');
var $itemDesc = $row.find('#itemDesc');
var $itemPrice = $row.find('#itemPrice');
var $itemQty = $row.find('#itemQty');
var $itemDim = $row.find('#itemDim');
if ( $('#itemCode:last').val() !== '' ) {
// apply autocomplete widget to newly created row
$row.find('#itemCode').autocomplete({
source: 'data/item-data.php',
minLength: 1,
select: function(event, ui) {
$itemCode.val(ui.item.itemCode);
$itemDesc.val(ui.item.itemDesc);
$itemPrice.val(ui.item.itemPrice);
$itemDim.val(ui.item.itemDim);
// Give focus to the next input field to recieve input from user
$itemQty.focus();
return false;
}
}).data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.itemCode + " - " + item.itemDesc + "</a>" )
.appendTo( ul );
};
// Add row after the first row in table
$('.item-row:last', $itemsTable).after($row);
$($itemCode).focus();
} // End if last itemCode input is empty
return false;
});
$('#itemCode').focus(function(){
window.onbeforeunload = function(){ return "You haven't saved your data. Are you sure you want to leave this page without saving first?"; };
});
});
// Remove row when clicked
$("#deleteRow").live('click',function(){
$(this).parents('.item-row').remove();
// Hide delete Icon if we only have one row in the list.
if ($(".item-row").length < 2) $("#deleteRow").hide();
});
and this is my Main html:
<tr class="item-row">
<td></td>
<td><input name="itemCode[]" value="" class="tInput" id="itemCode" tabindex="1"/> </td>
<td><input name="itemDesc[]" value="" class="tInput" id="itemDesc" readonly="readonly" /></td>
<td><input name="itemDim[]" value="" class="tInput" id="itemDim"/></td>
<td><input name="itemQty[]" value="" class="tInput" id="itemQty" tabindex="2"/></td>
<td><input name="itemPrice[]" value="" class="tInput" id="itemPrice" readonly="readonly" /> </td>
<td><div id="priceTotal"></total> </td>
</tr>