feat: table-toolbar

This commit is contained in:
xingyu4j
2025-09-09 17:04:57 +08:00
parent b3a65f2492
commit 29e5017913
40 changed files with 429 additions and 650 deletions

View File

@@ -1,13 +1,27 @@
export { AsyncComponents, setupVbenVxeTable } from './init';
import { defineAsyncComponent } from 'vue';
export { setupVbenVxeTable } from './init';
export { default as VbenVxeTableToolbar } from './table-toolbar.vue';
export type { VxeTableGridOptions } from './types';
export * from './use-vxe-grid';
export { default as VbenVxeGrid } from './use-vxe-grid.vue';
export { useTableToolbar } from './use-vxe-toolbar';
export * from './validation';
export type {
VxeGridListeners,
VxeGridProps,
VxeGridPropTypes,
VxeTableInstance,
VxeToolbarInstance,
} from 'vxe-table';
// 异步导出 vxe-table 相关组件提供给需要单独使用 vxe-table 的场景
export const AsyncVxeTable = defineAsyncComponent(() =>
import('vxe-table').then((mod) => mod.VxeTable),
);
export const AsyncVxeColumn = defineAsyncComponent(() =>
import('vxe-table').then((mod) => mod.VxeColumn),
);
export const AsyncVxeToolbar = defineAsyncComponent(() =>
import('vxe-table').then((mod) => mod.VxeToolbar),
);

View File

@@ -1,6 +1,6 @@
import type { SetupVxeTable } from './types';
import { defineAsyncComponent, defineComponent, watch } from 'vue';
import { defineComponent, watch } from 'vue';
import { usePreferences } from '@vben/preferences';
@@ -100,18 +100,6 @@ export function initVxeTable() {
isInit = true;
}
// 异步导出 vxe-table 相关组件提供给需要单独使用 vxe-table 的场景
const AsyncVxeTable = defineAsyncComponent(() =>
import('vxe-table').then((mod) => mod.VxeTable),
);
const AsyncVxeColumn = defineAsyncComponent(() =>
import('vxe-table').then((mod) => mod.VxeColumn),
);
const AsyncVxeToolbar = defineAsyncComponent(() =>
import('vxe-table').then((mod) => mod.VxeToolbar),
);
export const AsyncComponents = [AsyncVxeTable, AsyncVxeColumn, AsyncVxeToolbar];
export function setupVbenVxeTable(setupOptions: SetupVxeTable) {
const { configVxeTable, useVbenForm } = setupOptions;

View File

@@ -0,0 +1,74 @@
<!-- add by puhui999vxe table 工具栏二次封装提供给 vxe 原生列表使用 -->
<script setup lang="ts">
import type { VxeToolbarInstance } from 'vxe-table';
import { ref } from 'vue';
import { useContentMaximize, useRefresh } from '@vben/hooks';
import { IconifyIcon } from '@vben/icons';
import { VxeButton, VxeTooltip } from 'vxe-pc-ui';
import { VxeToolbar } from 'vxe-table';
/** 列表工具栏封装 */
defineOptions({ name: 'TableToolbar' });
const props = defineProps<{
hiddenSearch: boolean;
}>();
const emits = defineEmits(['update:hiddenSearch']);
const toolbarRef = ref<VxeToolbarInstance>();
const { toggleMaximizeAndTabbarHidden, contentIsMaximize } =
useContentMaximize();
const { refresh } = useRefresh();
/** 隐藏搜索栏 */
function onHiddenSearchBar() {
emits('update:hiddenSearch', !props.hiddenSearch);
}
defineExpose({
getToolbarRef: () => toolbarRef.value,
});
</script>
<template>
<VxeToolbar ref="toolbarRef" custom>
<template #toolPrefix>
<slot></slot>
<VxeTooltip placement="bottom" content="搜索">
<template #default>
<VxeButton class="ml-2 font-normal" circle @click="onHiddenSearchBar">
<IconifyIcon icon="lucide:search" :size="15" />
</VxeButton>
</template>
</VxeTooltip>
<VxeTooltip
placement="bottom"
:content="contentIsMaximize ? '还原' : '全屏'"
>
<template #default>
<VxeButton class="ml-2 font-medium" circle @click="refresh">
<IconifyIcon icon="lucide:refresh-cw" :size="15" />
</VxeButton>
</template>
</VxeTooltip>
<VxeTooltip placement="bottom" content="全屏">
<template #default>
<VxeButton
class="ml-2 font-medium"
circle
@click="toggleMaximizeAndTabbarHidden"
>
<IconifyIcon
:icon="contentIsMaximize ? 'lucide:minimize' : 'lucide:maximize'"
:size="15"
/>
</VxeButton>
</template>
</VxeTooltip>
</template>
</VxeToolbar>
</template>

View File

@@ -0,0 +1,48 @@
import type { VxeTableInstance, VxeToolbarInstance } from 'vxe-table';
import { ref, watch } from 'vue';
import VbenVxeTableToolbar from './table-toolbar.vue';
/**
* vxe 原生工具栏挂载封装
* 解决每个组件使用 vxe-table 组件时都需要写一遍的问题
*/
export function useTableToolbar() {
const hiddenSearchBar = ref(false); // 隐藏搜索栏
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) {
// 延迟 1 秒,确保 toolbar 组件已经挂载
setTimeout(async () => {
const toolbar = tableToolbar.getToolbarRef();
if (!toolbar) {
console.error('[toolbar 挂载失败] Table toolbar not found');
}
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,
};
}