若依vue根据字典Dict的value获取Label,类似于通过key获得value

1292人浏览 / 0人评论 / 添加收藏

背景
项目需求需要根据字典的value来获取label进行展示,若依提供了selectDictLabel, selectDictLabels两个方法通过value获取label

main.js里已挂载了这两个方法:


方法的使用:


这两个方法的源码
selectDictLabel(); datas(字典列表), value(当前值) 回显数据字典

// 回显数据字典
export function selectDictLabel(datas, value) {
 if (value === undefined) {
   return "";
 }
 var actions = [];
 Object.keys(datas).some((key) => {
   if (datas[key].value == ('' + value)) {
     actions.push(datas[key].label);
     return true;
   }
 })
 if (actions.length === 0) {
   actions.push(value);
 }
 return actions.join('');
}

// 回显数据字典(字符串数组)
export function selectDictLabels(datas, value, separator) {
 if (value === undefined) {
   return "";
 }
 var actions = [];
 var currentSeparator = undefined === separator ? "," : separator;
 var temp = value.split(currentSeparator);
 Object.keys(value.split(currentSeparator)).some((val) => {
   var match = false;
   Object.keys(datas).some((key) => {
     if (datas[key].value == ('' + temp[val])) {
       actions.push(datas[key].label + currentSeparator);
       match = true;
     }
   })
   if (!match) {
     actions.push(temp[val] + currentSeparator);
   }
 })
 return actions.join('').substring(0, actions.join('').length - 1);
}
 

全部评论