下拉框查询字典

import { getDicts } from '@/common/api/common'   //请求后台接口
/**
 * 获取字典数据
 */
export async function useDict(...args) {
	let res = {};
	for(let i = 0; i < args.length; i++){
		let d = args[i];
		res[d] = [];
		await getDicts(d).then(resp => {
		//视具体后台接口返回字段更改
			res[d] = resp.data.map(p => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass }))
		})
	}
	return res;
}
// 回显数据字典
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('');
}
//全局挂载
import { useDict,selectDictLabel } from './dict'
Vue.prototype.useDict = useDict;
Vue.prototype.selectDictLabel = selectDictLabel;

//将方法挂载到全局后可在页面中直接使用,bus_tam_gzlx, bus_tam_clsx是字典类型
async getDics(){
  const {bus_tam_gzlx, bus_tam_clsx} = await this.useDict("bus_tam_gzlx","bus_tam_clsx");
  this.bus_tam_gzlx=[bus_tam_gzlx]
  this.bus_tam_clsx=[bus_tam_clsx]
}
//取字典值
this.form.gzlxName=this.selectDictLabel(this.bus_tam_gzlx,this.form.gzlx)
posted @ 2022-09-08 11:20  俺是前端小菜  阅读(82)  评论(0编辑  收藏  举报