TableSelectForm.vue 2.4 KB

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