ExtDSelect.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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} 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. watch(() => props.modelValue, (val, oldVal) => {
  78. console.log('ExtDSelect', val, oldVal)
  79. state.dataVal = props.modelValue;
  80. })
  81. const handleChange = (val: number | Array<number>) => {
  82. console.log("handleChange", val)
  83. emit("update:modelValue", val)
  84. emit("on-change", val)
  85. }
  86. const setupColorStyle = (hex: string = "#000000") => {
  87. let hexToRgb = u.hexToRgb(hex);
  88. let {r, g, b} = hexToRgb;
  89. let v = {
  90. 'text-shadow': `2px 2px 3px rgba(${r},${g},${b},0.2)`,
  91. // 'background-color': `rgba(${r},${g},${b},0.2)`,
  92. 'color': `rgb(${r},${g},${b})`
  93. }
  94. return v;
  95. }
  96. onMounted(() => {
  97. state.dataVal = props.modelValue;
  98. if (!u.isEmptyOrNull(props.dataRange)) {
  99. state.dicts = props.dataRange;
  100. } else {
  101. const dicts = Session.get("dicts");
  102. if (!u.isEmptyOrNull(dicts)) {
  103. state.dicts = dicts[props.type];
  104. } else {
  105. state.dicts = state.dictList[props.type]
  106. console.table(state.dicts)
  107. }
  108. }
  109. });
  110. </script>
  111. <style scoped lang="scss">
  112. .option-item {
  113. display: inline-block;
  114. }
  115. </style>