index.vue 2.1 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. :key="item.value"
  7. :index="index"
  8. :class="item.elTagClass"
  9. >{{ item.label + " " }}</span>
  10. <!-- <up-tag
  11. v-else
  12. :disable-transitions="true"
  13. :key="item.value + ''"
  14. :index="index"
  15. :type="item.elTagType"
  16. :class="item.elTagClass"
  17. :text="item.label + ''"
  18. ></up-tag> -->
  19. </template>
  20. </template>
  21. <template v-if="unmatch && showValue">
  22. {{ unmatchArray | handleArray }}
  23. </template>
  24. </div>
  25. </template>
  26. <script setup>
  27. import { computed, ref } from "vue";
  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 === '' || !Array.isArray(props.options) || 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>