Browse Source

基础数据新增编辑form组件

master
陈薪名 1 year ago
parent
commit
0cb3e88753
  1. 154
      src/components/BasicForm/src/BasicForm.vue

154
src/components/BasicForm/src/BasicForm.vue

@ -0,0 +1,154 @@
<template>
<Dialog :title="dialogTitle" v-model="dialogVisible">
<Form ref="formRef" v-loading="formLoading" :rules="rules" :schema="formSchema" :is-col="true" />
<template #footer>
<ButtonBase :Butttondata="Butttondata" @button-base-click="buttonBaseClick" />
</template>
</Dialog>
<SearchTable ref="searchTableRef" :dialogTitle="searchTableTitle" />
</template>
<script setup lang="ts">
import { SearchTable } from '@/components/SearchTable';
import * as defaultButtons from '@/utils/disposition/defaultButtons'
import ButtonBase from '@/components/XButton/src/ButtonBase.vue'
const props = defineProps({
// rules
rules: {
type: Object,
required: true,
default: null
},
//
formAllSchemas: {
type: Object,
required: true,
default: null
},
//
searchTableTitle: {
type: String,
required: false,
default:'查询'
},
// Schemas
searchTableAllSchemas: {
type: Object,
required: true,
default: null
},
// APIpagelist
searchTablePage: {
type: Object,
required: true,
default: null
},
// APIVo
apiVo: {
type: Object,
required: true,
default: null
},
// API
apiCreate: {
type: Function,
required: true,
default: null
},
// API
apiUpdate: {
type: Function,
required: true,
default: null
}
})
const { t } = useI18n() //
const message = useMessage() //
const dialogVisible = ref(false) //
const dialogTitle = ref('') //
const formLoading = ref(false) // 12
const formType = ref('') // create - update -
const formRef = ref() // Ref
const formSchema = ref(props.formAllSchemas.formSchema)
/** 弹层操作 */
const searchTableRef = ref()
const opensearchTable = () => {
searchTableRef.value.open('物品基础信息', props.searchTableAllSchemas, props.searchTablePage)
}
/** 打开弹窗 */
const open = async (type: string, row?: any) => {
dialogVisible.value = true
dialogTitle.value = t('action.' + type)
formType.value = type
resetForm()
//
if (row) {
formLoading.value = true
try {
nextTick(() => {
formRef.value.setValues(row)
})
} finally {
formLoading.value = false
}
}
}
defineExpose({ open }) // open
/** 弹窗按钮 */
const Butttondata = [
defaultButtons.formSaveBtn(null), //
defaultButtons.formCloseBtn(null), //
]
/** 按钮事件 */
const buttonBaseClick = (val) => {
//
if (val == 'save') {
submitForm()
}
//
else if (val == 'close') {
dialogVisible.value = false
}
}
/** 提交表单 */
const emit = defineEmits(['success']) // success
const submitForm = async () => {
const elForm = unref(formRef)?.getElFormRef()
//
if (!elForm) return
const valid = await elForm.validate()
if (!valid) return
//
formLoading.value = true
try {
// const data = unref(formRef)?.formModel as ItempackagingApi.ItempackagingVO
// TODO: as VO props
const data = unref(formRef)?.formModel
if (formType.value === 'create') {
await props.apiCreate(data)
message.success(t('common.createSuccess'))
} else {
await props.apiUpdate(data)
message.success(t('common.updateSuccess'))
}
dialogVisible.value = false
//
emit('success')
} finally {
formLoading.value = false
}
}
/** 重置表单 */
const resetForm = () => {
unref(formRef)?.resetFields()
}
</script>
Loading…
Cancel
Save