Files
iot-device-management-frontend/packages/effects/plugins/src/vxe-table/use-vxe-toolbar.ts

49 lines
1.3 KiB
TypeScript
Raw Normal View History

2025-09-09 17:04:57 +08:00
import type { VxeTableInstance, VxeToolbarInstance } from 'vxe-table';
import { ref, watch } from 'vue';
2025-09-09 17:04:57 +08:00
import VbenVxeTableToolbar from './table-toolbar.vue';
/**
* vxe
* 使 vxe-table
*/
export function useTableToolbar() {
const hiddenSearchBar = ref(false); // 隐藏搜索栏
2025-09-09 17:04:57 +08:00
const tableToolbarRef = ref<InstanceType<typeof VbenVxeTableToolbar>>();
const tableRef = ref<VxeTableInstance>();
const isBound = ref<boolean>(false);
/** 挂载 toolbar 工具栏 */
async function bindTableToolbar() {
const table = tableRef.value;
const tableToolbar = tableToolbarRef.value;
if (table && tableToolbar) {
2025-05-20 21:02:28 +08:00
// 延迟 1 秒,确保 toolbar 组件已经挂载
setTimeout(async () => {
const toolbar = tableToolbar.getToolbarRef();
if (!toolbar) {
console.error('[toolbar 挂载失败] Table toolbar not found');
}
2025-09-09 17:04:57 +08:00
await table.connectToolbar(toolbar as VxeToolbarInstance);
isBound.value = true;
}, 1000); // 延迟挂载确保 toolbar 正确挂载
}
}
watch(
() => tableRef.value,
async (val) => {
if (!val || isBound.value) return;
await bindTableToolbar();
},
{ immediate: true },
);
return {
hiddenSearchBar,
tableToolbarRef,
tableRef,
};
}