ExtDSelect.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <el-select
  3. :size="size"
  4. transfer
  5. :disabled="disabled"
  6. :multiple="multiple"
  7. filterable
  8. clearable
  9. style="width: 100%"
  10. @change="handleChange"
  11. :placeholder="placeholder"
  12. v-model="state.dataVal">
  13. <template #empty>
  14. <el-empty :image-size="40" description=""></el-empty>
  15. </template>
  16. <el-option
  17. v-for="(item,idx) in state.dicts"
  18. :key="idx"
  19. :label="item.name||item.label"
  20. :value="item.value">
  21. <span class="option-item" style="float: left"><i :style="setupColorStyle(state.colorList[item.value%8])">{{ item.name || item.label }}</i></span>
  22. <!-- :style="setupColorStyle(item.color)"-->
  23. </el-option>
  24. </el-select>
  25. </template>
  26. <script setup lang="ts" name="ExtDSelect">
  27. import {onMounted, reactive, watch,nextTick} from 'vue';
  28. import {Session} from "/@/utils/storage";
  29. import u from "/@/utils/u";
  30. const props = defineProps({
  31. modelValue: {
  32. type: [Number, String]
  33. },
  34. //可选的值(字典二次过滤)
  35. values: {
  36. type: Array,
  37. default: () => {
  38. return []
  39. }
  40. },
  41. type: {
  42. type: String,
  43. required: true
  44. },
  45. dataRange: {
  46. type: Array<IFieldRange>
  47. },
  48. disabled: {
  49. type: Boolean,
  50. default: false
  51. },
  52. multiple: {
  53. type: Boolean,
  54. default: false
  55. },
  56. placeholder: {
  57. type: String,
  58. default: '请选择'
  59. },
  60. size: {
  61. type: String,
  62. default: 'default'
  63. }
  64. })
  65. const state = reactive({
  66. dicts: [] as Array<any>,
  67. dataVal: null as any,
  68. colorList: ["#FFB800", "#009688", "#1E9FFF", "#00C7D2", "#599CDE", "#FF5722", "#eb2f96", "#4a5055"],
  69. dictList: {
  70. 'User.status': [
  71. {label: '有效', value: 1},
  72. {label: '无效', value: 0},
  73. ]
  74. }
  75. })
  76. const emit = defineEmits(['update:modelValue', 'on-change']);
  77. const typeOf = (obj: any) => {
  78. return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
  79. }
  80. watch(() => props.modelValue, (val, oldVal) => {
  81. console.log('ExtDSelect', val, oldVal,typeOf(val) )
  82. nextTick(()=>{
  83. if (typeOf(val) === "number") {
  84. state.dataVal = val + "";
  85. }else{
  86. state.dataVal = val;
  87. }
  88. })
  89. },{immediate:true})
  90. const handleChange = (val: number | Array<number>) => {
  91. console.log("handleChange", val)
  92. emit("update:modelValue", val)
  93. emit("on-change", val)
  94. }
  95. const setupColorStyle = (hex: string = "#000000") => {
  96. let hexToRgb = u.hexToRgb(hex);
  97. let {r, g, b} = hexToRgb;
  98. let v = {
  99. 'text-shadow': `2px 2px 3px rgba(${r},${g},${b},0.2)`,
  100. // 'background-color': `rgba(${r},${g},${b},0.2)`,
  101. 'color': `rgb(${r},${g},${b})`
  102. }
  103. return v;
  104. }
  105. const setupDicts = () => {
  106. let dictList: Array<any> = [];
  107. if (!u.isEmptyOrNull(props.dataRange)) {
  108. dictList = props.dataRange;
  109. } else {
  110. let dicts = Session.get("dicts");
  111. if (!u.isEmptyOrNull(dicts)) {
  112. dictList = dicts[props.type];
  113. } else {
  114. dictList = state.dictList[props.type]
  115. }
  116. }
  117. state.dicts = dictList;
  118. console.table(dictList)
  119. }
  120. onMounted(() => {
  121. setupDicts();
  122. });
  123. </script>
  124. <style scoped lang="scss">
  125. .option-item {
  126. display: inline-block;
  127. }
  128. </style>