# 更新模型 update-model
<template>
<div class="demo-box">
<form-builder ref="formBuilder" v-model="formModel" :form-data="formData">
<template slot="actions">
<el-form-item>
<pre style="background: none;">{{ formModel }}</pre>
</el-form-item>
<el-button type="primary" @click="updateModel1">更新模型(直接更新formModel)</el-button>
<el-button type="primary" @click="updateModel2">更新模型(调用方法updateModel)</el-button>
</template>
</form-builder>
</div>
</template>
<script>
export default {
data() {
return {
formModel: {
name: '',
area: '',
date: '',
switch: '',
multi: [],
single: '',
description: ''
},
formData: {
config: {
labelWidth: '50px'
},
list: [{
type: 'input',
model: 'name',
label: '名字',
attrs: {
placeholder: '请填写名字',
clearable: true
}
}, {
type: 'select',
model: 'area',
label: '区域',
options: [{
value: 'AREA1',
label: '区域一'
}, {
value: 'AREA2',
label: '区域二'
}]
}, {
type: 'date-picker',
model: 'date',
label: '日期',
attrs: {
type: 'date',
placeholder: '请选择日期'
}
}, {
type: 'switch',
model: 'switch',
label: '开关'
}, {
type: 'checkbox-group',
model: 'multi',
label: '多选',
options: [{
label: '多选A'
}, {
label: '多选B'
}, {
label: '多选C'
}]
}, {
type: 'radio-group',
model: 'single',
label: '单选',
options: [{
value: 'A',
label: '单选A'
}, {
value: 'B',
label: '单选B'
}]
}, {
type: 'input',
label: '描述',
model: 'description',
attrs: {
type: 'textarea',
rows: 3
}
}]
}
}
},
methods: {
updateModel1() {
this.formModel = {
name: 'TyroCCC',
area: 'AREA1',
date: '2020-5-20',
switch: true,
multi: ['多选B', '多选C'],
single: 'A',
description: '你好,世界!'
}
},
updateModel2() {
this.$refs.formBuilder.updateModel({
name: 'Nash',
area: 'AREA2',
date: '2010-10-10',
switch: false,
multi: ['多选A'],
single: 'B',
description: 'Hello World!'
})
},
}
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123