WholeProcessPlatform/frontend/src/components/BasicTable/index.vue

213 lines
5.3 KiB
Vue
Raw Normal View History

2026-04-22 17:53:20 +08:00
<template>
<div class="table-container" ref="tableContainerRef">
<a-table
size="small"
:loading="loading"
:row-selection="enableRowSelection ? rowSelection : undefined"
:data-source="tableData"
:columns="columns"
:pagination="paginationConfig"
:scroll="scrollConfig"
:row-key="rowKey"
@change="handleTableChange"
>
<!-- 透传插槽支持自定义列内容 -->
<template v-for="slot in Object.keys($slots)" #[slot]="scope" :key="slot">
<slot :name="slot" v-bind="scope"></slot>
</template>
</a-table>
</div>
2026-04-22 17:53:20 +08:00
</template>
<script setup lang="ts">
import { ref, computed, onMounted, watch, nextTick } from "vue";
import { calcTableScrollY } from "@/utils/index";
2026-04-22 17:53:20 +08:00
// --- Types ---
interface Props {
columns: any[];
scrollY?: string | number; // 优先使用传入的固定值
2026-04-22 17:53:20 +08:00
listUrl: (params: any) => Promise<any>;
data?: any[];
2026-04-22 17:53:20 +08:00
enableRowSelection?: boolean;
rowKey?: string;
searchParams?: Record<string, any>;
defaultPageSize?: number;
getCheckboxProps?: (record: any) => any;
transformData?: (res: any) => { records: any[]; total: number };
2026-04-22 17:53:20 +08:00
}
const props = withDefaults(defineProps<Props>(), {
enableRowSelection: false,
rowKey: "id",
data: () => [],
2026-04-22 17:53:20 +08:00
searchParams: () => ({}),
defaultPageSize: 20,
getCheckboxProps: undefined,
transformData: undefined,
scrollY: undefined,
2026-04-22 17:53:20 +08:00
});
const emit = defineEmits<{
(e: "data-loaded", params: any, data: any): void;
(e: "selection-change", selectedRowKeys: string[], selectedRows: any[]): void;
}>();
// --- State ---
const loading = ref(false);
const tableData = ref<any[]>([]);
const total = ref(0);
const page = ref(1);
const size = ref(props.defaultPageSize);
const selectedRowKeys = ref<string[]>([]);
const selectedRows = ref<any[]>([]);
2026-05-05 22:38:24 +08:00
const lastFilter = ref<Record<string, any> | undefined>(undefined);
const tableContainerRef = ref<any>(null);
const tableScrollY = ref<number>(0); // ✅ 新增:存储容器高度
// --- Computed Scroll Config ---
const scrollConfig = computed(() => {
const config: any = {
x: "100%",
};
// 1. 如果父组件传入了固定的 scrollY优先使用
if (props.scrollY !== undefined && props.scrollY !== null) {
config.y = props.scrollY;
}
// 2. 否则,如果检测到容器有高度,则使用容器高度作为滚动高度
else if (tableScrollY.value > 0) {
config.y = tableScrollY.value;
}
2026-05-05 22:38:24 +08:00
return config;
});
2026-04-22 17:53:20 +08:00
// --- Row Selection ---
const rowSelection = computed(() => ({
selectedRowKeys: selectedRowKeys.value,
onChange: (keys: string[], rows: any[]) => {
selectedRowKeys.value = keys;
selectedRows.value = rows;
emit("selection-change", keys, rows);
},
getCheckboxProps: props.getCheckboxProps
? props.getCheckboxProps
: (record: any) => ({}),
2026-04-22 17:53:20 +08:00
}));
// --- Pagination Config ---
const paginationConfig = computed(() => ({
total: total.value,
current: page.value,
pageSize: size.value,
showSizeChanger: true,
showQuickJumper: true,
showTotal: (total: number) => `${total}`,
pageSizeOptions: ["20", "50", "100"],
}));
// --- Methods ---
2026-04-24 15:31:32 +08:00
const getList = async (filter?: Record<string, any>) => {
if (props.data.length > 0) return;
2026-04-22 17:53:20 +08:00
loading.value = true;
2026-05-07 15:40:18 +08:00
tableData.value = [];
total.value = 0;
2026-05-05 22:38:24 +08:00
if (filter !== undefined) {
lastFilter.value = filter;
}
2026-04-22 17:53:20 +08:00
try {
const params = {
2026-04-30 17:09:07 +08:00
...props.searchParams,
2026-04-22 17:53:20 +08:00
skip: page.value,
take: size.value,
2026-05-05 22:38:24 +08:00
filter: lastFilter.value,
2026-04-22 17:53:20 +08:00
};
const res = await props.listUrl(params);
let finalRecords: any[] = [];
let finalTotal: number = 0;
if (props.transformData) {
const result = props.transformData(res);
finalRecords = result.records || [];
finalTotal = result.total || 0;
} else {
finalRecords = res?.data?.records || res?.data || [];
finalTotal = res?.data?.total || res?.total || 0;
}
2026-04-24 15:31:32 +08:00
tableData.value = finalRecords;
total.value = finalTotal;
emit("data-loaded", params, { records: finalRecords, total: finalTotal });
2026-04-22 17:53:20 +08:00
} catch (error) {
console.error("Fetch table data error:", error);
tableData.value = [];
total.value = 0;
} finally {
loading.value = false;
}
};
const handleTableChange = (pag: any) => {
page.value = pag.current;
size.value = pag.pageSize;
getList();
};
const reset = () => {
page.value = 1;
size.value = props.defaultPageSize;
selectedRowKeys.value = [];
selectedRows.value = [];
getList();
};
const refresh = () => {
getList();
};
2026-05-07 15:40:18 +08:00
const clearSelection = () => {
selectedRowKeys.value = [];
selectedRows.value = [];
};
2026-04-22 17:53:20 +08:00
defineExpose({
loading,
2026-04-22 17:53:20 +08:00
getList,
reset,
refresh,
2026-05-07 15:40:18 +08:00
tableData,
clearSelection,
2026-04-22 17:53:20 +08:00
getSelected: () => ({
keys: selectedRowKeys.value,
rows: selectedRows.value,
}),
});
watch(
() => props.searchParams,
() => {
page.value = 1;
2026-04-22 17:53:20 +08:00
getList();
},
{ deep: true }
);
const observer = new ResizeObserver(() => {
tableScrollY.value = calcTableScrollY(tableContainerRef.value);
});
2026-04-22 17:53:20 +08:00
onMounted(() => {
nextTick(() => {
tableScrollY.value = calcTableScrollY(tableContainerRef.value);
if (tableContainerRef.value) {
observer.observe(tableContainerRef.value);
}
});
if (props.data && props.data.length > 0) {
tableData.value = props.data || [];
}
2026-04-22 17:53:20 +08:00
});
</script>
<style scoped lang="scss"></style>