$(document).ready(function(){
$(".css_i_select").on("change",function(){
var i_select=$(this).val();
if(i_select=="เช่า"){
$(this).parents("tr").find(".css_more").show();
}else{
$(this).parents("tr").find(".css_more").hide();
}
});
// Use the .autocomplete() method to compile the list based on input from user
$('#brand').autocomplete({
source: 'autocomplete/autoProducts/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('#codeST').val(ui.item.codeST);
$itemrow.find('#brand').val(ui.item.brand);
$itemrow.find('#type').val(ui.item.type);
$itemrow.find('#unit').val(ui.item.unit);
// Give focus to the next input field to recieve input from user
$('#typeQT').focus();
$('#detail').focus();
$('#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.brand + " - " + item.codeST + "</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/autoProducts/images/icon-minus.png" alt="Remove Item" title="Remove Item"></a></td>',
'<td><select name="typeQT[]" id="typeQT" class="form-control" tabindex="1"><option value="">เลือกประเภท</option><option value="ขาย">ขาย</option><option value="เช่า">เช่า</option><option value="ค่าบริการ">ค่าบริการ</option></select></td>',
'<td><input name="codeST[]" type="hidden" class="form-control" id="codeST"/><input name="brand[]" value="" class="form-control" id="brand" tabindex="2"/></td>',
'<td><input name="type[]" value="" class="form-control" id="type" tabindex="3" /></td>',
'<td><input name="unit[]" value="" class="form-control" id="unit" tabindex="12"/></td>',
'<td><input name="price[]" value="" class="form-control" id="price" tabindex="13" OnKeyPress="return chkNumber(this);"/></td>',
'<td><input name="discount[]" value="" class="form-control" id="discount" tabindex="14" 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 $codeST = $row.find('#codeST');
var $brand = $row.find('#brand');
var $type = $row.find('#type');
var $typeQT = $row.find('#typeQT');
var $unit = $row.find('#unit');
var $price = $row.find('#price');
var $quantity = $row.find('#quantity');
var $discount = $row.find('#discount');
if ( $('#brand:last').val() !== '' ) {
// apply autocomplete widget to newly created row
$row.find('#brand').autocomplete({
source: 'autocomplete/autoProducts/data/item-data.php',
minLength: 1,
select: function(event, ui) {
$codeST.val(ui.item.codeST);
$brand.val(ui.item.brand);
$type.val(ui.item.type);
$transmission.val(ui.item.transmission);
$unit.val(ui.item.unit);
// Give focus to the next input field to recieve input from user
$typeQT.focus();
$detail.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.brand + " - " + item.codeST + "</a>" )
.appendTo( ul );
};
// Add row after the first row in table
$('.item-row:last', $itemsTable).after($row);
$($brand).focus();
} // End if last brand input is empty
return false;
});
$('#brand').focus(function(){
window.onbeforeunload = function(){ return "ทำรายการใบเสนอราคา"; };
});
}); // 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();
});