vue3字典dict小组件 适用表单form及表格table

项目环境:
vue3 项目 element-plus 组件库

直接看组件代码

表单的单元form

<template>
	<el-select :model-value="modelValue + ''" :placeholder="placeholder" :clearable="clearable" @change="$emit('update:modelValue', $event)">
		<el-option v-for="data in dataList" :key="data.dictValue" :label="data.dictLabel" :value="data.dictValue">{{ data.dictLabel }}</el-option>
	</el-select>
</template>

<script setup lang="ts" name="FastSelect">
import store from '@/store'
import { getDictDataList } from '@/utils/tool'

const props = defineProps({
	modelValue: {
		type: [Number, String],
		required: true
	},
	dictType: {
		type: String,
		required: true
	},
	clearable: {
		type: Boolean,
		required: false,
		default: () => false
	},
	placeholder: {
		type: String,
		required: false,
		default: () => ''
	}
})

const dataList = getDictDataList(store.appStore.dictList, props.dictType)
</script>

表格的列table

<template>
	<el-table-column
		:prop="prop"
		:label="label"
		:header-align="headerAlign"
		:align="align"
		:width="width"
		:min-width="minWidth"
		:class-name="className"
	>
		<template #default="scope">
			<el-tag
				v-if="getDictLabelClass(store.appStore.dictList, props.dictType, scope.row[props.prop])"
				:type="
					getDictLabelClass(store.appStore.dictList, props.dictType, scope.row[props.prop]) === 'primary'
						? ''
						: getDictLabelClass(store.appStore.dictList, props.dictType, scope.row[props.prop])
				"
			>
				{{ getDictLabel(store.appStore.dictList, props.dictType, scope.row[props.prop]) }}
			</el-tag>
			<span v-else>
				{{ getDictLabel(store.appStore.dictList, props.dictType, scope.row[props.prop]) }}
			</span>
		</template>
	</el-table-column>
</template>

<script setup lang="ts" name="FastTableColumn">
import store from '@/store'
import { getDictLabel, getDictLabelClass } from '@/utils/tool'

const props = defineProps({
	prop: {
		type: String,
		required: true
	},
	label: {
		type: String,
		required: true
	},
	dictType: {
		type: String,
		required: true
	},
	headerAlign: {
		type: String,
		required: false,
		default: () => 'center'
	},
	align: {
		type: String,
		required: false,
		default: () => 'center'
	},
	width: {
		type: String,
		required: false,
		default: () => ''
	},
	minWidth: {
		type: String,
		required: false,
		default: () => ''
	},
	className: {
		type: String,
		required: false,
		default: () => ''
	}
})
</script>

然后分别看下依赖的模块代码

import store from '@/store'
import { getDictLabel, getDictLabelClass } from '@/utils/tool'
store就是vue3 默认的pinia状态管理,我们在登录的时候就获取到当前账号的所有字典数据
getDictLabel函数
export const getDictLabel = (dictList: any[], dictType: string, dictValue: string) => {
	const type = dictList.find((element: any) => element.dictType === dictType)
	if (type) {
		const val = type.dataList.find((element: any) => element.dictValue === dictValue + '')
		if (val) {
			return val.dictLabel
		} else {
			return dictValue
		}
	} else {
		return dictValue
	}
}

这个函数就是从pinia中查询对应的字典样式,说白了就是tag标签的位子,最终决定显示的标签内容是什么。

getDictLabelClass函数
export const getDictLabelClass = (dictList: any[], dictType: string, dictValue: string): string => {
	const type = dictList.find((element: any) => element.dictType === dictType)
	if (type) {
		const val = type.dataList.find((element: any) => element.dictValue === dictValue + '')
		if (val) {
			return val.labelClass
		} else {
			return ''
		}
	} else {
		return ''
	}
}

这个函数就是从pinia中查询对应的字典样式,说白了就是tag标签的type属性,最终决定显示的标签是什么颜色。

组件使用
form表单中
<el-form 属性省略 >
	<el-form-item label="是否新品" prop="isNew">
			<fast-select v-model="dataForm.isNew" dict-type="is_new" placeholder="是否在售必选" clearable></fast-select>
	</el-form-item>
</el-form>
table中
<el-table 属性省略 >
	<fast-table-column prop="isNew" dict-type="is_new" label="是否新品"></fast-table-column>
</el-table>
最终展示效果

form表单效果
在这里插入图片描述

table列效果
在这里插入图片描述

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中封装form表单组件的思路可以参考以下步骤: 1. 创建一个Form组件,作为整个表单的容器。该组件可以包含一个form元素,并接受一个表单数据对象作为props。 2. 在Form组件内部,使用v-for指令遍历表单数据对象的属性,并根据属性的类型渲染对应的表单项。 3. 对于每个表单项,可以创建一个FormItem组件,并根据属性的类型渲染对应的表单元素,比如input、select、textarea等。 4. FormItem组件可以接受属性的名称、类型和值作为props,并根据类型渲染不同的表单元素。 5. 可以根据需要扩展Form组件FormItem组件,添加更多的表单项类型,比如富文本编辑器、上传图片等。 6. 在Form组件中,可以监听表单元素的值变化,并将变化的值更新到表单数据对象中。 7. 可以在Form组件中添加提交按钮,并在点击按钮时触发表单提交事件,将表单数据对象作为参数传递给父组件。 需要注意的是,这只是一个基本的封装思路,如果需要使用到更多的业务场景,可以根据具体需求进行扩展和定制。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* *2* [vue3 ts element plus form表单二次封装详细步骤 (附参数、类型详细介绍及简单使用示例)](https://blog.csdn.net/weixin_45291937/article/details/126275955)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值