refactor: preserveTreeTableState

This commit is contained in:
dap
2025-05-26 09:28:54 +08:00
parent 95859e36a2
commit bea7c1a094
3 changed files with 28 additions and 24 deletions

View File

@@ -1,5 +1,4 @@
import type { VxeGridPropTypes } from '@vben/plugins/vxe-table';
import type { MaybePromise } from '@vben/types';
import { h } from 'vue';
@@ -134,25 +133,3 @@ export function addSortParams(
params.orderByColumn = orderByColumn;
params.isAsc = isAsc;
}
/**
* 保存表格滚动/展开状态并执行回调 用于树表在执行 新增/编辑/删除等操作后 依然在当前位置(体验优化)
*
* @param tableApi 表格api
* @param callback 回调
*/
export async function preserveTreeTableState(
tableApi: ReturnType<typeof useVbenVxeGrid>[1],
callback: () => MaybePromise<void>,
) {
// 保存当前状态
const scrollState = tableApi.grid.getScroll();
const expandRecords = tableApi.grid.getTreeExpandRecords();
// 执行回调
await callback();
// 恢复状态
tableApi.grid.setTreeExpand(expandRecords, true);
tableApi.grid.scrollTo(scrollState.scrollLeft, scrollState.scrollTop);
}