Springboot+layui实现二级联动

二级联动 网上搜了好久都没看懂,终于看懂了一个:Springboot+Mybatis+Jquery实现自定义二级联动。将它改了下改成了layui的。

html代码

<div class="layui-col-xs2 layui-input-inline">
				<form class="layui-form layui-form-pane">
					<select lay-filter="selectTheme" id="board" selected="selected"><!--一级-->
						<option value="" selected=""></option>
						<option th:each="b:${board}" th:text="${b.boName}" th:value="${b.boId}"></option>
					</select>
				</form>
			</div>
			<div class="layui-col-xs1 layui-input-inline">
				<form class="layui-form layui-form-pane" action="">
					<select id="theme"><!--二级-->
					</select>
				</form>
			</div>

上面比较重要的是lay-filter=“selectTheme”(代替onchange来响应),id=“board”,id=“theme”。

js代码

layui.use(['form'], function() {
		var $ = layui.$;
		var form = layui.form;
		form.on('select(selectTheme)', function (data) {//对应lay-filter="selectTheme"
			var index1 = $('#board option:selected').val();//对应id="board"  得到一级菜单选中值的value
			var optionJson = [];
			$.ajax({
				url: "/selectTheme",//对应controller层
				type: "post",
				data: {"bo": index1},//将选中值的value传给controller
				datatype: "json",
				success: function (data) {
					optionJson = data;
					var selectObj = document.getElementById("theme");//对应id="theme"
					selectObj.options.length = 0;
					for (var i = 0; i < data.length; i++) {
						selectObj.add(new Option(optionJson[i].thName, optionJson[i].thId));//对应数据库中名字,两个参数是(文本,value)
					}
					form.render();
				}
			})
		});
	})

controller

@Autowired
    private IBoardService bs;
@Autowired
    private IThemeService ts;
    
@RequestMapping("/index")
    public String index(Model model){
        List<Board> board=bs.selectAllBoard();
        model.addAttribute("board",board);
        return "index";
    }

    @RequestMapping(value = "/selectTheme", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<Theme> selectCollege(HttpServletRequest request){
        String board=request.getParameter("bo");//得到value值
        System.out.println(board);
        List<Theme> theme=ts.selectTheme(Integer.valueOf(board));//在数据库查询
        return theme;
    }

IBoardService 与 IThemeService 就只是根据条件查询数据库

以及数据库:
board:在这里插入图片描述
theme:在这里插入图片描述
实现效果:
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值