若依/RuoYi框架(Thymeleaf模板引擎)前端遍历list显示select下拉框,根据选择option动态加载内容

一、前端遍历list显示select下拉框

后台代码:

@GetMapping("/addPets/{id}")
    public String addPets(@PathVariable("id") Long id, ModelMap mmap)
    {
        Users users = usersService.selectUsersById(id);
        List<Pets> list = petsService.selectPetsList(new Pets());
        mmap.put("petsList", list);
        Map<Object, Object> map = new HashMap<>();
        for(Pets pets : list){
            map.put(pets.getLevel(), pets);
        }
        mmap.put("list", JSONObject.toJSON(map));
        return prefix + "/addPets";
    }

前端代码:

<label class="col-sm-3 control-label is-required">宠物:</label>
                <div class="col-sm-8">
                    <select name="level" id="level" lay-verify="required" class="form-control">
                        <option th:each="pets:${petsList}" th:value="${pets.id}"
                                th:text="${pets.name}"></option>
                    </select>
                </div>
petsList后台获取到的集合th:each进行遍历,pets作为集合对象的变量名称,th:value="${pets.id}作为value值
th:text="${pets.name}"作为需要显示的内容

效果如下:

 

二、根据选择option动态加载内容

效果如下:

前端代码:

<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('分发宠物')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <input name="petsLists" id="petsLists" type="hidden" th:value="${list}">
        <form class="form-horizontal m" id="form-users-edit" th:object="${users}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">手机号:</label>
                <div class="col-sm-8">
                    <input name="phone" th:field="*{phone}" class="form-control" type="text" required readonly="readonly">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">宠物:</label>
                <div class="col-sm-8">
                    <select name="level" id="level" lay-verify="required" class="form-control">
                        <option th:each="pets:${petsList}" th:value="${pets.level}"
                                th:text="${pets.name}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group" style="display:none" id="levelDiv">
                <label class="col-sm-3 control-label is-required">级别:</label>
                <div class="col-sm-8">
                    <input name="levell" id="levell"  class="form-control" type="text" required readonly="readonly">
                </div>
            </div>
            <div class="form-group" style="display:none" id="priceDiv">
                <label class="col-sm-3 control-label is-required">价值:</label>
                <div class="col-sm-8">
                    <input name="priceLimit" id="priceLimit" class="form-control" type="text" required readonly="readonly">
                </div>
            </div>
            <div class="form-group" style="display:none" id="profitDiv">
                <label class="col-sm-3 control-label is-required">合约收益:</label>
                <div class="col-sm-8">
                    <input name="profit" id="profit" class="form-control" type="text" required readonly="readonly">
                </div>
            </div>
            <div class="form-group" style="display:none" id="spendDiv">
                <label class="col-sm-3 control-label is-required">预约/直购领养消耗积分:</label>
                <div class="col-sm-8">
                    <input name="spend" id="spend" class="form-control" type="text" required readonly="readonly">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">状态:</label>
                <div class="col-sm-8">
                    <select name="state" lay-verify="required" class="form-control">
                        <option value="0">收益中(有收益)</option>
                        <option value="1">待转让(无收益)</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">价值:</label>
                <div class="col-sm-8">
                    <input name="price" class="form-control" type="text" required>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script type="text/javascript">
        var prefix = ctx + "system/users";
        $("#form-users-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/saveAddPets", $('#form-users-edit').serialize());
            }
        }

        $("#level").change(function() {
            var petsLists = $("#petsLists").val();
            var json = JSON.parse(petsLists);
            var level = $("#level").val();
            var objects = json[level];
            $("#levelDiv").show();
            $("#levell").val(objects.level);
            $("#priceDiv").show();
            $("#priceLimit").val(objects.priceMin + " - " + objects.priceMix);
            $("#profitDiv").show();
            $("#profit").val(objects.profitDays + "天/" + objects.profitRate * 100 + "%");
            $("#spendDiv").show();
            $("#spend").val(objects.appointmentAmount + " / " + objects.payAmount);
        });
    </script>
</body>
</html>

1、

 

首先将petsList转为json对象,然后把select选择的值看作key去获取其pets对象,接下来把相应div设为可见并且赋予对应的值即可。

 

注:默认其div为隐藏不可见,当select有变化时将其设为可见。

 

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zgsdzczh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值