index.vue 2.2 KB

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