index.vue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <template>
  2. <div>
  3. <template v-for="(item, index) in options">
  4. <template v-if="values.includes(item.value)">
  5. <span v-if="(item.elTagType === 'default' || item.elTagType === '') && (item.elTagClass === '' || item.elTagClass == null)"
  6. :key="item.value" :index="index" :class="item.elTagClass">
  7. {{ item.label + " " }}
  8. </span>
  9. <el-tag
  10. v-else
  11. :disable-transitions="true"
  12. :key="item.value + ''"
  13. :index="index"
  14. :type="(item.elTagType === 'primary' || item.elTagType === 'default')? '' : item.elTagType"
  15. :class="item.elTagClass"
  16. >
  17. {{ item.label + " " }}
  18. </el-tag>
  19. </template>
  20. </template>
  21. <template v-if="unmatch && showValue">
  22. {{ unmatchArray }}
  23. </template>
  24. </div>
  25. </template>
  26. <script setup lang="ts">
  27. import { propTypes } from '@/utils/propTypes';
  28. const props = defineProps({
  29. // 数据
  30. options: {
  31. type: Array as PropType<DictDataOption[]>,
  32. default: null,
  33. },
  34. // 当前的值
  35. value: [Number, String, Array] as PropType<number | string | Array<number | string>>,
  36. // 当未找到匹配的数据时,显示value
  37. showValue: propTypes.bool.def(true),
  38. separator: propTypes.string.def(","),
  39. });
  40. const values = computed(() => {
  41. if (props.value === '' || props.value === null || typeof props.value === "undefined") return []
  42. return Array.isArray(props.value) ? props.value.map(item => '' + item) : String(props.value).split(props.separator);
  43. });
  44. const unmatch = computed(() => {
  45. if (props.options?.length == 0 || props.value === '' || props.value === null || typeof props.value === "undefined") return false
  46. // 传入值为非数组
  47. values.value.forEach(item => {
  48. if (!props.options.some(v => v.value === item)) {
  49. return true // 如果有未匹配项,将标志设置为true
  50. }
  51. })
  52. return false // 返回标志的值
  53. });
  54. const unmatchArray = computed(() => {
  55. // 记录未匹配的项
  56. const itemUnmatchArray: Array<string | number> = [];
  57. if (props.value !== '' && props.value !== null && typeof props.value !== "undefined") {
  58. values.value.forEach(item => {
  59. if (!props.options.some(v => v.value === item)) {
  60. itemUnmatchArray.push(item);
  61. }
  62. })
  63. }
  64. // 没有value不显示
  65. return handleArray(itemUnmatchArray);
  66. });
  67. const handleArray = (array: Array<string | number>) => {
  68. if (array.length === 0) return "";
  69. return array.reduce((pre, cur) => {
  70. return pre + " " + cur;
  71. });
  72. }
  73. </script>
  74. <style scoped>
  75. .el-tag + .el-tag {
  76. margin-left: 10px;
  77. }
  78. </style>