From 14538ce399667e659419f393ea159efc91ff8dee Mon Sep 17 00:00:00 2001 From: 抓蛙师 <770492966@qq.com> Date: 星期五, 08 九月 2023 18:35:17 +0800 Subject: [PATCH] 优化字典标签支持数组和多标签 --- src/views/system/client/index.vue | 6 -- src/components/DictTag/index.vue | 57 ++++++++++++---------------- 2 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/components/DictTag/index.vue b/src/components/DictTag/index.vue index 08aef21..0fe0581 100644 --- a/src/components/DictTag/index.vue +++ b/src/components/DictTag/index.vue @@ -2,7 +2,8 @@ <div> <template v-for="(item, index) in options"> <template v-if="values.includes(item.value)"> - <span v-if="item.elTagType == 'default' || item.elTagType == ''" :key="item.value" :index="index" :class="item.elTagClass"> + <span v-if="item.elTagType == 'default' || item.elTagType == ''" :key="item.value" :index="index" + :class="item.elTagClass"> {{ item.label + " " }} </span> <el-tag @@ -24,7 +25,7 @@ </template> <script setup lang="ts"> -import { propTypes } from '@/utils/propTypes'; +import {propTypes} from '@/utils/propTypes'; const props = defineProps({ @@ -37,46 +38,38 @@ value: [Number, String, Array] as PropType<number | string | Array<number | string>>, // 褰撴湭鎵惧埌鍖归厤鐨勬暟鎹椂锛屾樉绀簐alue showValue: propTypes.bool.def(true), -}); - -const values = computed(() => { - if (props.value !== null && typeof props.value !== "undefined") { - return Array.isArray(props.value) ? props.value : [String(props.value)]; - } else { - return []; + separator: { + type: String, + default: ',' } +}); +const values = computed(() => { + if (props.value === '' || props.value === null || typeof props.value === "undefined") return [] + let valu= Array.isArray(props.value) ? props.value.map(item => '' + item) : String(props.value).split(props.separator); + console.log("valu",valu) + return valu }); const unmatch = computed(() => { - if (props.value !== null && typeof props.value !== "undefined") { - // 浼犲叆鍊间负闈炴暟缁� - if (!Array.isArray(props.value)) { - if (props.options.some((v) => v.value == props.value)) { - return false; - } - return true; + if (props.options?.length == 0 || props.value === '' || props.value === null || typeof props.value === "undefined") return false + // 浼犲叆鍊间负闈炴暟缁� + values.value.forEach(item => { + if (!props.options.some(v => v.value === item)) { + return true // 濡傛灉鏈夋湭鍖归厤椤癸紝灏嗘爣蹇楄缃负true } - return true; - } - // 娌℃湁value涓嶆樉绀� - return false; + }) + return false // 杩斿洖鏍囧織鐨勫�� }); const unmatchArray = computed(() => { // 璁板綍鏈尮閰嶇殑椤� const itemUnmatchArray: Array<string | number> = []; - if (props.value !== null && typeof props.value !== "undefined") { - // 浼犲叆鍊间负闈炴暟缁� - if (!Array.isArray(props.value)) { - itemUnmatchArray.push(props.value); - } else { - // 浼犲叆鍊间负Array - props.value.forEach((item) => { - if (!props.options.some((v) => v.value == item)) { - itemUnmatchArray.push(item); - } - }); - } + if (props.value !== '' && props.value !== null && typeof props.value !== "undefined") { + values.value.forEach(item => { + if (!props.options.some(v => v.value === item)) { + itemUnmatchArray.push(item); + } + }) } // 娌℃湁value涓嶆樉绀� return handleArray(itemUnmatchArray); diff --git a/src/views/system/client/index.vue b/src/views/system/client/index.vue index 827eaf2..0780c82 100644 --- a/src/views/system/client/index.vue +++ b/src/views/system/client/index.vue @@ -53,11 +53,7 @@ <el-table-column label="瀹㈡埛绔閽�" align="center" prop="clientSecret" /> <el-table-column label="鎺堟潈绫诲瀷" align="center"> <template #default="scope"> - <div> - <template v-for="(type, index) in scope.row.grantTypeList" :key="index"> - <dict-tag class="el-check-tag" :options="sys_grant_type" :value="type" /> - </template> - </div> + <dict-tag :options="sys_grant_type" :value="scope.row.grantTypeList" /> </template> </el-table-column> <el-table-column label="璁惧绫诲瀷" align="center"> -- Gitblit v1.9.3