123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- <script lang="ts" setup>
- import type { VbenFormProps } from '@vben-core/form-ui';
- import type {
- VxeGridInstance,
- VxeGridProps as VxeTableGridProps,
- } from 'vxe-table';
- import type { ExtendedVxeGridApi, VxeGridProps } from './types';
- import {
- computed,
- nextTick,
- onMounted,
- onUnmounted,
- toRaw,
- useSlots,
- useTemplateRef,
- watch,
- } from 'vue';
- import { usePriorityValues } from '@vben/hooks';
- import { EmptyIcon } from '@vben/icons';
- import { $t } from '@vben/locales';
- import { usePreferences } from '@vben/preferences';
- import { cloneDeep, cn, mergeWithArrayOverride } from '@vben/utils';
- import { VbenHelpTooltip, VbenLoading } from '@vben-core/shadcn-ui';
- import { VxeGrid, VxeUI } from 'vxe-table';
- import { extendProxyOptions } from './extends';
- import { useTableForm } from './init';
- import 'vxe-table/styles/cssvar.scss';
- import 'vxe-pc-ui/styles/cssvar.scss';
- import './theme.css';
- interface Props extends VxeGridProps {
- api: ExtendedVxeGridApi;
- }
- const props = withDefaults(defineProps<Props>(), {});
- const FORM_SLOT_PREFIX = 'form-';
- const TOOLBAR_ACTIONS = 'toolbar-actions';
- const TOOLBAR_TOOLS = 'toolbar-tools';
- const gridRef = useTemplateRef<VxeGridInstance>('gridRef');
- const state = props.api?.useStore?.();
- const {
- gridOptions,
- class: className,
- gridClass,
- gridEvents,
- formOptions,
- tableTitle,
- tableTitleHelp,
- } = usePriorityValues(props, state);
- const { isMobile } = usePreferences();
- const slots = useSlots();
- const [Form, formApi] = useTableForm({
- handleSubmit: async () => {
- const formValues = formApi.form.values;
- props.api.reload(formValues);
- },
- handleReset: async () => {
- await formApi.resetForm();
- const formValues = formApi.form.values;
- props.api.reload(formValues);
- },
- commonConfig: {
- componentProps: {
- class: 'w-full',
- },
- },
- showCollapseButton: true,
- submitButtonOptions: {
- content: $t('common.query'),
- },
- wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3',
- });
- const showTableTitle = computed(() => {
- return !!slots.tableTitle?.() || tableTitle.value;
- });
- const showToolbar = computed(() => {
- return (
- !!slots[TOOLBAR_ACTIONS]?.() ||
- !!slots[TOOLBAR_TOOLS]?.() ||
- showTableTitle.value
- );
- });
- const toolbarOptions = computed(() => {
- const slotActions = slots[TOOLBAR_ACTIONS]?.();
- const slotTools = slots[TOOLBAR_TOOLS]?.();
- if (!showToolbar.value) {
- return {};
- }
- // 强制使用固定的toolbar配置,不允许用户自定义
- // 减少配置的复杂度,以及后续维护的成本
- return {
- toolbarConfig: {
- slots: {
- ...(slotActions || showTableTitle.value
- ? { buttons: TOOLBAR_ACTIONS }
- : {}),
- ...(slotTools ? { tools: TOOLBAR_TOOLS } : {}),
- },
- },
- };
- });
- const options = computed(() => {
- const globalGridConfig = VxeUI?.getConfig()?.grid ?? {};
- const mergedOptions: VxeTableGridProps = cloneDeep(
- mergeWithArrayOverride(
- {},
- toolbarOptions.value,
- toRaw(gridOptions.value),
- globalGridConfig,
- ),
- );
- if (mergedOptions.proxyConfig) {
- const { ajax } = mergedOptions.proxyConfig;
- mergedOptions.proxyConfig.enabled = !!ajax;
- // 不自动加载数据, 由组件控制
- mergedOptions.proxyConfig.autoLoad = false;
- }
- if (mergedOptions.pagerConfig) {
- const mobileLayouts = [
- 'PrevJump',
- 'PrevPage',
- 'Number',
- 'NextPage',
- 'NextJump',
- ] as any;
- const layouts = [
- 'Total',
- 'Sizes',
- 'Home',
- ...mobileLayouts,
- 'End',
- ] as readonly string[];
- mergedOptions.pagerConfig = mergeWithArrayOverride(
- {},
- mergedOptions.pagerConfig,
- {
- pageSize: 20,
- background: true,
- pageSizes: [10, 20, 30, 50, 100, 200],
- className: 'mt-2 w-full',
- layouts: isMobile.value ? mobileLayouts : layouts,
- size: 'mini' as const,
- },
- );
- }
- if (mergedOptions.formConfig) {
- mergedOptions.formConfig.enabled = false;
- }
- return mergedOptions;
- });
- const events = computed(() => {
- return {
- ...gridEvents.value,
- };
- });
- const delegatedSlots = computed(() => {
- const resultSlots: string[] = [];
- for (const key of Object.keys(slots)) {
- if (!['empty', 'form', 'loading', TOOLBAR_ACTIONS].includes(key)) {
- resultSlots.push(key);
- }
- }
- return resultSlots;
- });
- const delegatedFormSlots = computed(() => {
- const resultSlots: string[] = [];
- for (const key of Object.keys(slots)) {
- if (key.startsWith(FORM_SLOT_PREFIX)) {
- resultSlots.push(key);
- }
- }
- return resultSlots.map((key) => key.replace(FORM_SLOT_PREFIX, ''));
- });
- async function init() {
- await nextTick();
- const globalGridConfig = VxeUI?.getConfig()?.grid ?? {};
- const defaultGridOptions: VxeTableGridProps = mergeWithArrayOverride(
- {},
- toRaw(gridOptions.value),
- toRaw(globalGridConfig),
- );
- // 内部主动加载数据,防止form的默认值影响
- const autoLoad = defaultGridOptions.proxyConfig?.autoLoad;
- const enableProxyConfig = options.value.proxyConfig?.enabled;
- if (enableProxyConfig && autoLoad) {
- props.api.reload(formApi.form?.values ?? {});
- }
- // form 由 vben-form代替,所以不适配formConfig,这里给出警告
- const formConfig = gridOptions.value?.formConfig;
- // 处理某个页面加载多个Table时,第2个之后的Table初始化报出警告
- // 因为第一次初始化之后会把defaultGridOptions和gridOptions合并后缓存进State
- if (formConfig && formConfig.enabled) {
- console.warn(
- '[Vben Vxe Table]: The formConfig in the grid is not supported, please use the `formOptions` props',
- );
- }
- props.api?.setState?.({ gridOptions: defaultGridOptions });
- // form 由 vben-form 代替,所以需要保证query相关事件可以拿到参数
- extendProxyOptions(props.api, defaultGridOptions, () => formApi.form.values);
- }
- // formOptions支持响应式
- watch(
- formOptions,
- () => {
- formApi.setState((prev) => {
- const finalFormOptions: VbenFormProps = mergeWithArrayOverride(
- {},
- formOptions.value,
- prev,
- );
- return {
- ...finalFormOptions,
- collapseTriggerResize: !!finalFormOptions.showCollapseButton,
- };
- });
- },
- {
- immediate: true,
- },
- );
- onMounted(() => {
- props.api?.mount?.(gridRef.value, formApi);
- init();
- });
- onUnmounted(() => {
- formApi?.unmount?.();
- props.api?.unmount?.();
- });
- </script>
- <template>
- <div :class="cn('bg-card h-full rounded-md', className)">
- <VxeGrid
- ref="gridRef"
- :class="
- cn(
- 'p-2',
- {
- 'pt-0': showToolbar && !formOptions,
- },
- gridClass,
- )
- "
- v-bind="options"
- v-on="events"
- >
- <!-- 左侧操作区域或者title -->
- <template v-if="showToolbar" #toolbar-actions="slotProps">
- <slot v-if="showTableTitle" name="table-title">
- <div class="mr-1 pl-1 text-[1rem]">
- {{ tableTitle }}
- <VbenHelpTooltip v-if="tableTitleHelp" trigger-class="pb-1">
- {{ tableTitleHelp }}
- </VbenHelpTooltip>
- </div>
- </slot>
- <slot name="toolbar-actions" v-bind="slotProps"> </slot>
- </template>
- <!-- 继承默认的slot -->
- <template
- v-for="slotName in delegatedSlots"
- :key="slotName"
- #[slotName]="slotProps"
- >
- <slot :name="slotName" v-bind="slotProps"></slot>
- </template>
- <!-- form表单 -->
- <template #form>
- <div v-if="formOptions" class="relative rounded py-3 pb-4">
- <slot name="form">
- <Form>
- <template
- v-for="slotName in delegatedFormSlots"
- :key="slotName"
- #[slotName]="slotProps"
- >
- <slot
- :name="`${FORM_SLOT_PREFIX}${slotName}`"
- v-bind="slotProps"
- ></slot>
- </template>
- <template #reset-before="slotProps">
- <slot name="reset-before" v-bind="slotProps"></slot>
- </template>
- <template #submit-before="slotProps">
- <slot name="submit-before" v-bind="slotProps"></slot>
- </template>
- <template #expand-before="slotProps">
- <slot name="expand-before" v-bind="slotProps"></slot>
- </template>
- <template #expand-after="slotProps">
- <slot name="expand-after" v-bind="slotProps"></slot>
- </template>
- </Form>
- </slot>
- <div
- class="bg-background-deep z-100 absolute -left-2 bottom-1 h-2 w-[calc(100%+1rem)] overflow-hidden md:bottom-2 md:h-3"
- ></div>
- </div>
- </template>
- <!-- loading -->
- <template #loading>
- <slot name="loading">
- <VbenLoading :spinning="true" />
- </slot>
- </template>
- <!-- 统一控状态 -->
- <template #empty>
- <slot name="empty">
- <EmptyIcon class="mx-auto" />
- <div class="mt-2">{{ $t('common.noData') }}</div>
- </slot>
- </template>
- </VxeGrid>
- </div>
- </template>
|