index.vue 2.2 KB

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