diff --git a/mayfly_go_web/src/components/pagetable/PageTable.vue b/mayfly_go_web/src/components/pagetable/PageTable.vue
index b6a64d4a..3a551026 100644
--- a/mayfly_go_web/src/components/pagetable/PageTable.vue
+++ b/mayfly_go_web/src/components/pagetable/PageTable.vue
@@ -2,7 +2,7 @@
-
+
@@ -50,7 +50,7 @@
-
+
@@ -82,16 +82,17 @@
-
+
必传
- data?: any[]; // 静态 table data 数据,若存在则不会使用 requestApi 返回的 data ==> 非必传
pageApi: Api; // 请求表格数据的 api
+ columns: TableColumn[]; // 列配置项 ==> 必传
+ showSelection?: boolean;
+ selectable?: (row: any) => boolean; // 是否可选
+ showSearch?: boolean; // 是否显示搜索表单
+ data?: any[]; // 静态 table data 数据,若存在则不会使用 requestApi 返回的 data ==> 非必传
lazy?: boolean; // 是否自动执行请求 api ==> 非必传(默认为false)
beforeQueryFn?: (params: any) => any; // 执行查询时对查询参数进行处理,调整等
dataHandlerFn?: (data: any) => any; // 数据处理回调函数,用于将请求回来的数据二次加工处理等
@@ -212,6 +216,9 @@ const props = withDefaults(defineProps(), {
searchCol: () => ({ xs: 1, sm: 3, md: 3, lg: 4, xl: 5 }),
});
+// table 实例
+const tableRef = ref>();
+
// 接收 columns 并设置为响应式
const tableColumns = reactive(props.columns);
@@ -228,18 +235,6 @@ const showToolButton = (key: 'setting' | 'search') => {
return Array.isArray(props.toolButton) ? props.toolButton.includes(key) : props.toolButton;
};
-const state = reactive({
- pageSizes: [] as any, // 可选每页显示的数据量
- loading: false,
- data: [],
- total: 0,
- // 输入框宽度
- formatVal: '', // 格式化后的值
- tableMaxHeight: '500px',
-});
-
-const { pageSizes, formatVal, tableMaxHeight } = toRefs(state);
-
const nowSearchItem: Ref = ref(null) as any;
/**
@@ -254,19 +249,32 @@ const changeSimpleFormItem = (searchItem: SearchItem) => {
const queryForm_: Ref = useVModel(props, 'queryForm', emit);
-watch(
- () => state.data,
- (newValue: any) => {
- if (newValue && newValue.length > 0) {
- props.columns.forEach((item) => {
- if (item.autoWidth && item.show) {
- item.autoCalculateMinWidth(state.data);
- }
- });
- }
- }
+const { tableData, total, loading, search, reset, getTableData, handlePageNumChange, handlePageSizeChange } = usePageTable(
+ props.pageApi,
+ queryForm_,
+ props.beforeQueryFn,
+ props.dataHandlerFn
);
+const state = reactive({
+ pageSizes: [] as any, // 可选每页显示的数据量
+ // 输入框宽度
+ formatVal: '', // 格式化后的值
+ tableMaxHeight: '500px',
+});
+
+const { pageSizes, formatVal, tableMaxHeight } = toRefs(state);
+
+watch(tableData, (newValue: any) => {
+ if (newValue && newValue.length > 0) {
+ props.columns.forEach((item) => {
+ if (item.autoWidth && item.show) {
+ item.autoCalculateMinWidth(tableData.value);
+ }
+ });
+ }
+});
+
watch(isShowSearch, () => {
calcuTableHeight();
});
@@ -294,7 +302,7 @@ onMounted(async () => {
state.pageSizes = [pageSize, pageSize * 2, pageSize * 3, pageSize * 4, pageSize * 5];
if (!props.lazy) {
- await reqPageApi();
+ await getTableData();
}
});
@@ -316,66 +324,9 @@ const handleSelectionChange = (val: any) => {
emit('update:selectionData', val);
};
-const reqPageApi = async () => {
- try {
- state.loading = true;
-
- let qf = queryForm_.value;
- if (props.beforeQueryFn) {
- qf = await props.beforeQueryFn(qf);
- }
-
- const res = await props.pageApi?.request(qf);
- if (!res) {
- return;
- }
-
- state.total = res.total;
- if (props.dataHandlerFn) {
- state.data = await props.dataHandlerFn(res.list);
- } else {
- state.data = res.list;
- }
- } finally {
- state.loading = false;
- }
-};
-
-const handlePageChange = (val: number) => {
- queryForm_.value.pageNum = val;
- execQuery();
-};
-
-const handleSizeChange = () => {
- changePageNum(1);
- execQuery();
-};
-
-const queryData = () => {
- changePageNum(1);
- execQuery();
-};
-
-const reset = () => {
- // 将查询参数绑定的值置空,并重新粗发查询接口
- for (let qi of props.searchItems) {
- queryForm_.value[qi.prop] = null;
- }
-
- changePageNum(1);
- execQuery();
-};
-
-const changePageNum = (pageNum: number) => {
- queryForm_.value.pageNum = pageNum;
-};
-
-const execQuery = async () => {
- await reqPageApi();
-};
-
defineExpose({
- search: execQuery,
+ tableRef: tableRef,
+ search: getTableData,
});