$(document).ready(function(){
// Use the .autocomplete() method to compile the list based on input from user
$('#Scode').autocomplete({
source: '../autocomplete/data/item-data3.php',
minLength: 1,
select: function(event, ui) {
var $itemrow = $(this).closest('tr');
// Populate the input fields from the returned values
$itemrow.find('#Scode').val(ui.item.Scode);
$itemrow.find('#Sname').val(ui.item.Sname);
$itemrow.find('#TypeScode').val(ui.item.TypeScode)
// Give focus to the next input field to recieve input from user
$('#Price').focus();
$('#Quantity').focus();
$('#Discount').focus();
return false;
}
// Format the list menu output of the autocomplete
}).data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.Scode + " - " + item.Sname + "</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="../autocomplete/images/icon-minus.png" alt="Remove Item" title="Remove Item"></a></td>',
'<td><input name="h_item_id[]" type="hidden" id="h_item_id[]" value="" /><input name="Scode[]" class="tInput" value="" id="Scode" /> </td>',
'<td><input name="Sname[]" class="tInput" value="" id="Sname" readonly="readonly" /></td>',
'<td><select name="typeScode[]" class="tInput" id="typeScode"><option value=""> -- ประเภท --</option><option value="ล้อเป็น">ล้อเป็น</option><option value="ล้อตาย">ล้อตาย</option><option value="หูลาก">หูลาก</option><option value="ล้อเปล่า">ล้อเปล่า</option></select></td>',
'<td><input name="Price[]" value="" class="tInput" id="Price" OnKeyPress="return chkNumber(this);" /></td>',
'<td><input name="Quantity[]" value="" class="tInput" id="Quantity" OnKeyPress="return chkNumber(this);" /> </td>',
'<td><input name="Discount[]" value="" class="tInput" id="Discount" OnKeyPress="return chkNumber(this);"/></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 $Scode = $row.find('#Scode');
var $Sname = $row.find('#Sname');
var $TypeScode = $row.find('#TypeScode');
var $Price = $row.find('#Price');
var $Quantity = $row.find('#Quantity');
var $Discount = $row.find('#Discount');
if ( $('#Scode:last').val() !== '' ) {
// apply autocomplete widget to newly created row
$row.find('#Scode').autocomplete({
source: '../autocomplete/data/item-data3.php',
minLength: 1,
select: function(event, ui) {
$Scode.val(ui.item.Scode);
$Sname.val(ui.item.Sname);
// Give focus to the next input field to recieve input from user
$TypeScode.focus();
$Price.focus();
$Quantity.focus();
$Discount.focus();
return false;
}
}).data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.Scode + " - " + item.Sname + "</a>" )
.appendTo( ul );
};
// Add row after the first row in table
$('.item-row:last', $itemsTable).after($row);
$($Scode).focus();
} // End if last Scode input is empty
return false;
});
$('#Scode').focus(function(){
window.onbeforeunload = function(){ return "ทำรายการ PO"; };
});
}); // End DOM
// 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();
});
ลองบันทึกลง DB โดยการเพิ่มสินค้า2รายการ ทั้ง2ครั้ง มีแค่รายการแรกที่ประเภทสินค้าขึ้น