简单的数据字典前端实现

最近因为业务需求,需要用到数据字典,于是自己动手写了个简单的前端实现,在这里记录一下,以备以后查看。

首先说说思路,把数据字典所有数据取出来,拼成json字符串,发送到前台,由前台解析成json对象,并应用到各个页面。

json字符串格式是这样的:

[
    {
        "groupName":  {
                        "typeCode1": "typeText1",
                        "typeCode2": "typeText2",
                        "typeCode3": "typeText3"
        },
        "groupName": {
                        "typeCode1": "typeText1",
                        "typeCode2": "typeText2",
                        "typeCode3": "typeText3"
        }
    }
]

java端取数据拼字符串代码:

    @Transactional(readOnly = true)
	public String getAllTypeGroup() {
        //获取数据字典中所有数据
		List<TSTypegroup> typeGroups = this.commonDao.loadAll(TSTypegroup.class);

		StringBuffer dic = new StringBuffer();
		dic.append("{");
		for (int j = 0; j < typeGroups.size(); j++) {
			TSTypegroup typeGroup = typeGroups.get(j);
			if (j==0){
				dic.append("\"").append(typeGroup.getTypegroupcode()).append("\": {");
				List<TSType> tsTypes = typeGroup.getTSTypes();
				for (int i = 0; i < tsTypes.size(); i++) {
					TSType tsType =  tsTypes.get(i);
					if (i==0){
						dic.append("\"").append(tsType.getTypecode()).append("\": \"").append(tsType.getTypename()).append("\"");
					}else {
						dic.append(",\"").append(tsType.getTypecode()).append("\": \"").append(tsType.getTypename()).append("\"");
					}
				}
				dic.append("}");
			}else {
				dic.append(",\"").append(typeGroup.getTypegroupcode()).append("\": {");
				List<TSType> tsTypes = typeGroup.getTSTypes();
				for (int i = 0; i < tsTypes.size(); i++) {
					TSType tsType =  tsTypes.get(i);
					if (i==0){
						dic.append("\"").append(tsType.getTypecode()).append("\": \"").append(tsType.getTypename()).append("\"");
					}else {
						dic.append(",\"").append(tsType.getTypecode()).append("\": \"").append(tsType.getTypename()).append("\"");
					}
				}
				dic.append("}");
			}
		}
		dic.append("}");
		return dic.toString();
	}

json字符串拼接好后,在登陆时将他传递到后台主界面中,主界面接收后台数据并处理成数据字典对象:

var dic = $.parseJSON('${dicList}');

子界面调用父页面数据字典:

//数据字典对象
    var dic = parent.dic;

数据字典的使用:

//根据字典code获取到字典数据
var TSType = dic['saleMethod'];
//根据当前需要转换的code获取到文本值
return TSType[value];

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue中实现前端数据字典可以通过以下步骤进行: 1. 首先,在后端维护好字典类型和对应的字典值。可以使用一个API来获取字典数据。 2. 在前端,可以通过调用后端提供的API来获取字典数据。可以使用Vue的生命周期钩子函数或者watch来监听数据字典类型的变化,当数据字典类型的值发生变化时,调用API获取对应的字典值。 3. 在Vue组件中,可以使用一个Vue实例的变量来存储字典值,比如`dictionaryItemList`。当获取到字典值后,将其赋值给`dictionaryItemList`。 4. 在模板中,可以使用v-for指令遍历`dictionaryItemList`,将字典值展示在下拉列表或其他地方。 下面是一个简单的示例代码,用于展示如何在Vue中实现前端数据字典: ```javascript <template> <div> <select v-model="selectedItem"> <option v-for="item in dictionaryItemList" :value="item.value">{{ item.label }}</option> </select> </div> </template> <script> export default { data() { return { code: 'gender', // 数据字典类型代码 dictionaryItemList: [], // 存储字典值的数组 selectedItem: '' // 选择的字典项 } }, watch: { code: { immediate: true, handler: 'loadData' } }, methods: { loadData() { if (this.code) { this.dictionaryItemList = [] // 清空之前的字典值 this.$api.system.dictionaryType.getItem(this.code).then(res => { this.dictionaryItemList = res.data // 获取到字典值后赋值给dictionaryItemList }) } } } } </script> ``` 在上面的示例代码中,`code`表示数据字典类型的编码,`dictionaryItemList`用于存储字典值,`selectedItem`表示当前选中的字典项。通过watch监听`code`的变化,当`code`发生变化时,调用`loadData`方法获取对应的字典值,然后将其赋值给`dictionaryItemList`。在模板中,使用v-for指令遍历`dictionaryItemList`生成下拉列表的选项。 注意,上述示例代码中的`this.$api.system.dictionaryType.getItem`是一个示例API,具体的API调用需要根据实际情况进行修改。 总结起来,Vue中实现前端数据字典可以通过监听数据字典类型的变化,在合适的时机调用API获取字典值,并将其存储在Vue实例的变量中,然后在模板中展示字典值。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值