|
@@ -0,0 +1,130 @@
|
|
|
+<template>
|
|
|
+ <div class="tableBox">
|
|
|
+ <div class="table" ref="tableRef">
|
|
|
+ <el-table
|
|
|
+ stripe
|
|
|
+ :data="dataSource"
|
|
|
+ :style="{ height: tableHeight ? tableHeight + 'px' : '500px' }"
|
|
|
+ v-loading="loading"
|
|
|
+ :header-cell-style="{
|
|
|
+ background: '#E5F0FB',
|
|
|
+ color: '#233755',
|
|
|
+ height: '50px'
|
|
|
+ }"
|
|
|
+ >
|
|
|
+ <el-table-column label="序号" width="60" align="center" v-if="showIndex">
|
|
|
+ <template #default="scope">{{ scope.$index + 1 }}</template>
|
|
|
+ </el-table-column>
|
|
|
+ <el-table-column
|
|
|
+ v-for="item in column"
|
|
|
+ :key="item.prop"
|
|
|
+ :prop="item.prop"
|
|
|
+ :label="item.label"
|
|
|
+ :width="item.width"
|
|
|
+ :align="item.align"
|
|
|
+ />
|
|
|
+ </el-table>
|
|
|
+ </div>
|
|
|
+ <div class="pageBox" v-if="dataSource.length > 0">
|
|
|
+ <el-pagination
|
|
|
+ :page-size="20"
|
|
|
+ background
|
|
|
+ layout="total, prev, pager, next"
|
|
|
+ :total="pageTotal"
|
|
|
+ :current-page="pageNo"
|
|
|
+ @current-change="currentChange"
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+<script lang="ts" setup>
|
|
|
+import { ref, onMounted } from 'vue'
|
|
|
+import http from '@/config/axios'
|
|
|
+import PubsubService from '@/utils/PubsubService'
|
|
|
+
|
|
|
+interface ITable {
|
|
|
+ pageKey: string // 页面的唯一标识
|
|
|
+ request: {
|
|
|
+ url: string
|
|
|
+ params: any
|
|
|
+ }
|
|
|
+ column: {
|
|
|
+ // 表格属性
|
|
|
+ label: string
|
|
|
+ prop: string
|
|
|
+ width?: string | number
|
|
|
+ align?: string
|
|
|
+ tooltip?: boolean
|
|
|
+ }[]
|
|
|
+ showIndex?: boolean // 是否显示表格序号
|
|
|
+}
|
|
|
+
|
|
|
+const props = defineProps<ITable>()
|
|
|
+const { pageKey, request, column, showIndex } = props
|
|
|
+
|
|
|
+const loading = ref(false)
|
|
|
+const dataSource = ref([])
|
|
|
+const pageNo = ref(1)
|
|
|
+const pageTotal = ref(0)
|
|
|
+
|
|
|
+const tableRef: any = ref(null)
|
|
|
+const tableHeight: any = ref(0)
|
|
|
+
|
|
|
+const updateTableHeight = () => {
|
|
|
+ if (tableRef.value) {
|
|
|
+ tableHeight.value = tableRef.value.clientHeight
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+onMounted(() => {
|
|
|
+ updateTableHeight()
|
|
|
+ window.addEventListener('resize', updateTableHeight)
|
|
|
+
|
|
|
+ // 初始化
|
|
|
+ getDataSource()
|
|
|
+ // 接收查询事件
|
|
|
+ PubsubService.subscribe(pageKey, (params) => {
|
|
|
+ getDataSource(params)
|
|
|
+ })
|
|
|
+})
|
|
|
+
|
|
|
+onBeforeUnmount(() => {
|
|
|
+ window.removeEventListener('resize', updateTableHeight)
|
|
|
+})
|
|
|
+
|
|
|
+// 获取数据
|
|
|
+const getDataSource = (params = {}) => {
|
|
|
+ if (!request.url || request.url == '') return
|
|
|
+ loading.value = true
|
|
|
+ http
|
|
|
+ .get({
|
|
|
+ url: request.url,
|
|
|
+ params: { ...request.params, ...params, pageNo: pageNo.value, pageSize: 20 }
|
|
|
+ })
|
|
|
+ .then((res) => {
|
|
|
+ ElMessage.success(`数据查询成功`)
|
|
|
+ loading.value = false
|
|
|
+ const { list, total } = res
|
|
|
+ dataSource.value = list
|
|
|
+ pageTotal.value = total
|
|
|
+ pageNo.value = 1
|
|
|
+ })
|
|
|
+ .catch(() => {
|
|
|
+ ElMessage.error('查询失败,请稍后重试!')
|
|
|
+ loading.value = false
|
|
|
+ })
|
|
|
+}
|
|
|
+// 切换页面
|
|
|
+const currentChange = (page) => {
|
|
|
+ pageNo.value = page
|
|
|
+ getDataSource()
|
|
|
+}
|
|
|
+</script>
|
|
|
+<style scoped lang="scss">
|
|
|
+.tableBox {
|
|
|
+ margin-top: 10px;
|
|
|
+}
|
|
|
+.pageBox {
|
|
|
+ margin-top: 10px;
|
|
|
+}
|
|
|
+</style>
|