index3.vue 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 === ''" :key="item.value" :index="index" :class="item.elTagClass">
  6. {{ item.label + ' ' }}
  7. </span>
  8. <el-tag
  9. v-else
  10. :key="item.value + ''"
  11. :disable-transitions="true"
  12. :index="index"
  13. :type="
  14. item.elTagType === 'primary' ||
  15. item.elTagType === 'success' ||
  16. item.elTagType === 'info' ||
  17. item.elTagType === 'warning' ||
  18. item.elTagType === 'danger'
  19. ? item.elTagType
  20. : 'default'
  21. "
  22. :class="item.elTagClass"
  23. >
  24. {{ item.label + ' ' }}
  25. </el-tag>
  26. </template>
  27. </template>
  28. <template v-if="unmatch && showValue">
  29. {{ unmatchArray }}
  30. </template>
  31. </div>
  32. </template>
  33. <script setup lang="ts">
  34. interface Props {
  35. options: Array<DictDataOption>;
  36. value: number | string | Array<number | string>;
  37. showValue?: boolean;
  38. separator?: string;
  39. }
  40. const props = withDefaults(defineProps<Props>(), {
  41. showValue: true,
  42. separator: ','
  43. });
  44. const values = computed(() => {
  45. if (props.value === '' || props.value === null || typeof props.value === 'undefined') return [];
  46. return Array.isArray(props.value) ? props.value.map((item) => '' + item) : String(props.value).split(props.separator);
  47. });
  48. const unmatch = computed(() => {
  49. if (props.options?.length == 0 || props.value === '' || props.value === null || typeof props.value === 'undefined') return false;
  50. // 传入值为非数组
  51. let unmatch = false; // 添加一个标志来判断是否有未匹配项
  52. values.value.forEach((item) => {
  53. if (!props.options.some((v) => v.value === item)) {
  54. unmatch = true; // 如果有未匹配项,将标志设置为true
  55. }
  56. });
  57. return unmatch; // 返回标志的值
  58. });
  59. const unmatchArray = computed(() => {
  60. // 记录未匹配的项
  61. const itemUnmatchArray: Array<string | number> = [];
  62. if (props.value !== '' && props.value !== null && typeof props.value !== 'undefined') {
  63. values.value.forEach((item) => {
  64. if (!props.options.some((v) => v.value === item)) {
  65. itemUnmatchArray.push(item);
  66. }
  67. });
  68. }
  69. // 没有value不显示
  70. return handleArray(itemUnmatchArray);
  71. });
  72. const handleArray = (array: Array<string | number>) => {
  73. if (array.length === 0) return '';
  74. return array.reduce((pre, cur) => {
  75. return pre + ' ' + cur;
  76. });
  77. };
  78. </script>
  79. <style scoped>
  80. .el-tag + .el-tag {
  81. margin-left: 10px;
  82. }
  83. </style>