index.vue 2.7 KB

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