Explorar el Código

优化字典标签支持自定义分隔符

RuoYi hace 2 años
padre
commit
adec3f0608
Se han modificado 1 ficheros con 23 adiciones y 26 borrados
  1. 23 26
      ruoyi-ui/src/components/DictTag/index.vue

+ 23 - 26
ruoyi-ui/src/components/DictTag/index.vue

@@ -7,7 +7,7 @@
7 7
           :key="item.value"
8 8
           :index="index"
9 9
           :class="item.raw.cssClass"
10
-          >{{ item.label + " " }}</span
10
+          >{{ item.label + ' ' }}</span
11 11
         >
12 12
         <el-tag
13 13
           v-else
@@ -17,7 +17,7 @@
17 17
           :type="item.raw.listClass == 'primary' ? '' : item.raw.listClass"
18 18
           :class="item.raw.cssClass"
19 19
         >
20
-          {{ item.label + " " }}
20
+          {{ item.label + ' ' }}
21 21
         </el-tag>
22 22
       </template>
23 23
     </template>
@@ -40,6 +40,10 @@ export default {
40 40
     showValue: {
41 41
       type: Boolean,
42 42
       default: true,
43
+    },
44
+    separator: {
45
+      type: String,
46
+      default: ","
43 47
     }
44 48
   },
45 49
   data() {
@@ -49,39 +53,32 @@ export default {
49 53
   },
50 54
   computed: {
51 55
     values() {
52
-      if (this.value !== null && typeof this.value !== "undefined") {
53
-        return Array.isArray(this.value) ? this.value : [String(this.value)];
54
-      } else {
55
-        return [];
56
-      }
56
+      if (this.value === null || typeof this.value === 'undefined' || this.value === '') return []
57
+      return Array.isArray(this.value) ? this.value.map(item => '' + item) : String(this.value).split(this.separator)
57 58
     },
58 59
     unmatch() {
59
-      this.unmatchArray = [];
60
-      if (this.value !== null && typeof this.value !== "undefined") {
61
-        // 传入值为非数组
62
-        if (!Array.isArray(this.value)) {
63
-          if (this.options.some((v) => v.value == this.value)) return false;
64
-          this.unmatchArray.push(this.value);
65
-          return true;
66
-        }
67
-        // 传入值为Array
68
-        this.value.forEach((item) => {
69
-          if (!this.options.some((v) => v.value == item))
70
-            this.unmatchArray.push(item);
71
-        });
72
-        return true;
73
-      }
60
+      this.unmatchArray = []
74 61
       // 没有value不显示
75
-      return false;
62
+      if (this.value === null || typeof this.value === 'undefined' || this.value === '' || this.options.length === 0) return false
63
+      // 传入值为数组
64
+      let unmatch = false // 添加一个标志来判断是否有未匹配项
65
+      this.values.forEach(item => {
66
+        if (!this.options.some(v => v.value === item)) {
67
+          this.unmatchArray.push(item)
68
+          unmatch = true // 如果有未匹配项,将标志设置为true
69
+        }
70
+      })
71
+      return unmatch // 返回标志的值
76 72
     },
73
+
77 74
   },
78 75
   filters: {
79 76
     handleArray(array) {
80
-      if (array.length === 0) return "";
77
+      if (array.length === 0) return '';
81 78
       return array.reduce((pre, cur) => {
82
-        return pre + " " + cur;
79
+        return pre + ' ' + cur;
83 80
       })
84
-    }
81
+    },
85 82
   }
86 83
 };
87 84
 </script>