@@ -199,25 +199,9 @@ export default {
199
199
const dict = ` ${ genList[i].model } dict` ;
200
200
this .models [dict] = this .value [dict];
201
201
}
202
- this .formValueToArray (genList[i]);
202
+ this .initFormValue (genList[i]);
203
203
} else {
204
- const config = genList[i];
205
- // 如果时间选择器需要默认值,默认回填当前日期
206
- if (config .type === ' date' && config .options .defaultValue ) {
207
- this .models [genList[i].model ] = DateTimeNowSplit ();
208
- } else {
209
- let { defaultValue } = genList[i].options ;
210
- // 如果默认值设置为$开头,则表示要读取vuex中的全局变量
211
- // 如设置为 $deptname 则读取 this.$store.getters.deptname
212
- if (typeof (defaultValue) === ' string' && defaultValue .includes (' $' )) {
213
- defaultValue = this .$store .getters [defaultValue .replace (' $' , ' ' )];
214
- }
215
- this .models [genList[i].model ] = typeof (defaultValue) === ' boolean' ? ' ' : defaultValue;
216
- }
217
- // 多选下拉框初始化需要为Array,故此处单独处理
218
- if (genList[i].type === ' checkbox' ) {
219
- this .models [genList[i].model ] = [];
220
- }
204
+ this .setDefaultValue (genList[i]);
221
205
}
222
206
if (this .rules [genList[i].model ]) {
223
207
this .rules [genList[i].model ] = [
@@ -233,13 +217,16 @@ export default {
233
217
234
218
/**
235
219
* 如果select,radio,checkbox等组件为多选情况 后台返回逗号分隔字符串 => 数组
220
+ * 如果 this.value为null 则会按默认值赋值相应字段
236
221
* @param {String} 当前表单json.list
237
222
*/
238
- formValueToArray (row ) {
239
- if (row .options .multiple || row .type === ' cascader' || row . type === ' checkbox ' ) {
223
+ initFormValue (row ) {
224
+ if (row .options .multiple || row .type === ' cascader' ) {
240
225
if (this .value [row .model ] != null && this .value [row .model ] !== ' ' ) {
241
226
this .models [row .model ] = this .value [row .model ].split (' ,' );
242
227
}
228
+ } else if (this .value [row .model ] == null ) {
229
+ this .setDefaultValue (row);
243
230
} else {
244
231
this .models [row .model ] = this .value [row .model ];
245
232
}
@@ -254,13 +241,30 @@ export default {
254
241
});
255
242
return model;
256
243
},
244
+ // 表单默认值回填单独拉出来封装
245
+ setDefaultValue (config ) {
246
+ // 如果时间选择器需要默认值,默认回填当前日期
247
+ if (config .type === ' date' && config .options .defaultValue ) {
248
+ this .models [config .model ] = DateTimeNowSplit ();
249
+ } else {
250
+ let { defaultValue } = config .options ;
251
+ // 如果默认值设置为$开头,则表示要读取vuex中的全局变量
252
+ // 如设置为 $deptname 则读取 this.$store.getters.deptname
253
+ if (typeof (defaultValue) === ' string' && defaultValue .includes (' $' )) {
254
+ defaultValue = this .$store .getters [defaultValue .replace (' $' , ' ' )];
255
+ }
256
+ this .models [config .model ] = typeof (defaultValue) === ' boolean' ? ' ' : defaultValue;
257
+ }
258
+ },
257
259
// 先验证再获取表单内容
258
260
getData (args ) {
259
261
return new Promise ((resolve , reject ) => {
260
262
this .$refs .generateForm .validate ((valid ) => {
261
263
if (valid) {
262
264
resolve (this .formValueToString (), args);
263
265
} else {
266
+ // 校验失败时focus到文本框
267
+ // 注意此处没有考虑textarea的情况,多行文本会失败
264
268
setTimeout (() => {
265
269
const isError = document .getElementsByClassName (' is-error' );
266
270
isError[0 ].querySelector (' input' ).focus ();
0 commit comments