提交 | 用户 | 时间
|
314507
|
1 |
<!-- 列表选择通用组件,参考 ProductList 组件使用 --> |
H |
2 |
<template> |
|
3 |
<Dialog v-model="dialogVisible" :appendToBody="true" :scroll="true" :title="title" width="60%"> |
|
4 |
<el-table |
|
5 |
ref="multipleTableRef" |
|
6 |
v-loading="loading" |
|
7 |
:data="list" |
|
8 |
:show-overflow-tooltip="true" |
|
9 |
:stripe="true" |
|
10 |
@selection-change="handleSelectionChange" |
|
11 |
> |
|
12 |
<el-table-column type="selection" width="55" /> |
|
13 |
<slot></slot> |
|
14 |
</el-table> |
|
15 |
<!-- 分页 --> |
|
16 |
<Pagination |
|
17 |
v-model:limit="queryParams.pageSize" |
|
18 |
v-model:page="queryParams.pageNo" |
|
19 |
:total="total" |
|
20 |
@pagination="getList" |
|
21 |
/> |
|
22 |
<template #footer> |
|
23 |
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button> |
|
24 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
25 |
</template> |
|
26 |
</Dialog> |
|
27 |
</template> |
|
28 |
|
|
29 |
<script lang="ts" setup> |
|
30 |
import { ElTable } from 'element-plus' |
|
31 |
|
|
32 |
defineOptions({ name: 'TableSelectForm' }) |
|
33 |
withDefaults( |
|
34 |
defineProps<{ |
|
35 |
modelValue: any[] |
|
36 |
title: string |
|
37 |
}>(), |
|
38 |
{ modelValue: () => [], title: '选择' } |
|
39 |
) |
|
40 |
const list = ref([]) // 列表的数据 |
|
41 |
const total = ref(0) // 列表的总页数 |
|
42 |
const loading = ref(false) // 列表的加载中 |
|
43 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
44 |
const formLoading = ref(false) |
|
45 |
const queryParams = reactive({ |
|
46 |
pageNo: 1, |
|
47 |
pageSize: 10 |
|
48 |
}) |
|
49 |
// 确认选择时的触发事件 |
|
50 |
const emits = defineEmits<{ |
|
51 |
(e: 'update:modelValue', v: number[]): void |
|
52 |
}>() |
|
53 |
const multipleTableRef = ref<InstanceType<typeof ElTable>>() |
|
54 |
const multipleSelection = ref<any[]>([]) |
|
55 |
const handleSelectionChange = (val: any[]) => { |
|
56 |
multipleSelection.value = val |
|
57 |
} |
|
58 |
/** 触发 */ |
|
59 |
const submitForm = () => { |
|
60 |
formLoading.value = true |
|
61 |
try { |
|
62 |
emits('update:modelValue', multipleSelection.value) // 返回选择的原始数据由使用方处理 |
|
63 |
} finally { |
|
64 |
formLoading.value = false |
|
65 |
// 关闭弹窗 |
|
66 |
dialogVisible.value = false |
|
67 |
} |
|
68 |
} |
|
69 |
|
|
70 |
const getList = async (getListFunc: Function) => { |
|
71 |
loading.value = true |
|
72 |
try { |
|
73 |
const data = await getListFunc(queryParams) |
|
74 |
list.value = data.list |
|
75 |
total.value = data.total |
|
76 |
} finally { |
|
77 |
loading.value = false |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
/** 打开弹窗 */ |
|
82 |
const open = async (getListFunc: Function) => { |
|
83 |
dialogVisible.value = true |
|
84 |
await nextTick() |
|
85 |
if (multipleSelection.value.length > 0) { |
|
86 |
multipleTableRef.value!.clearSelection() |
|
87 |
} |
|
88 |
await getList(getListFunc) |
|
89 |
} |
|
90 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
91 |
</script> |