✅P247_商城业务-购物车-删除购物项

gong_yz小于 1 分钟谷粒商城

实现效果


修改cartList.html

为图中的删除按钮设置class,绑定单击事件临时保存skuId

cfmall-cart/src/main/resources/templates/cartList.html

<li>
    <p class="deleteItemBtn" th:attr="skuId=${item.skuId}">删除</p>
</li>
let deleteId = 0;
$(".deleteItemBtn").click(function () {
    deleteId = $(this).attr("skuId");
});

删除事件

//删除购物车选项
function deleteItem() {
    location.href = "http://cart.cfmall.com/deleteItem?skuId=" + deleteId;
}

Controller

cfmall-cart/src/main/java/com/gyz/cfmall/controller/CartController.java

    /**
     * 删除商品信息
     *
     * @param skuId
     * @return
     */
    @GetMapping(value = "/deleteItem")
    public String deleteItem(@RequestParam("skuId") Integer skuId) {

        cartService.deleteIdCartInfo(skuId);
        return "redirect:http://cart.cfmall.com/cart.html";

    }

Service

cfmall-cart/src/main/java/com/gyz/cfmall/service/impl/CartServiceImpl.java

    @Override
    public void deleteIdCartInfo(Integer skuId) {
        BoundHashOperations<String, Object, Object> cartOps = getCartOpts();
        cartOps.delete(skuId.toString());
    }