index.vue 2.3 KB

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