提交 | 用户 | 时间
|
e7c126
|
1 |
#set ($subColumns = $subColumnsList.get($subIndex))##当前字段数组 |
H |
2 |
#set ($subSimpleClassName = $subSimpleClassNames.get($subIndex)) |
|
3 |
#set ($subJoinColumn = $subJoinColumns.get($subIndex))##当前 join 字段 |
|
4 |
<template> |
|
5 |
<Dialog :title="dialogTitle" v-model="dialogVisible"> |
|
6 |
<el-form |
|
7 |
ref="formRef" |
|
8 |
:model="formData" |
|
9 |
:rules="formRules" |
|
10 |
label-width="100px" |
|
11 |
v-loading="formLoading" |
|
12 |
> |
|
13 |
#foreach($column in $subColumns) |
|
14 |
#if ($column.createOperation || $column.updateOperation) |
|
15 |
#set ($dictType = $column.dictType) |
|
16 |
#set ($javaField = $column.javaField) |
|
17 |
#set ($javaType = $column.javaType) |
|
18 |
#set ($AttrName = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)}) |
|
19 |
#set ($comment = $column.columnComment) |
|
20 |
#set ($dictMethod = "getDictOptions")## 计算使用哪个 dict 字典方法 |
|
21 |
#if ($javaType == "Integer" || $javaType == "Long" || $javaType == "Byte" || $javaType == "Short") |
|
22 |
#set ($dictMethod = "getIntDictOptions") |
|
23 |
#elseif ($javaType == "String") |
|
24 |
#set ($dictMethod = "getStrDictOptions") |
|
25 |
#elseif ($javaType == "Boolean") |
|
26 |
#set ($dictMethod = "getBoolDictOptions") |
|
27 |
#end |
|
28 |
#if ( $column.id == $subJoinColumn.id) ## 特殊:忽略主子表的 join 字段,不用填写 |
|
29 |
#elseif ($column.htmlType == "input" && !$column.primaryKey)## 忽略主键,不用在表单里 |
|
30 |
<el-form-item label="${comment}" prop="${javaField}"> |
|
31 |
<el-input v-model="formData.${javaField}" placeholder="请输入${comment}" /> |
|
32 |
</el-form-item> |
|
33 |
#elseif($column.htmlType == "imageUpload")## 图片上传 |
|
34 |
<el-form-item label="${comment}" prop="${javaField}"> |
|
35 |
<UploadImg v-model="formData.${javaField}" /> |
|
36 |
</el-form-item> |
|
37 |
#elseif($column.htmlType == "fileUpload")## 文件上传 |
|
38 |
<el-form-item label="${comment}" prop="${javaField}"> |
|
39 |
<UploadFile v-model="formData.${javaField}" /> |
|
40 |
</el-form-item> |
|
41 |
#elseif($column.htmlType == "editor")## 文本编辑器 |
|
42 |
<el-form-item label="${comment}" prop="${javaField}"> |
|
43 |
<Editor v-model="formData.${javaField}" height="150px" /> |
|
44 |
</el-form-item> |
|
45 |
#elseif($column.htmlType == "select")## 下拉框 |
|
46 |
<el-form-item label="${comment}" prop="${javaField}"> |
|
47 |
<el-select v-model="formData.${javaField}" placeholder="请选择${comment}"> |
|
48 |
#if ("" != $dictType)## 有数据字典 |
|
49 |
<el-option |
|
50 |
v-for="dict in $dictMethod(DICT_TYPE.$dictType.toUpperCase())" |
|
51 |
:key="dict.value" |
|
52 |
:label="dict.label" |
|
53 |
:value="dict.value" |
|
54 |
/> |
|
55 |
#else##没数据字典 |
|
56 |
<el-option label="请选择字典生成" value="" /> |
|
57 |
#end |
|
58 |
</el-select> |
|
59 |
</el-form-item> |
|
60 |
#elseif($column.htmlType == "checkbox")## 多选框 |
|
61 |
<el-form-item label="${comment}" prop="${javaField}"> |
|
62 |
<el-checkbox-group v-model="formData.${javaField}"> |
|
63 |
#if ("" != $dictType)## 有数据字典 |
|
64 |
<el-checkbox |
|
65 |
v-for="dict in $dictMethod(DICT_TYPE.$dictType.toUpperCase())" |
|
66 |
:key="dict.value" |
|
67 |
:label="dict.value" |
|
68 |
> |
|
69 |
{{ dict.label }} |
|
70 |
</el-checkbox> |
|
71 |
#else##没数据字典 |
|
72 |
<el-checkbox>请选择字典生成</el-checkbox> |
|
73 |
#end |
|
74 |
</el-checkbox-group> |
|
75 |
</el-form-item> |
|
76 |
#elseif($column.htmlType == "radio")## 单选框 |
|
77 |
<el-form-item label="${comment}" prop="${javaField}"> |
|
78 |
<el-radio-group v-model="formData.${javaField}"> |
|
79 |
#if ("" != $dictType)## 有数据字典 |
|
80 |
<el-radio |
|
81 |
v-for="dict in $dictMethod(DICT_TYPE.$dictType.toUpperCase())" |
|
82 |
:key="dict.value" |
|
83 |
:label="dict.value" |
|
84 |
> |
|
85 |
{{ dict.label }} |
|
86 |
</el-radio> |
|
87 |
#else##没数据字典 |
|
88 |
<el-radio label="1">请选择字典生成</el-radio> |
|
89 |
#end |
|
90 |
</el-radio-group> |
|
91 |
</el-form-item> |
|
92 |
#elseif($column.htmlType == "datetime")## 时间框 |
|
93 |
<el-form-item label="${comment}" prop="${javaField}"> |
|
94 |
<el-date-picker |
|
95 |
v-model="formData.${javaField}" |
|
96 |
type="date" |
|
97 |
value-format="x" |
|
98 |
placeholder="选择${comment}" |
|
99 |
/> |
|
100 |
</el-form-item> |
|
101 |
#elseif($column.htmlType == "textarea")## 文本框 |
|
102 |
<el-form-item label="${comment}" prop="${javaField}"> |
|
103 |
<el-input v-model="formData.${javaField}" type="textarea" placeholder="请输入${comment}" /> |
|
104 |
</el-form-item> |
|
105 |
#end |
|
106 |
#end |
|
107 |
#end |
|
108 |
</el-form> |
|
109 |
<template #footer> |
|
110 |
<el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button> |
|
111 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
112 |
</template> |
|
113 |
</Dialog> |
|
114 |
</template> |
|
115 |
<script setup lang="ts"> |
|
116 |
import { getIntDictOptions, getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict' |
|
117 |
import { ${simpleClassName}Api } from '@/api/${table.moduleName}/${table.businessName}' |
|
118 |
|
|
119 |
const { t } = useI18n() // 国际化 |
|
120 |
const message = useMessage() // 消息弹窗 |
|
121 |
|
|
122 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
123 |
const dialogTitle = ref('') // 弹窗的标题 |
|
124 |
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
125 |
const formType = ref('') // 表单的类型:create - 新增;update - 修改 |
|
126 |
const formData = ref({ |
|
127 |
#foreach ($column in $subColumns) |
|
128 |
#if ($column.createOperation || $column.updateOperation) |
|
129 |
#if ($column.htmlType == "checkbox") |
|
130 |
$column.javaField: [], |
|
131 |
#else |
|
132 |
$column.javaField: undefined, |
|
133 |
#end |
|
134 |
#end |
|
135 |
#end |
|
136 |
}) |
|
137 |
const formRules = reactive({ |
|
138 |
#foreach ($column in $subColumns) |
|
139 |
#if (($column.createOperation || $column.updateOperation) && !$column.nullable && !${column.primaryKey})## 创建或者更新操作 && 要求非空 && 非主键 |
|
140 |
#set($comment=$column.columnComment) |
|
141 |
$column.javaField: [{ required: true, message: '${comment}不能为空', trigger: #if($column.htmlType == 'select')'change'#else'blur'#end }], |
|
142 |
#end |
|
143 |
#end |
|
144 |
}) |
|
145 |
const formRef = ref() // 表单 Ref |
|
146 |
|
|
147 |
/** 打开弹窗 */ |
|
148 |
const open = async (type: string, id?: number, ${subJoinColumn.javaField}: number) => { |
|
149 |
dialogVisible.value = true |
|
150 |
dialogTitle.value = t('action.' + type) |
|
151 |
formType.value = type |
|
152 |
resetForm() |
|
153 |
formData.value.${subJoinColumn.javaField} = ${subJoinColumn.javaField} |
|
154 |
// 修改时,设置数据 |
|
155 |
if (id) { |
|
156 |
formLoading.value = true |
|
157 |
try { |
|
158 |
formData.value = await ${simpleClassName}Api.get${subSimpleClassName}(id) |
|
159 |
} finally { |
|
160 |
formLoading.value = false |
|
161 |
} |
|
162 |
} |
|
163 |
} |
|
164 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
165 |
|
|
166 |
/** 提交表单 */ |
|
167 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
168 |
const submitForm = async () => { |
|
169 |
// 校验表单 |
|
170 |
await formRef.value.validate() |
|
171 |
// 提交请求 |
|
172 |
formLoading.value = true |
|
173 |
try { |
|
174 |
const data = formData.value |
|
175 |
if (formType.value === 'create') { |
|
176 |
await ${simpleClassName}Api.create${subSimpleClassName}(data) |
|
177 |
message.success(t('common.createSuccess')) |
|
178 |
} else { |
|
179 |
await ${simpleClassName}Api.update${subSimpleClassName}(data) |
|
180 |
message.success(t('common.updateSuccess')) |
|
181 |
} |
|
182 |
dialogVisible.value = false |
|
183 |
// 发送操作成功的事件 |
|
184 |
emit('success') |
|
185 |
} finally { |
|
186 |
formLoading.value = false |
|
187 |
} |
|
188 |
} |
|
189 |
|
|
190 |
/** 重置表单 */ |
|
191 |
const resetForm = () => { |
|
192 |
formData.value = { |
|
193 |
#foreach ($column in $subColumns) |
|
194 |
#if ($column.createOperation || $column.updateOperation) |
|
195 |
#if ($column.htmlType == "checkbox") |
|
196 |
$column.javaField: [], |
|
197 |
#else |
|
198 |
$column.javaField: undefined, |
|
199 |
#end |
|
200 |
#end |
|
201 |
#end |
|
202 |
} |
|
203 |
formRef.value?.resetFields() |
|
204 |
} |
|
205 |
</script> |