index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. :key="item.value + ''"
  12. :disable-transitions="true"
  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. interface Props {
  28. options: Array<DictDataOption>;
  29. value: number | string | Array<number | string>;
  30. showValue: boolean;
  31. separator: string;
  32. }
  33. const props = withDefaults(defineProps<Props>(), {
  34. showValue: true,
  35. separator: ','
  36. });
  37. const values = computed(() => {
  38. if (props.value === '' || props.value === null || typeof props.value === 'undefined') return [];
  39. return Array.isArray(props.value) ? props.value.map((item) => '' + item) : String(props.value).split(props.separator);
  40. });
  41. const unmatch = computed(() => {
  42. if (props.options?.length == 0 || props.value === '' || props.value === null || typeof props.value === 'undefined') return false;
  43. // 传入值为非数组
  44. values.value.forEach((item) => {
  45. if (!props.options.some((v) => v.value === item)) {
  46. return true; // 如果有未匹配项,将标志设置为true
  47. }
  48. });
  49. return false; // 返回标志的值
  50. });
  51. const unmatchArray = computed(() => {
  52. // 记录未匹配的项
  53. const itemUnmatchArray: Array<string | number> = [];
  54. if (props.value !== '' && props.value !== null && typeof props.value !== 'undefined') {
  55. values.value.forEach((item) => {
  56. if (!props.options.some((v) => v.value === item)) {
  57. itemUnmatchArray.push(item);
  58. }
  59. });
  60. }
  61. // 没有value不显示
  62. return handleArray(itemUnmatchArray);
  63. });
  64. const handleArray = (array: Array<string | number>) => {
  65. if (array.length === 0) return '';
  66. return array.reduce((pre, cur) => {
  67. return pre + ' ' + cur;
  68. });
  69. };
  70. </script>
  71. <style scoped>
  72. .el-tag + .el-tag {
  73. margin-left: 10px;
  74. }
  75. </style>