Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
fix NaN when quantity 0
Browse files Browse the repository at this point in the history
  • Loading branch information
thson58 committed Mar 10, 2024
1 parent 47e3dd8 commit ce85aeb
Showing 1 changed file with 31 additions and 73 deletions.
104 changes: 31 additions & 73 deletions src/main/webapp/view/jsp/management/order/add-product-into-order.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
<link rel="icon" type="image/png" sizes="512x512" href="<%=request.getContextPath()%>/view/assets/images/favicon/favicon.png">

<link rel="stylesheet" href="<%=request.getContextPath()%>/view/assets/css/bootstrap.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i&amp;display=swap">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<link rel="stylesheet" href="https://f...content-available-to-author-only...s.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i&amp;display=swap">
<link rel="stylesheet" href="https://c...content-available-to-author-only...e.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<link rel="stylesheet" href="<%=request.getContextPath()%>/view/assets/fonts/fontawesome-all.min.css">
<link rel="stylesheet" href="<%=request.getContextPath()%>/view/assets/fonts/line-awesome.min.css">
<link rel="stylesheet" href="<%=request.getContextPath()%>/view/assets/css/animate.min.css">
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://c...content-available-to-author-only...y.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="<%=request.getContextPath()%>/view/assets/css/validate/validator.css">

</head>

<body id="page-top" class="d-flex flex-column min-vh-100">
<body id="page-top">
<div id="wrapper">
<%@include file="../../../common/_sidenav.jsp" %>
<div class="d-flex flex-column" id="content-wrapper">
Expand All @@ -46,19 +46,9 @@
Chưa nhập tên sản phẩm. Vui lòng kiểm tra lại.
</div>
</c:when>
<c:when test="${requestScope.message eq 'notInputProductQuantity'}">
<div class="alert alert-danger text-center" role="alert">
Chưa nhập số lượng sản phẩm. Vui lòng kiểm tra lại.
</div>
</c:when>
<c:when test="${requestScope.message eq 'productQuantityNotExist'}">
<div class="alert alert-danger text-center" role="alert">
Nhập sai định dạng số lượng sản phẩm. Vui lòng kiểm tra lại.
</div>
</c:when>
<c:when test="${requestScope.message eq 'productQuantityNotPositive'}">
<c:when test="${requestScope.message eq 'productNotExist'}">
<div class="alert alert-danger text-center" role="alert">
Số lượng sản phẩm phải lớn hơn 0. Vui lòng kiểm tra lại.
Chưa nhập số lượng sản phẩm cần mua. Vui lòng kiểm tra lại.
</div>
</c:when>
<c:when test="${requestScope.message eq 'missingCustomer'}">
Expand Down Expand Up @@ -119,75 +109,42 @@
</div>
</div>
</div>
<%@include file="../../../common/_footer.jsp" %>
</div>
<%@include file="../../../common/_footer.jsp" %>
<%@include file="../../../common/_goback.jsp" %>
</div>
<%@include file="../../../common/_goback.jsp" %>
</div>
<script src="<%=request.getContextPath()%>/view/assets/js/bootstrap.min.js"></script>
<script src="<%=request.getContextPath()%>/view/assets/js/bs-init.js"></script>
<script src="<%=request.getContextPath()%>/view/assets/js/theme.js"></script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<script src="<%=request.getContextPath()%>/view/assets/js/validate/validator.js"></script>
<script src="<%=request.getContextPath()%>/view/assets/js/alert-timeout.js"></script>
<script src="<%=request.getContextPath()%>/view/assets/js/bootstrap.min.js"></script>
<script src="<%=request.getContextPath()%>/view/assets/js/bs-init.js"></script>
<script src="<%=request.getContextPath()%>/view/assets/js/theme.js"></script>
<script src="https://c...content-available-to-author-only...y.com/jquery-3.5.1.min.js"></script>
<script src="https://c...content-available-to-author-only...y.com/ui/1.12.1/jquery-ui.min.js"></script>
<script src="<%=request.getContextPath()%>/view/assets/js/validate/validator.js"></script>

<!--Danh sach goi y-->
<script>
$(document).ready(function () {
var productsArray = [
var availableProducts = [
<c:forEach var="product" items="${requestScope.products}">
{ name: "${product.name}", ID: "${product.ID}", price: "${product.price}" },
"${product.name}",
</c:forEach>
];
$("#product-name").autocomplete({
source: function(request, response) {
var term = request.term.toLowerCase();
var filteredProducts = productsArray.filter(function(product) {
return product.name.toLowerCase().includes(term);
});
response(filteredProducts.map(function(product) {
return product.name;
}));
},
select: function (event, ui) {
var selectedProductName = ui.item.value;
var selectedProduct = findProductByName(selectedProductName);
if (selectedProduct) {
$('#product-id').val(selectedProduct.ID);
$('#product-price').val(formatCurrency(selectedProduct.price));
$('#product-quantity').val(1);
calculateTotal();
}
},
change: function (event, ui) {
var enteredProductName = $(this).val();
var enteredProduct = findProductByName(enteredProductName);
if (enteredProduct) {
$('#product-id').val(enteredProduct.ID);
$('#product-price').val(formatCurrency(enteredProduct.price));
$('#product-quantity').val(1);
calculateTotal();
} else {
$('#product-id').val('');
$('#product-price').val('');
$('#product-quantity').val('');
$('#product-total-price').val('');
}
source: availableProducts,
select: function( event, ui ) {
var selectedProduct = ui.item.value;
<c:forEach var="product" items="${requestScope.products}">
if("${product.name}" === selectedProduct) {
$('#product-id').val("${product.ID}");
$('#product-price').val(formatCurrency("${product.price}"));
$('#product-quantity').val(1);
}
</c:forEach>
calculateTotal();
}
});
function findProductByName(productName) {
return productsArray.find(function(product) {
return product.name === productName;
});
}
});
</script>


<!--Validate input-->
<script>
//Validate form add product into order
Expand All @@ -197,9 +154,7 @@
formGroupSelector: '.form-group',
rules: [
Validator.isRequired('#product-name', 'Vui lòng nhập tên sản phẩm!'),
Validator.isRequired('#product-quantity', 'Vui lòng nhập số lượng sản phẩm!'),
Validator.isPositive('#product-quantity', 'Vui lòng nhập số lượng sản phẩm lớn hơn 0!')
Validator.isRequired('#product-quantity', 'Vui lòng nhập số lượng sản phẩm!')
]
});
</script>
Expand All @@ -225,6 +180,9 @@
var quantity = parseInt($('#product-quantity').val());
var price = parseFloat($('#product-price').val().replace(/[^0-9.-]+/g, '')) * 1000;
var total = quantity * price;
if(isNaN(total)){
total = 0;
}
$('#product-total-price').val(formatCurrency(total));
}
Expand Down

0 comments on commit ce85aeb

Please sign in to comment.