springboot AJAX 一二三级列表联动

springboot AJAX 联动
1.创建onchange事件
在这里插入图片描述
在这里插入图片描述
以上两步后,f12 右侧console可以出现对应的数字
在这里插入图片描述
2.使页面数据返回controller控制层
在这里插入图片描述
以上步骤以后,可以在控制台看到页面的返回值
在这里插入图片描述
3.controller控制层代码完整如下

@RequestMapping("/GoodsInfo/ajaxQueryByPid")
    @ResponseBody
    public List<CategoryTwo> ajaxQueryByPid(Integer pid){
        System.out.println("pid = " + pid);
        List<CategoryTwo> list = categoryTwoService.queryByPid(pid);
        System.out.println("list = " + list);
        return list ;
    }

二级分类模型层
在这里插入图片描述

二级分类xml文件下,sql代码如下

<select id="selectListByPid" resultMap="resultMap">
        select ctwo_id, cone_id, ctwo_name, ctwo_num, ctwo_code, ctwo_info
        from category_two
        where cone_id = #{pid}
    </select>

以上代码后控制层可以根据页面的返回值查询对应的数据库信息并返回控制层
在这里插入图片描述
4.add_goodsinfo.html 页面层完整代码如下

function ajaxCtwo() {
			//$("[name = cthrId]").empty(); //aa.三级分类联动后接剪切至这里
			var proid = $("#coneId").val();
			console.log(proid);
			var url = "/GoodsInfo/ajaxQueryByPid";
			var data = {"pid":proid};
			$.ajax({
				async:false,
				type:"post",
				url : url ,
				data : data,
				success:function (resp) {
					$("[name = ctwoId]").empty();
					console.log(resp);
					if (resp.length > 0){
						$.each(resp,function (i,item) {
							console.log(item.ctwoId+":"+item.ctwoName)
							$("<option value='"+ item.ctwoId +"'>"+item.ctwoName+"</option>").appendTo("[name = ctwoId]");
						})
						ajaxCthr();//bb.自动联动三级用的,没有三级分类,这行删除
					}
				},
				dataType:"json"
			})
		}

至此,最后完成联动的效果如下
在这里插入图片描述
5.三级分类的联动与二级类似,代码如下

@RequestMapping("/GoodsInfo/ajaxQueryByThrPid")
    @ResponseBody
    public List<CategoryThree> ajaxQueryByThrPid(Integer pid){
        System.out.println("pid = " + pid);
        List<CategoryThree> list = categoryThreeService.queryByThrPid(pid);
        System.out.println("list = " + list);
        return list ;
    }

categoryThree模型层代码

@Override
    public List<CategoryThree> queryByThrPid(Integer pid) {
        return categoryThreeMapper.selectListByThrPid(pid);
    }

xml文件

<select id="selectListByThrPid" resultMap="resultMap">
        select cthr_id, cone_id, ctwo_id, cthr_name, cthr_num, cthr_code, cthr_info
        from category_three
        where ctwo_id = #{id}
    </select>

add_goodsinfo.html

function ajaxCthr() {
			var proid = $("#ctwoId").val();
			console.log(proid);
			var url = "/GoodsInfo/ajaxQueryByThrPid";
			var data = {"pid":proid};
			$.ajax({
				async:false,
				type:"post",
				url : url ,
				data : data,
				success:function (resp) {
					$("[name = cthrId]").empty();//aa.联动后剪切至二级分类,否则会有bug
					console.log(resp);
					if (resp.length > 0){
						$.each(resp,function (i,item) {
							// console.log(item.ctwoId+":"+item.cthrId)
							$("<option value='"+ item.cthrId +"'>"+item.cthrName+"</option>").appendTo("[name = cthrId]");
						})
					}
				},
				dataType:"json"
			})
		}

6.最后注意上述aa. ,bb. 的注释,bb.完成自动联动,
aa.解决一级分类下空,三级分类仍会现值的问题

最终效果如下
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值