dict-utils.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { storeToRefs } from 'pinia';
  2. import { useUserInfo } from '/@/stores/userInfo';
  3. const stores = useUserInfo();
  4. const { dictList } = storeToRefs(stores);
  5. // 用于在 Table 中把字段的代码转换为名称,示例如下:
  6. /*
  7. import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
  8. <el-table-column prop="字段名" label="描述" width="140">
  9. <template #default="scope">
  10. <el-tag :type="di('字典名代码', scope.row.credentialsType)?.tagType"> [{{di("字典名代码", scope.row.credentialsType)?.code}}]{{di("字典名代码", scope.row.credentialsType)?.value}} </el-tag>
  11. </template>
  12. </el-table-column>
  13. */
  14. export function getDictDataItem(dicName:string, dicItemCode:string): any{
  15. const dict = dictList.value.filter(item => item.code === dicName);
  16. if (dict.length === 0)
  17. return null;
  18. const dictData = dict[0].children.filter(item => item.code === dicItemCode);
  19. if (dictData.length === 0)
  20. return null;
  21. return dictData[0];
  22. }
  23. // select 控件使用,用于获取字典列表,示例如下:
  24. /*
  25. import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
  26. <el-select clearable v-model="ruleForm.字段" placeholder="请选择证件提示">
  27. <el-option v-for="(item,index) in dl('字段名名码')" :key="index" :value="item.code" :label="`[${item.code}] ${item.value}`"></el-option>
  28. </el-select>
  29. */
  30. export function getDictType(dicName:string): any{
  31. const dict = dictList.value.filter(item => item.code === dicName);
  32. if (dict.length === 0)
  33. return null;
  34. return dict[0];
  35. }
  36. export function getDictDataList(dicName:string): any{
  37. const result = getDictType(dicName)?.children;
  38. return result ?? [];
  39. }