Files
mayfly-go/frontend/src/views/ops/db/component/table/DbTableDataForm.vue

117 lines
3.5 KiB
Vue
Raw Normal View History

2024-01-31 20:41:41 +08:00
<template>
<el-dialog v-model="visible" :title="title" :destroy-on-close="true" width="600px">
<el-form ref="dataForm" :model="modelValue" :show-message="false" label-width="auto" size="small">
<el-form-item
v-for="column in columns"
:key="column.columnName"
class="w100 mb5"
:prop="column.columnName"
2025-02-20 17:07:13 +08:00
:required="props.tableName != '' && !column.nullable && !column.isPrimaryKey && !column.autoIncrement"
2024-01-31 20:41:41 +08:00
>
<template #label>
2024-07-19 17:06:11 +08:00
<span class="pointer" :title="column?.columnComment ? `${column.columnType} | ${column.columnComment}` : column.columnType">
2024-02-01 12:05:41 +08:00
{{ column.columnName }}
</span>
2024-01-31 20:41:41 +08:00
</template>
<ColumnFormItem
v-model="modelValue[`${column.columnName}`]"
:data-type="dbInst.getDialect().getDataType(column.dataType)"
2024-07-19 17:06:11 +08:00
:placeholder="column?.columnComment ? `${column.columnType} | ${column.columnComment}` : column.columnType"
2024-01-31 20:41:41 +08:00
:column-name="column.columnName"
2025-02-20 17:07:13 +08:00
:disabled="column.autoIncrement"
2024-01-31 20:41:41 +08:00
/>
</el-form-item>
</el-form>
2024-07-19 17:06:11 +08:00
<template #footer v-if="props.tableName">
2024-01-31 20:41:41 +08:00
<span class="dialog-footer">
2024-11-20 22:43:53 +08:00
<el-button @click="closeDialog">{{ $t('common.cancel') }}</el-button>
<el-button type="primary" @click="confirm">{{ $t('common.confirm') }}</el-button>
2024-01-31 20:41:41 +08:00
</span>
</template>
</el-dialog>
</template>
<script lang="ts" setup>
2024-02-01 12:05:41 +08:00
import { ref, watch, onMounted } from 'vue';
2024-01-31 20:41:41 +08:00
import ColumnFormItem from './ColumnFormItem.vue';
import { DbInst } from '../../db';
2024-11-20 22:43:53 +08:00
import { useI18nFormValidate } from '@/hooks/useI18n';
2024-01-31 20:41:41 +08:00
export interface ColumnFormItemProps {
dbInst: DbInst;
dbName: string;
tableName: string;
columns: any[];
title?: string; // dialog title
}
const props = withDefaults(defineProps<ColumnFormItemProps>(), {
title: '',
});
const modelValue = defineModel<any>('modelValue');
const visible = defineModel<boolean>('visible', {
default: false,
});
const emit = defineEmits(['submitSuccess']);
const dataForm: any = ref(null);
let oldValue = null as any;
2024-02-01 12:05:41 +08:00
onMounted(() => {
setOldValue();
});
2024-01-31 20:41:41 +08:00
watch(visible, (newValue) => {
2024-02-01 12:05:41 +08:00
if (newValue) {
setOldValue();
2024-01-31 20:41:41 +08:00
}
});
2024-02-01 12:05:41 +08:00
const setOldValue = () => {
// 空对象则为insert操作否则为update
if (Object.keys(modelValue.value).length > 0) {
oldValue = Object.assign({}, modelValue.value);
}
};
2024-01-31 20:41:41 +08:00
const closeDialog = () => {
visible.value = false;
modelValue.value = {};
};
const confirm = async () => {
2024-11-20 22:43:53 +08:00
await useI18nFormValidate(dataForm);
2024-01-31 20:41:41 +08:00
const dbInst = props.dbInst;
const data = modelValue.value;
const db = props.dbName;
const tableName = props.tableName;
2024-01-31 20:41:41 +08:00
let sql = '';
if (oldValue) {
2024-07-19 17:06:11 +08:00
const updateColumnValue: any = {};
Object.keys(oldValue).forEach((key) => {
// 如果新旧值不相等,则为需要更新的字段
if (oldValue[key] !== modelValue.value[key]) {
updateColumnValue[key] = modelValue.value[key];
}
2024-01-31 20:41:41 +08:00
});
sql = await dbInst.genUpdateSql(db, tableName, updateColumnValue, oldValue);
} else {
sql = await dbInst.genInsertSql(db, tableName, [data], true);
}
dbInst.promptExeSql(db, sql, null, () => {
closeDialog();
emit('submitSuccess');
2024-01-31 20:41:41 +08:00
});
};
</script>
<style lang="scss"></style>