@@ -199,25 +199,9 @@ export default {
199199 const dict = ` ${ genList[i].model } dict` ;
200200 this .models [dict] = this .value [dict];
201201 }
202- this .formValueToArray (genList[i]);
202+ this .initFormValue (genList[i]);
203203 } 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]);
221205 }
222206 if (this .rules [genList[i].model ]) {
223207 this .rules [genList[i].model ] = [
@@ -233,13 +217,16 @@ export default {
233217
234218 /**
235219 * 如果select,radio,checkbox等组件为多选情况 后台返回逗号分隔字符串 => 数组
220+ * 如果 this.value为null 则会按默认值赋值相应字段
236221 * @param {String} 当前表单json.list
237222 */
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' ) {
240225 if (this .value [row .model ] != null && this .value [row .model ] !== ' ' ) {
241226 this .models [row .model ] = this .value [row .model ].split (' ,' );
242227 }
228+ } else if (this .value [row .model ] == null ) {
229+ this .setDefaultValue (row);
243230 } else {
244231 this .models [row .model ] = this .value [row .model ];
245232 }
@@ -254,13 +241,30 @@ export default {
254241 });
255242 return model;
256243 },
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+ },
257259 // 先验证再获取表单内容
258260 getData (args ) {
259261 return new Promise ((resolve , reject ) => {
260262 this .$refs .generateForm .validate ((valid ) => {
261263 if (valid) {
262264 resolve (this .formValueToString (), args);
263265 } else {
266+ // 校验失败时focus到文本框
267+ // 注意此处没有考虑textarea的情况,多行文本会失败
264268 setTimeout (() => {
265269 const isError = document .getElementsByClassName (' is-error' );
266270 isError[0 ].querySelector (' input' ).focus ();
0 commit comments