This commit is contained in:
lihang19980418
2019-09-04 16:38:27 +08:00
parent 8c9d50b393
commit 1d208779dc
6 changed files with 130 additions and 74 deletions
@@ -32,7 +32,7 @@ public class ShoppingCartController {
shoppingCartItems = new HashSet<>();
}
model.addAttribute("productlist", shoppingCartItems);
System.out.println("get "+shoppingCartItems.size()+" cart items");
return "cart.html";
}
@@ -50,7 +50,7 @@ public class ShoppingCartController {
@PostMapping("/delete")
@ResponseBody
public void removeShoppingCart(@RequestParam("shoppingCartID") long cartID, @AuthenticationPrincipal User user) {
shoppingCartService.deleteCartByID(cartID);
shoppingCartService.deleteCartByID(cartID, user);
}
@PostMapping("/update")
@@ -72,4 +72,20 @@ public class ShoppingCart {
public void setUpdate_time(Date update_time) {
this.update_time = update_time;
}
public long getCartID() {
return cartID;
}
public void setCartID(long cartID) {
this.cartID = cartID;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof ShoppingCart){
return this.cartID==((ShoppingCart) obj).cartID;
}
return false;
}
}
@@ -44,7 +44,9 @@ public class ShoppingCartService {
return true;
}
public void deleteCartByID(long cartID) {
public void deleteCartByID(long cartID, User user) {
var cart = shoppingCartRepository.findById(cartID);
user.getShoppingCart().remove(cart);
shoppingCartRepository.deleteById(cartID);
}
+70 -66
View File
@@ -1,122 +1,126 @@
$(function(){
$(function() {
/**************数量加减***************/
$(".num .sub").click(function(){
$(".num .sub").click(function () {
var num = parseInt($(this).siblings("span").text());
if(num<=1){
$(this).attr("disabled","disabled");
}else{
if (num <= 1) {
$(this).attr("disabled", "disabled");
} else {
num--;
$(this).siblings("span").text(num);
//获取除了货币符号以外的数字
var price = $(this).parents(".number").prev().text().substring(1);
//单价和数量相乘并保留两位小数
$(this).parents(".th").find(".sAll").text('¥'+(num*price).toFixed(2));
$(this).parents(".th").find(".sAll").text('¥' + (num * price).toFixed(2));
jisuan();
zg();
}
});
$(".num .add").click(function(){
$(".num .add").click(function () {
var num = parseInt($(this).siblings("span").text());
if(num>=5){
if (num >= 5) {
confirm("限购5件");
}else{
} else {
num++;
$(this).siblings("span").text(num);
var price = $(this).parents(".number").prev().text().substring(1);
$(this).parents(".th").find(".sAll").text('¥'+(num*price).toFixed(2));
$(this).parents(".th").find(".sAll").text('¥' + (num * price).toFixed(2));
jisuan();
zg();
}
});
//计算总价
function jisuan(){
var all=0;
var len =$(".th input[type='checkbox']:checked").length;
if(len==0){
$("#all").text('¥'+parseFloat(0).toFixed(2));
}else{
$(".th input[type='checkbox']:checked").each(function(){
//获取小计里的数值
var sAll = $(this).parents(".pro").siblings('.sAll').text().substring(1);
//累加
all+=parseFloat(sAll);
//赋值
$("#all").text('¥'+all.toFixed(2));
})
function jisuan() {
var all = 0;
var len = $(".th input[type='checkbox']:checked").length;
if (len == 0) {
$("#all").text('¥' + parseFloat(0).toFixed(2));
} else {
$(".th input[type='checkbox']:checked").each(function () {
//获取小计里的数值
var sAll = $(this).parents(".pro").siblings('.sAll').text().substring(1);
//累加
all += parseFloat(sAll);
//赋值
$("#all").text('¥' + all.toFixed(2));
})
}
}
//计算总共几件商品
function zg(){
function zg() {
var zsl = 0;
var index = $(".th input[type='checkbox']:checked").parents(".th").find(".num span");
var len =index.length;
if(len==0){
var len = index.length;
if (len == 0) {
$("#sl").text(0);
}else{
index.each(function(){
zsl+=parseInt($(this).text());
} else {
index.each(function () {
zsl += parseInt($(this).text());
$("#sl").text(zsl);
})
}
if($("#sl").text()>0){
$(".count").css("background","#c10000");
}else{
$(".count").css("background","#8e8e8e");
if ($("#sl").text() > 0) {
$(".count").css("background", "#c10000");
} else {
$(".count").css("background", "#8e8e8e");
}
}
/*****************商品全选***********************/
$("input[type='checkbox']").on('click',function(){
$("input[type='checkbox']").on('click', function () {
var sf = $(this).is(":checked");
var sc= $(this).hasClass("checkAll");
if(sf){
if(sc){
$("input[type='checkbox']").each(function(){
this.checked=true;
});
var sc = $(this).hasClass("checkAll");
if (sf) {
if (sc) {
$("input[type='checkbox']").each(function () {
this.checked = true;
});
zg();
jisuan();
}else{
$(this).checked=true;
var len = $("input[type='checkbox']:checked").length;
var len1 = $("input").length-1;
if(len==len1){
$("input[type='checkbox']").each(function(){
this.checked=true;
});
jisuan();
} else {
$(this).checked = true;
var len = $("input[type='checkbox']:checked").length;
var len1 = $("input").length - 1;
if (len == len1) {
$("input[type='checkbox']").each(function () {
this.checked = true;
});
}
zg();
jisuan();
}
}else{
if(sc){
$("input[type='checkbox']").each(function(){
this.checked=false;
});
} else {
if (sc) {
$("input[type='checkbox']").each(function () {
this.checked = false;
});
zg();
jisuan();
}else{
$(this).checked=false;
} else {
$(this).checked = false;
var len = $(".th input[type='checkbox']:checked").length;
var len1 = $("input").length-1;
if(len<len1){
$('.checkAll').attr("checked",false);
var len1 = $("input").length - 1;
if (len < len1) {
$('.checkAll').attr("checked", false);
}
zg();
jisuan();
}
}
});
/****************************proDetail 加入购物车*******************************/
$(".btns .cart").click(function(){
if($(".categ p").hasClass("on")){
$(".btns .cart").click(function () {
if ($(".categ p").hasClass("on")) {
var num = parseInt($(".num span").text());
var num1 = parseInt($(".goCart span").text());
$(".goCart span").text(num+num1);
$(".goCart span").text(num + num1);
}
});
// }
//删除购物车商品
$('.del').click(function(){
//单个删除
+26 -4
View File
@@ -45,12 +45,12 @@
<div class="number">
<p class="num clearfix">
<img class="fl sub" src="img/temp/sub.jpg">
<span class="fl">1</span>
<span class="fl" th:text="${product.getCount()}"></span>
<img class="fl add" src="img/temp/add.jpg">
</p>
</div>
<div class="price sAll " th:text="${'¥'+product.getProduct().price}"></div>
<div class="price"><a class="del" href="#2">删除</a></div>
<div class="price sAll " th:text="${'¥'+product.getCount()*product.getProduct().price}"></div>
<div class="price"><a th:onclick="deleteCart([[${product.getCartID()}]])" class="del">删除</a></div>
</div>
<div class="goOn">空空如也~<a href="product.html">去逛逛</a></div>
<div class="tr clearfix">
@@ -65,7 +65,7 @@
<p class="fr">
<span>共<small id="sl">0</small>件商品</span>
<span>合计:&nbsp;<small id="all">¥0.00</small></span>
<a href="order.html" class="count">结算</a>
<a class="count">结算</a>
</p>
</div>
</div>
@@ -103,5 +103,27 @@
<script src="js/public.js" type="text/javascript" charset="utf-8"></script>
<script src="js/pro.js" type="text/javascript" charset="utf-8"></script>
<script src="js/cart.js" type="text/javascript" charset="utf-8"></script>
<script>
function deleteCart(cart_id) {
$.ajax({
url:"/cart/delete",
type:"post",
data:{
shoppingCartID:cart_id
}
})
}
function updateCart(cart_id, item_count) {
$.ajax({
url:"/cart/update",
type:"post",
data:{
shoppingCartID:cart_id,
count:item_count
}
})
}
</script>
</body>
</html>
@@ -71,7 +71,8 @@
<div class="add-buy-car-box">
<a href="/cart">
<button class="btn-add-buy-car btn btn-danger">
<button class="btn-add-buy-car btn btn-danger"
th:onclick="addToCart([[${product.ID}]])">
加入购物车
</button>
</a>
@@ -165,5 +166,16 @@
$('.item-detail-img-small img').hover(function () {
$('.item-detail-big-img img').attr('src', $(this).attr('src'));
});
function addToCart(product_id) {
$.ajax({
type:"POST",
data:{
count:1,
productID:product_id
},
url:"/cart/add"
})
}
</script>
</html>