支持 mb-form getDetail 和 formData共用

This commit is contained in:
吕金泽 2022-08-05 19:03:24 +08:00
parent 24ca788032
commit 6384bebdc3

View File

@ -13,6 +13,7 @@
v-model="formData[col.name]" v-model="formData[col.name]"
:item-label="col.label" :item-label="col.label"
v-bind="col.props" v-bind="col.props"
@change="col.change"
/> />
</el-form-item> </el-form-item>
</el-col> </el-col>
@ -21,10 +22,10 @@
</template> </template>
<script setup> <script setup>
import {ref, reactive, getCurrentInstance } from 'vue' import { ref, reactive, getCurrentInstance, watch } from 'vue'
const { proxy } = getCurrentInstance() const { proxy } = getCurrentInstance()
const rules = reactive(getRules()) const rules = reactive(getRules())
const formData = ref(initFormData()) const formData = ref({})
const dataForm = ref() const dataForm = ref()
const props = defineProps({ const props = defineProps({
form: { form: {
@ -41,6 +42,9 @@
} }
}) })
const emit = defineEmits(['reload']) const emit = defineEmits(['reload'])
watch(() => props.detail.formData, (value) => {
Object.assign(formData.value, value)
},{ deep: true })
props.form.props = props.form.props || {} props.form.props = props.form.props || {}
proxy.$common.setDefaultValue(props.form.props, 'labelPosition', 'right') proxy.$common.setDefaultValue(props.form.props, 'labelPosition', 'right')
@ -106,14 +110,21 @@
} }
function getDetail(id) { function getDetail(id) {
formData.value[props.primaryField] = id formData.value = props.detail.formData || {}
var detailData = initFormData()
detailData[props.primaryField] = id
proxy.$get(props.detail.request.url, { [props.primaryField]: id }).then(res => { proxy.$get(props.detail.request.url, { [props.primaryField]: id }).then(res => {
const { data } = res const { data } = res
for (var t in formData.value) { for (var t in detailData) {
if (data[t] && (!props.detail.excludeAssign || props.detail.excludeAssign.indexOf(t) === -1)) { if ((data[t] || data[t] === 0) && (!props.detail.excludeAssign || props.detail.excludeAssign.indexOf(t) === -1)) {
formData.value[t] = data[t] detailData[t] = data[t]
} }
} }
if(formData.value){
formData.value = Object.assign(detailData, formData.value)
} else {
formData.value = detailData
}
}) })
} }