行业知识
基于element-plus的Dialog选择控件
2026-11-18 18:23:00
来源:互联网转载
���看之前工程师写的vue2的代码,很多都是复制、粘贴,也真是搞不懂,明明可以写一个控件,不就可以重复使用。很多前端总喜欢element搞一下,ant-design也搞一下,有啥意义,控件也不是自己写的,积木也没有搭好。
选中之后将值带回去
先看看组件代码
{{ node.label }} 返 回 import {ref,getCurrentInstance,computed,onMounted,watch } from 'vue'import BaseDialog from '@/components/base/BaseDialog.vue';import { useAppStore } from '@/store'import { de } from 'element-plus/es/locale';const { proxy }: any = getCurrentInstance();const appStore = useAppStore()const userInfo = computed(() => appStore.userInfo)const curAccountSet = computed(() => appStore.curAccountSet)const defaultSubjecTreeProps = ref({children: 'children',label: 'label'})const inputProps = ref({label:'name',value:'id'})const inputLabel = ref('')const emits = defineEmits(['update:modelValue','change'])const visible = ref(false)const props = defineProps modelValue:{},}()const showSub = () =>{ visible.value = true;}const subjectName = ref(1)const subjectTabs = ref(curAccountSet.value?.accountingStandard=='1'?[ {title: "资产", name: 1,}, {title: "负债", name: 2,}, {title: "权益", name: 3,}, {title: "成本", name: 4,}, {title: "损益", name: 5,},]:[ {title: "资产", name: 1,}, {title: "负债", name: 2,}, {title: "共同", name: 3,}, {title: "权益", name: 4,}, {title: "成本", name: 5,}, {title: "损益", name: 6,},])const categoryTree = ref([])const getCategoryTree = async (cate) =>{ const res = await proxy.$api.acc.accountsubject.list({asId:curAccountSet.value.accId,category:cate}); if (res.success){ categoryTree.value[cate - 1] = res.data; } else{ proxy.$message.error(res.msg); }}const handleSubjectClick = (data) => { if (proxy.$_.isEmpty(data.children)){ inputLabel.value = data.name emits('update:modelValue',{value:data.id,label:data.name}) visible.value = false }}const cancelDialog = () =>{ visible.value = false;}watch(()=>props.modelValue,(newVal,oldVal)=>{ if (!proxy.$_.isEmpty(newVal)){ inputLabel.value = newVal['label'] }},{immediate:true,deep:true})onMounted(()=>{ getCategoryTree(1); getCategoryTree(2); getCategoryTree(3); getCategoryTree(4); getCategoryTree(5);}) 控件使用比较简单了
但是在保存和加载的时候需要特殊处理,因为控件的值是{value:data.id,label:data.name}
保存之前,需要做一次深拷贝,不能直接修改orderForm.value,因为双向绑定,页面的数据会改变。
const convertParams = () =>{ debugger let params = proxy.$tool.deepClone({...orderForm.value}) // 应收账款 params['yszk'] = orderForm.value['yszk']?.value || '' params['yszkName'] = orderForm.value['yszk']?.label || '' return params;}const save = async () => { orderRef.value?.validate(async valid=>{ if (valid){ isLoading.value = true const params = convertParams() const res = await proxy.$api.setting.psiAccConfig.save(params) if (res.success){ isLoading.value = false; proxy.$message.success(res.msg); } else{ isLoading.value = false; proxy.$message.error(res.msg); } } })} 页面加载的时候也需要做一下处理,还是因为数据结构的缘故
const load = async () => { const res = await proxy.$api.setting.psiAccConfig.load({asId:userInfo.value.currentAsId}) if (res.success){ isLoading.value = false; orderForm.value = res.data; // 应收张狂 if (orderForm.value['yszkName']){ orderForm.value['yszk'] = {label:orderForm.value['yszkName'],value:orderForm.value['yszk']} } } } else{ isLoading.value = false; proxy.$message.error(res.msg); }}
上一篇:法然上人,神秘传奇一生的揭秘
下一篇:服务器攻击事件怎么解决?