dictionary.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <!--
  2. * @FileDescription: 字典
  3. * @Author: 黄琪勇
  4. * @Date:2022-07-08 16:37:56
  5. -->
  6. <template>
  7. <el-cascader
  8. class="dictionary"
  9. v-model="myValue"
  10. :disabled="props.disabled"
  11. :size="props.size"
  12. :show-all-levels="false"
  13. :options="props.options"
  14. :props="propsOpt"
  15. :clearable="props.clearable"
  16. :placeholder="placeholder"
  17. :popperClass="popperClass"
  18. />
  19. </template>
  20. <script setup lang="ts">
  21. import { CascaderOption, CascaderProps, CascaderValue } from "element-plus"
  22. import { computed } from "vue"
  23. import dictionaryCacheStore from "@/store/modules/dictionaryCache"
  24. const dictionaryCacheStoreHook = dictionaryCacheStore()
  25. const props = withDefaults(
  26. defineProps<{
  27. modelValue?: CascaderValue
  28. options?: CascaderOption[]
  29. clearable?: boolean
  30. propsOpt?: Omit<CascaderProps, "lazyLoad" | "emitPath" | "lazy">
  31. dictionaryKey?: string
  32. disabled?: boolean
  33. size?: "default" | "small" | "large"
  34. placeholder?: string
  35. popperClass?: string
  36. }>(),
  37. {
  38. clearable: true,
  39. propsOpt: () => {
  40. return {}
  41. }
  42. }
  43. )
  44. const emits = defineEmits<{
  45. (e: "update:modelValue", value?: CascaderValue): void
  46. (e: "change", value?: CascaderValue): void
  47. }>()
  48. const propsOpt: CascaderProps = {
  49. emitPath: Array.isArray(props.modelValue) ? true : false, //当可能为多级字典时候 emitPath为true 值为数组模式
  50. lazy: props.dictionaryKey ? true : false,
  51. leaf: "haveChild",
  52. async lazyLoad({ level, data }, resolve) {
  53. const dictionaryData = await dictionaryCacheStoreHook.getDictionary(level === 0 ? props.dictionaryKey! : (data!.itemdetailid as string))
  54. resolve(dictionaryData)
  55. }
  56. }
  57. //赋值
  58. Object.assign(propsOpt, props.propsOpt)
  59. const myValue = computed({
  60. get() {
  61. return props.modelValue
  62. },
  63. set(value) {
  64. emits("update:modelValue", value)
  65. emits("change", value)
  66. }
  67. })
  68. </script>
  69. <style lang="scss">
  70. .dictionary {
  71. width: 100%;
  72. }
  73. </style>