利用若依的数字字典进行下拉框的关联配置

利用若依的数字字典进行下拉框的关联配置



前言

由于目前参与的项目当中需要对于页面的下拉框需要出现关联关系,并且父和子的数据都是通过数据字典进行维护的。在此对开发文档进行记录避免之后忘记。


提示:以下是本篇文章正文内容,下面案例可供参考

一、数字字典配置

由于若依数字字典本身并没有多级关联关系所以只能对于父下拉框下的每一个项目再配置一个新的数字字典(本人目前是这么做的,如果有更好的方法可以在评论区交流)在这里插入图片描述
在这里插入图片描述
配置时需要注意父下拉框下的项目名称与子数据字典创建时名称需要保持一致。

二、使用步骤

1.引入库

代码如下(示例):

    $("#module").bind('change', function () {
        var option = $.form.selectSelects("module");
        var label='';
        if(option=='1'){
            label=[[${@dict.getLabel1('sys_module_type','1')}]];
        }else if (option =='2' ){
            label=[[${@dict.getLabel1('sys_module_type','2')}]];
        }else if (option =='3' ){
            label=[[${@dict.getLabel1('sys_module_type','3')}]];
        }
        $.ajax({
            cache: true,
            type: "POST",
            url: ctx + "system/dict/list1",
            data: {
                "label": label
            },
            async: false,
            error: function (request) {
                $.modal.alertError("系统错误");
            },
            success: function (data) {
                $("#event").next().remove();
                $("#event").after($.common.dictToSelect(data, 0, 'event'));
            }
        });
    });

在需要有下拉框关联关系的页面需要添加一个异步函数来绑定父下拉框,在父下拉框更改之后通过异步加载的方式加载子下拉框。
这里的getLabel1是我修改若依本身的方法自己不过也大同小异,主要目的与原方法一致,自己重新创建方法主要是避免破坏原有的方法,因为这个方法是其他地方会调用。
注意:由于在异步方法中绑定了change事件对于父下拉框获得当前选中的下拉项的值,再调用service层的方法时“[[${@dict.getLabel1(‘sys_module_type’,‘3’)}]]” 传入的只能是一个固定的字符串,这也就是我为什么写的是if else if这种形式的判断。

/**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    public String getLabel1(String dictType, String dictValue)
    {
        return dictDataService.selectDictLabel1(dictType, dictValue);
    }

service 接口


String selectDictLabel1(String dictType, String dictValue);
    

service 接口实现类

/**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType  字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    public String selectDictLabel1(String dictType, String dictValue) {
        if (dictValue == null || dictValue.equals("")) {
            dictValue = "0";
        }
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

Mapper 接口

/**
     * 根据字典类型和字典键值查询字典数据信息
     * 
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    public String selectDictLabel(@Param("dictType") String dictType, @Param("dictValue") String dictValue);

Mapper xml

<select id="selectDictLabel" resultType="String">
		select dict_label from sys_dict_data
		where dict_type = #{dictType} and dict_value = #{dictValue}
	</select>

通过调用service层的方法对于数据库查询之后得到下拉框项的名称。
之后ajax方法根据url对于传入的label去数据库查询之后进行查找

@PostMapping("/list1")
  @ResponseBody
  public List<SysDictData> list1(String label)
  {
      List<SysDictData> list = dictTypeService.selectDictTypeList1(label);
      return list;
  }
		<sql id="selectDictDataVo">
        select dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by,  create_time, remark 
		from sys_dict_data
    </sql>
		<include refid="selectDictDataVo"/>
		where status = '0' and  DICT_TYPE =
		(SELECT DICT_TYPE FROM SYS_DICT_TYPE WHERE DICT_NAME = #{dictLabel})
		ORDER BY DICT_SORT ASC
	success: function (data) {
                $("#event").next().remove();
                $("#event").after($.common.dictToSelect(data, 0, 'event'));
            }

在将查到的数据展示到相应的子下拉框即可。
最终效果如图
在这里插入图片描述

总结

目前实现父下拉框与子下拉框的关联关系需要配置多个子数据字典,比较麻烦,不过配置一次就可以正常使用
  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前端多个下拉框的代码通常涉及到以下几个方面: 1. HTML代码:定义下拉框的标签,包括下拉框的ID、Name、选项等。 2. JavaScript代码:实现下拉框的交互功能,包括下拉框的选择事件、数据获取等。 3. CSS代码:定义下拉框的样式,包括大小、颜色、字体等。 下面是一个简单的例子,展示了一个包含多个下拉框的HTML代码: ``` <html> <head> <title>多个下拉框</title> <style> .dropdown { width: 200px; height: 30px; font-size: 18px; border-radius: 5px; padding: 5px; margin-bottom: 10px; } </style> </head> <body> <select id="dropdown1" class="dropdown"> <option value="1">选项1</option> <option value="2">选项2</option> <option value="3">选项3</option> </select> <select id="dropdown2" class="dropdown"> <option value="4">选项4</option> <option value="5">选项5</option> <option value="6">选项6</option> </select> <select id="dropdown3" class="dropdown"> <option value="7">选项7</option> <option value="8">选项8</option> <option value="9">选项9</option> </select> <script> // 获取下拉框的选项 var dropdown1 = document.getElementById("dropdown1"); var dropdown2 = document.getElementById("dropdown2"); var dropdown3 = document.getElementById("dropdown3"); // 下拉框选择事件 dropdown1.onchange = function() { var selectedValue = dropdown1.options[dropdown1.selectedIndex].value; console.log("下拉框1选择的值为:" + selectedValue); } dropdown2.onchange = function() { var selectedValue = dropdown2.options[dropdown2.selectedIndex].value; console.log("下拉框2选择的值为:" + selectedValue); } dropdown3.onchange = function() { var selectedValue = dropdown3.options[dropdown3.selectedIndex].value; console.log("下拉框3选择的值为:" + selectedValue); } </script> </body> </html> ``` 在这个例子中,我们定义了三个下拉框,每个下拉框包含三个选项。我们通过JavaScript代码实现了下拉框的选择事件,当下拉框的选项改变时,会打印出选择的值。同时,我们还通过CSS代码定义了下拉框的样式,使其更美观。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值