wangzhibo
2026-07-16 b57020623cf0c946706573bf102e548c3a544423
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
<template>
    <div class="scope">
        <div class="h">
            <el-tag size="small" effect="dark" disable-transitions>all</el-tag>
            <span>完整示例</span>
        </div>
 
        <div class="c">
            <el-button @click="open">预览</el-button>
            <demo-code :files="['crud/all.vue']" />
 
            <cl-dialog v-model="visible" title="完整示例" width="80%">
                <cl-crud ref="Crud">
                    <cl-row>
                        <!-- 刷新按钮 -->
                        <cl-refresh-btn />
 
                        <!-- 新增按钮 -->
                        <cl-add-btn />
 
                        <!-- 批量删除按钮 -->
                        <cl-multi-delete-btn />
 
                        <!-- 筛选 -->
                        <cl-filter label="状态筛选">
                            <!-- 配置prop,选择后会自动过滤列表 -->
                            <cl-select :options="options.status" prop="status" :width="120" />
                        </cl-filter>
 
                        <!-- 字典 -->
                        <cl-filter label="工作(字典)">
                            <cl-select
                                tree
                                :options="dict.get('occupation')"
                                prop="occupation"
                                :width="140"
                                check-strictly
                            />
                        </cl-filter>
 
                        <cl-flex1 />
 
                        <!-- 导入 -->
                        <cl-import-btn template="/用户导入模版.xlsx" />
 
                        <!-- 导出 -->
                        <cl-export-btn :columns="Table?.columns" />
 
                        <!-- 自定义列 -->
                        <cl-column-custom
                            :ref="setRefs('columnCustom')"
                            :columns="Table?.columns"
                        />
 
                        <!-- 关键字搜索 -->
                        <cl-search-key placeholder="搜索姓名、手机号" :width="250" />
 
                        <!-- 高级搜索按钮 -->
                        <cl-adv-btn />
                    </cl-row>
 
                    <cl-row>
                        <!-- 表格 -->
                        <cl-table
                            ref="Table"
                            show-summary
                            :summary-method="onSummaryMethod"
                            :auto-height="false"
                        >
                            <!-- 展开信息 -->
                            <template #column-detail="{ scope }">
                                <div style="padding: 0 10px">
                                    <el-descriptions border :column="3">
                                        <el-descriptions-item label="ID">
                                            {{ scope.row.id }}
                                        </el-descriptions-item>
 
                                        <el-descriptions-item label="姓名">
                                            {{ scope.row.name }}
                                        </el-descriptions-item>
 
                                        <el-descriptions-item label="存款">
                                            {{ scope.row.wages }}
                                        </el-descriptions-item>
 
                                        <el-descriptions-item label="出生年月">
                                            {{ scope.row.createTime }}
                                        </el-descriptions-item>
                                    </el-descriptions>
                                </div>
                            </template>
 
                            <!-- 自定义列 -->
                            <template #column-wages="{ scope }">
                                <span>{{ scope.row.wages }}🤑</span>
                            </template>
                        </cl-table>
                    </cl-row>
 
                    <cl-row>
                        <cl-flex1 />
 
                        <!-- 分页 -->
                        <cl-pagination />
                    </cl-row>
 
                    <!-- 新增、编辑 -->
                    <cl-upsert ref="Upsert" />
 
                    <!-- 高级搜索 -->
                    <cl-adv-search ref="AdvSearch" />
                </cl-crud>
            </cl-dialog>
        </div>
 
        <div class="f">
            <span class="date">2024-01-01</span>
        </div>
    </div>
</template>
 
<script lang="tsx" setup>
defineOptions({
    name: 'demo-crud'
});
 
import { useCrud, useUpsert, useTable, useAdvSearch, useSearch } from '@cool-vue/crud';
import { useDict } from '/$/dict';
import { reactive, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { useCool } from '/@/cool';
 
// 基础
const { service, refs, setRefs } = useCool();
 
// 字典
const { dict } = useDict();
 
// 选项,统一命名options,存放所有的下拉等其他选项列表数据
const options = reactive({
    status: [
        {
            label: '启用',
            value: 1
        },
        {
            label: '禁用',
            type: 'danger',
            value: 0
        }
    ]
});
 
// 合计数据
const subData = reactive({
    wages: 0
});
 
// crud
const Crud = useCrud(
    {
        // 绑定的服务,如:service.demo.goods、service.base.sys.user
        service: 'test',
 
        // 刷新事件
        async onRefresh(params, { next }) {
            const res = await next(params);
            Object.assign(subData, res.subData);
        }
    },
    app => {
        // Crud 加载完,默认刷新一次
        app.refresh({
            size: 10
            // status: 1 // 带额外参数的请求
        });
    }
);
 
// 刷新列表,统一调用这个方法去刷新
function refresh(params?: any) {
    Crud.value?.refresh(params);
}
 
// 新增、编辑
// 插入类型 <Eps.UserInfoEntity>,prop 和 data 会有提示
const Upsert = useUpsert<Eps.UserInfoEntity>({
    items: [
        // 分组
        {
            type: 'tabs',
            props: {
                type: 'card',
                labels: [
                    {
                        label: '基础信息',
                        value: 'base'
                    },
                    {
                        label: '其他配置',
                        value: 'other'
                    }
                ]
            }
        },
        {
            label: '头像',
            prop: 'avatarUrl',
            group: 'base',
            component: {
                name: 'cl-upload'
            }
        },
        {
            label: '账号',
            group: 'base',
            prop: 'account',
            component: {
                name: 'el-input'
            }
        },
        // 动态配置,新增显示、编辑隐藏
        () => {
            return () => {
                return {
                    label: '密码',
                    group: 'base',
                    prop: 'password',
                    hidden: Upsert.value?.mode == 'update', // 通过 mode 参数判断
                    component: {
                        name: 'el-input',
                        props: {
                            type: 'password'
                        }
                    }
                };
            };
        },
        {
            group: 'base',
            prop: 'user',
            component: {
                name: 'cl-form-card',
                props: {
                    label: '用户信息(多层级展示)'
                }
            },
            children: [
                {
                    label: '姓名',
                    prop: 'name',
                    required: true,
                    component: {
                        name: 'el-input'
                    }
                },
                {
                    label: '存款',
                    prop: 'wages',
                    component: {
                        name: 'el-input-number'
                    }
                }
            ]
        },
        {
            group: 'base',
            prop: 'contact',
            component: {
                name: 'cl-form-card',
                props: {
                    label: '联系信息',
                    expand: false
                }
            },
            children: [
                {
                    label: '手机号',
                    prop: 'phone',
                    component: {
                        name: 'el-input'
                    }
                },
                {
                    label: '省市区',
                    prop: 'pca',
                    group: 'base',
                    component: {
                        name: 'cl-distpicker'
                    }
                }
            ]
        },
        {
            group: 'other',
            label: '工作',
            prop: 'occupation',
            component: {
                name: 'el-tree-select',
                props: {
                    data: dict.get('occupation'),
                    checkStrictly: true
                }
            }
        },
        {
            group: 'other',
            label: '身份证照片',
            prop: 'idCardPic',
            component: {
                name: 'cl-upload',
                props: {
                    isSpace: true,
                    size: [200, 300]
                }
            }
        }
    ],
 
    // 详情钩子
    onInfo(data, { next, done }) {
        // 继续请求 info 接口,可以带其他自定义参数
        // next({
        //     id: data.id,
        //    status: 1
        // });
 
        // 使用其他接口
        // service.demo.goods.info({ id: data.id }).then((res) => {
        //     done(res);
        // });
 
        // 直接取列表的数据返回
        done(data);
    },
 
    // 提交钩子
    onSubmit(data, { next, close, done }) {
        console.log('onSubmit', data);
        // 继续请求 update/add 接口
        next(data);
 
        // 自定义接口
        // service.demo.goods
        //     .update(data)
        //     .then(() => {
        //         ElMessage.success("保存成功");
 
        //         // 操作完,刷新列表
        //         refresh();
 
        //         // 关闭窗口
        //         close();
        //     })
        //     .catch((err) => {
        //         ElMessage.error(err.message);
 
        //         // 关闭加载状态
        //         done();
        //     });
    },
 
    // 打开后,数据加载完,onInfo 之后
    onOpened(data) {
        if (Upsert.value?.mode != 'info') {
            ElMessage.info('编辑中');
        }
    },
 
    // 关闭钩子
    onClose(action, done) {
        if (Upsert.value?.mode == 'update') {
            if (action == 'close') {
                return ElMessageBox.confirm('还没填完,确定关闭不?', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        done();
                        ElMessage.info('好吧');
                    })
                    .catch(() => {
                        ElMessage.success('请继续编辑');
                    });
            }
        }
 
        done();
    }
});
 
// 表格
const Table = useTable({
    columns: [
        {
            type: 'selection',
            width: 60
        },
        // 展开列
        {
            label: '展开',
            type: 'expand',
            prop: 'detail',
            width: 60
        },
        {
            label: '头像',
            prop: 'avatar',
            width: 100,
            component: {
                name: 'cl-image',
                props: {
                    size: 40
                }
            }
        },
        {
            label: '姓名',
            prop: 'name',
            minWidth: 120
        },
        {
            label: '手机号',
            prop: 'phone',
            minWidth: 140,
 
            // 带搜索组件
            search: {
                component: {
                    name: 'el-input',
                    props: {
                        placeholder: '搜索手机号'
                    }
                }
            }
        },
        {
            label: '账号',
            prop: 'account',
            minWidth: 150
        },
        {
            label: '存款(元)',
            prop: 'wages',
            minWidth: 150,
            sortable: 'desc' // 默认倒序
        },
        {
            label: '工作',
            prop: 'occupation',
            dict: dict.get('occupation'),
            dictColor: true,
            minWidth: 150,
            dictAllLevels: true, // 显示所有等级
 
            // 带搜索组件
            search: {
                component: {
                    name: 'cl-select',
                    props: {
                        options: dict.get('occupation')
                    }
                }
            }
        },
        {
            label: '状态',
            orderNum: 2,
            prop: 'status',
            minWidth: 100,
            component: {
                name: 'cl-switch'
            }
        },
        {
            label: '出生年月',
            orderNum: 1,
            minWidth: 165,
            prop: 'createTime',
            sortable: 'custom',
            search: {
                component: {
                    name: 'cl-date-picker',
                    props: {
                        type: 'date',
                        valueFormat: 'YYYY-MM-DD',
                        placeholder: '搜索日期'
                    }
                }
            }
        },
        {
            type: 'op',
            width: 340,
            // 静态配置按钮
            // buttons: ["info", "edit", "delete"],
            // 动态配置按钮
            buttons({ scope }) {
                return [
                    'info',
                    'edit',
                    'delete',
                    {
                        label: '自定义',
                        onClick() {
                            ElMessage.info(`他是:${scope.row.name}`);
                        }
                    }
                ];
            }
        }
    ]
});
 
// 合计
function onSummaryMethod() {
    // 添加自定义列组件后
    return ['合计', '', ...refs.columnCustom.summary(subData)];
}
 
// 高级搜索
const AdvSearch = useAdvSearch({
    items: [
        {
            label: '姓名',
            prop: 'name',
            component: {
                name: 'el-input',
                props: {
                    clearable: true
                }
            }
        },
        {
            label: '手机号',
            prop: 'phone',
            component: {
                name: 'el-input',
                props: {
                    clearable: true
                }
            }
        },
        {
            label: '工作',
            prop: 'occupation',
            hook: {
                bind: 'string'
            },
            component: {
                name: 'el-select',
                options: dict.get('occupation')
            }
        }
    ]
});
 
// 搜索
const Search = useSearch({
    items: [
        {
            label: '姓名',
            prop: 'name',
            component: {
                name: 'el-input',
                props: {
                    clearable: true
                }
            }
        }
    ]
});
 
const visible = ref(false);
 
function open() {
    visible.value = true;
}
</script>