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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
| <template>
| <div class="scope">
| <div class="h">
| <el-tag size="small" effect="dark" disable-transitions>options</el-tag>
| <span>选项框配置</span>
| </div>
|
| <div class="c">
| <el-button @click="open">预览</el-button>
| <demo-code :files="['form/options.vue']" />
|
| <!-- 自定义表单组件 -->
| <cl-form ref="Form"></cl-form>
| </div>
|
| <div class="f">
| <span class="date">2024-01-01</span>
| </div>
| </div>
| </template>
|
| <script setup lang="ts">
| import { useForm } from '@cool-vue/crud';
| import { computed, reactive } from 'vue';
|
| const Form = useForm();
|
| // 觉得麻烦就 any,如 { user: [] as any[] }
| const options = reactive<{ [key: string]: { label: string; value: any }[] }>({
| user: []
| });
|
| function open() {
| Form.value?.open({
| title: '选项框配置',
| items: [
| {
| label: '下拉框',
| prop: 'select',
| component: {
| name: 'el-select',
| props: {
| clearable: true // 可清除
| },
| options: [
| {
| label: 'javascript',
| value: 1
| },
| {
| label: 'vue',
| value: 2
| },
| {
| label: 'html',
| value: 3
| },
| {
| label: 'css',
| value: 4
| }
| ]
| }
| },
| {
| label: '单选框',
| prop: 'radio',
| value: 1,
| component: {
| name: 'el-radio-group',
| options: [
| {
| label: '手机',
| value: 1
| },
| {
| label: '电脑',
| value: 2
| },
| {
| label: '电视',
| value: 3
| }
| ]
| }
| },
| {
| label: '多选框',
| prop: 'checkbox',
| value: [2, 3],
| component: {
| name: 'el-checkbox-group',
| options: [
| {
| label: '咖啡',
| value: 1
| },
| {
| label: '汉堡',
| value: 2
| },
| {
| label: '炸鸡',
| value: 3
| },
| {
| label: '奶茶',
| value: 4
| }
| ]
| }
| },
| {
| label: '动态配置1',
| prop: 'd1',
| component: {
| name: 'el-select',
| // 动态设置方法1,在 on.open 事件配置 options
| options: []
| }
| },
| {
| label: '动态配置2',
| prop: 'd2',
| component: {
| name: 'el-select',
| // 动态设置方法2,使用 computed 更新 options
| options: computed(() => options.user)
| }
| }
| ],
| on: {
| open() {
| // 模拟 1.5s 后取的数据
| setTimeout(() => {
| // 动态设置方法1,使用 setOptions 方法设置
| // d1 为 prop 值
| Form.value?.setOptions('d1', [
| {
| label: '😊',
| value: 1
| },
| {
| label: '😭',
| value: 2
| },
| {
| label: '😘',
| value: 3
| }
| ]);
|
| // 动态设置方法2,直接设置 options.user,由 computed 更新
| options.user = [
| {
| label: '💰',
| value: 1
| },
| {
| label: '🚗',
| value: 2
| }
| ];
| }, 1500);
| },
| submit(data, { close }) {
| close();
| }
| }
| });
| }
| </script>
|
|