Spring Boot快速搭建一个简易商城项目【四,优化购物车篇】

在之前的基础上继续将购物车进行完善:全选,删除,加减购物车数量

效果:

全选:

计算价格:

复制代码
	//计算总价
	function jisuan(){
		let total =0;
		$(".th").each((i,el)=>{
		//each遍历  i下标  el指的是当前的对象
		let price=	$(el).find('.myprice').text().replace("¥","")*1
		let num=	$(el).find('.mynum').text()*1
		$(el).find('.sAll').text("¥"+price*num)
		// 	总价选中显示
		let f=$(el).find('input[type=checkbox]').prop('checked')
		if (f) total+=price*num
		})
		$("#all").text("¥"+total)
	}

删除商品:

复制代码
	//删除购物车商品
	$('.del').click(function(){
		// 有id代表删除全部
		let gid = $(this).attr('data-gid');
		let ids=[]
		if (gid){
			ids.push(gid)
		}else {
			$(".th").each((i,el)=>{
				let f=$(el).find('input[type=checkbox]').prop('checked')
				if (f) {
					let id = $(el).find('.mynum').attr('data-gid')
					ids.push(id)
				}
			})
		}
		if (ids.length>0){
			$.post('/cart/del',{ids},resp=>{
				if (resp.code===200){
					alert("删除成功")
					// 刷新页面
					location.reload();
				}
			},"json")
		}
	})

后台:

复制代码
    @RequestMapping("/del")
    @ResponseBody
    public JsonResponseBody<?> del(User user,@RequestParam("ids[]")List<String> ids){
        redisService.delCart(user,ids);
        //将购物车的商品放到缓存数据库中
        return JsonResponseBody.success();
    }

删除这里有选中删除,和单个删除,传递的都有gid

相关推荐
lhldsg11 小时前
多商户团购系统源码:技术架构选型与二次开发实战指南
java·小程序·架构
亦暖筑序11 小时前
AgentScope Java 实战:@Tool 方法里到底该不该写业务逻辑?
java·agent·ai编程
stars36911 小时前
实验四 JSP内置对象的应用
后端
脉动数据行情111 小时前
Java SpringBoot 国际期货批量采集实践 美原油 / 黄金 / 指数期货定时落库
java·开发语言·spring boot
对象存储与RustFS11 小时前
用 Restic 把本地备份存进 RustFS:S3 兼容仓库实战
后端·rust·开源
Zane199411 小时前
写Stream时踩过的坑:中间操作不会真正执行,直到你调用这一个方法
java·后端
明月_清风11 小时前
Foundry Invariant Testing 实战:ERC-4626 + Handler + Ghost Variable
后端·web3·solidity
明月_清风11 小时前
Foundry Invariant Testing:让测试自动寻找复杂状态下的 Solidity Bug
后端·web3·solidity
EXI-小洲11 小时前
Spring AI (第二章)大模型对话上下文记忆
java·人工智能·spring
扬大平仔11 小时前
# 小深:用 AgentScope Java 2.0 Harness 做私人助手(上)mysql
java·开发语言·mysql