$(document).ready(function () {
$('#add_new_detail_row').click(function () {
var description = $('#description').val();
var amount = parseFloat($('#amount').val());
var total = 0;
var result = validateAddRow(description, amount);
var numItems = Math.round(new Date().getTime() + (Math.random() * 100));
if (result) {
var html = "<tr class='num-items' id='del_" + numItems + "_row'>";
html += "<td class='text-center count' id='count_num_" + numItems + "'></td>";
html += "<td class='text-left'>" + description + "</td>";
html += "<td class='text-right num-amount'>" + amount.toFixed(2) + "</td>";
html += "<td class='text-right' title='Delete'><i class='material-icons delete-inv-row' id='del_" + numItems + "'>delete_forever</i></td>";
html += "<input type='hidden' name='description[]' value='" + description + "'>";
html += "<input type='hidden' name='amount[]' value='" + amount.toFixed(2) + "'>";
html += "</tr>";
$('#description').val("");
$('#amount').val("");
$('#tb-details-body').append(html);
$(".count").each(function (index) {
$('#' + this.id).text(index + 1);
});
$(".num-amount").each(function (index) {
total += parseFloat($(this).text());
});
$('#total').text(total.toFixed(2));
$("#description").focus();
}
});
$('.des-item').keypress(function (event) {
if (event.keyCode === 10 || event.keyCode === 13) {
event.preventDefault();
$('#add_new_detail_row').click();
}
});
$('#tb-details-body').on('.delete-inv-row, click', function (event) {
$("#" + event.target.id + "_row").remove();
var total = 0;
$(".num-amount").each(function (index) {
total += parseFloat($(this).text());
});
$('#total').text(total.toFixed(2));
$(".count").each(function (index) {
$('#' + this.id).text(index + 1);
});
});
});
function validateAddRow(description, amount) {
if (description === '') {
swal({
title: "Error!",
text: "Please enter the description...",
type: 'error',
timer: 2000,
showConfirmButton: false
});
$("#description").focus();
return false;
} else if (amount === '' || isNaN(amount)) {
swal({
title: "Error!",
text: "Please enter the amount...",
type: 'error',
timer: 2000,
showConfirmButton: false
});
$("#amount").focus();
return false;
} else {
return true;
}
}
|