| | |
| | | <template> |
| | | <div |
| | | ref="wrapRef" |
| | | class="basic-table" |
| | | :class="{ |
| | | 'table-form-container': getBindValues.useSearchForm, |
| | |
| | | </div> |
| | | </template> |
| | | <script lang="ts"> |
| | | import { defineComponent, ref, computed, unref, watch, nextTick } from 'vue'; |
| | | import { defineComponent, ref, computed, unref, watch, nextTick, toRaw } from 'vue'; |
| | | import { Table } from 'ant-design-vue'; |
| | | import { basicProps } from './props'; |
| | | import type { |
| | |
| | | FetchParams, |
| | | GetColumnsParams, |
| | | TableActionType, |
| | | SizeType, |
| | | } from './types/table'; |
| | | |
| | | import { isFunction, isString } from '/@/utils/is'; |
| | | |
| | | import renderTitle from './components/renderTitle'; |
| | |
| | | import { provideTable } from './hooks/useProvinceTable'; |
| | | import { BasicForm, FormProps, useForm } from '/@/components/Form/index'; |
| | | import { omit } from 'lodash-es'; |
| | | import './style/index.less'; |
| | | import { ROW_KEY } from './const'; |
| | | import { PaginationProps } from './types/pagination'; |
| | | import { deepMerge } from '/@/utils'; |
| | | import { TableCustomRecord } from 'ant-design-vue/types/table/table'; |
| | | import { useEvent } from '/@/hooks/event/useEvent'; |
| | | |
| | | import './style/index.less'; |
| | | export default defineComponent({ |
| | | props: basicProps, |
| | | components: { Table, BasicForm }, |
| | | emits: ['fetch-success', 'fetch-error', 'selection-change', 'register'], |
| | | setup(props, { attrs, emit, slots }) { |
| | | const tableElRef = ref<any>(null); |
| | | const wrapRef = ref<Nullable<HTMLDivElement>>(null); |
| | | const innerPropsRef = ref<Partial<BasicTableProps>>(); |
| | | const [registerForm, { getFieldsValue }] = useForm(); |
| | | |
| | |
| | | }, |
| | | emit |
| | | ); |
| | | |
| | | const { getScrollRef, redoHeight } = useTableScroll(getMergeProps, tableElRef); |
| | | const { |
| | | getRowSelectionRef, |
| | |
| | | |
| | | return unref(getAutoCreateKey) ? ROW_KEY : rowKey; |
| | | }); |
| | | |
| | | const getBindValues = computed(() => { |
| | | const { title, titleHelpMessage, showSummary } = unref(getMergeProps); |
| | | const { title, titleHelpMessage, showSummary, showTableSetting, tableSetting } = unref( |
| | | getMergeProps |
| | | ); |
| | | const hideTitle = !slots.tableTitle && !title && !slots.toolbar && !showTableSetting; |
| | | const titleData: any = |
| | | !slots.tableTitle && !isString(title) && !title && !slots.toolbar |
| | | hideTitle && !isString(title) |
| | | ? {} |
| | | : { |
| | | title: |
| | | !slots.tableTitle && !title && !slots.toolbar |
| | | ? null |
| | | : renderTitle.bind(null, title, titleHelpMessage, slots), |
| | | title: hideTitle |
| | | ? null |
| | | : renderTitle.bind( |
| | | null, |
| | | title, |
| | | titleHelpMessage, |
| | | slots, |
| | | showTableSetting, |
| | | tableSetting |
| | | ), |
| | | }; |
| | | const pagination = unref(getPaginationRef); |
| | | const rowSelection = unref(getRowSelectionRef); |
| | |
| | | } |
| | | if (showSummary) { |
| | | propsData.footer = renderFooter.bind(null, { |
| | | scroll, |
| | | scroll: scroll as any, |
| | | columnsRef: getColumnsRef, |
| | | summaryFunc: unref(getMergeProps).summaryFunc, |
| | | dataSourceRef: getDataSourceRef, |
| | |
| | | } |
| | | return propsData; |
| | | }); |
| | | |
| | | const getFormProps = computed(() => { |
| | | const { formConfig } = unref(getBindValues); |
| | | const formProps: FormProps = { |
| | |
| | | setPagination(pagination); |
| | | fetch(); |
| | | } |
| | | |
| | | function handleSummary() { |
| | | if (unref(getMergeProps).showSummary) { |
| | | nextTick(() => { |
| | | const tableEl = unref(tableElRef); |
| | | if (!tableEl) { |
| | | return; |
| | | } |
| | | const bodyDomList = tableEl.$el.querySelectorAll('.ant-table-body') as HTMLDivElement[]; |
| | | const bodyDom = bodyDomList[0]; |
| | | useEvent({ |
| | | el: bodyDom, |
| | | name: 'scroll', |
| | | listener: () => { |
| | | const footerBodyDom = tableEl.$el.querySelector( |
| | | '.ant-table-footer .ant-table-body' |
| | | ) as HTMLDivElement; |
| | | if (!footerBodyDom || !bodyDom) return; |
| | | footerBodyDom.scrollLeft = bodyDom.scrollLeft; |
| | | }, |
| | | wait: 0, |
| | | options: true, |
| | | }); |
| | | }); |
| | | } |
| | | } |
| | | |
| | | watch( |
| | | () => unref(getDataSourceRef), |
| | | () => { |
| | | if (unref(getMergeProps).showSummary) { |
| | | nextTick(() => { |
| | | const tableEl = unref(tableElRef); |
| | | if (!tableEl) { |
| | | return; |
| | | } |
| | | const bodyDomList = tableEl.$el.querySelectorAll( |
| | | '.ant-table-body' |
| | | ) as HTMLDivElement[]; |
| | | const bodyDom = bodyDomList[0]; |
| | | useEvent({ |
| | | el: bodyDom, |
| | | name: 'scroll', |
| | | listener: () => { |
| | | const footerBodyDom = tableEl.$el.querySelector( |
| | | '.ant-table-footer .ant-table-body' |
| | | ) as HTMLDivElement; |
| | | if (!footerBodyDom || !bodyDom) return; |
| | | footerBodyDom.scrollLeft = bodyDom.scrollLeft; |
| | | }, |
| | | wait: 0, |
| | | options: true, |
| | | }); |
| | | }); |
| | | } |
| | | handleSummary(); |
| | | }, |
| | | { immediate: true } |
| | | ); |
| | | |
| | | function setProps(props: Partial<BasicTableProps>) { |
| | | innerPropsRef.value = deepMerge(unref(innerPropsRef) || {}, props); |
| | | } |
| | | |
| | | const tableAction: TableActionType = { |
| | | reload: async (opt?: FetchParams) => { |
| | |
| | | return unref(getPaginationRef); |
| | | }, |
| | | getColumns: (opt?: GetColumnsParams) => { |
| | | const { ignoreIndex } = opt || {}; |
| | | let columns = unref(getColumnsRef); |
| | | const { ignoreIndex, ignoreAction } = opt || {}; |
| | | let columns = toRaw(unref(getColumnsRef)); |
| | | if (ignoreIndex) { |
| | | columns = columns.filter((item) => item.flag !== 'INDEX'); |
| | | } |
| | | if (ignoreAction) { |
| | | columns = columns.filter((item) => item.flag !== 'ACTION'); |
| | | } |
| | | return columns; |
| | | }, |
| | |
| | | setLoading: (loading: boolean) => { |
| | | loadingRef.value = loading; |
| | | }, |
| | | setProps: (props: Partial<BasicTableProps>) => { |
| | | innerPropsRef.value = deepMerge(unref(innerPropsRef) || {}, props); |
| | | setProps, |
| | | getSize: (): SizeType => { |
| | | return unref(getBindValues).size; |
| | | }, |
| | | }; |
| | | |
| | | provideTable(tableAction); |
| | | provideTable({ |
| | | ...tableAction, |
| | | wrapRef, |
| | | }); |
| | | |
| | | emit('register', tableAction); |
| | | return { |
| | |
| | | getEmptyDataIsShowTable, |
| | | handleTableChange, |
| | | getRowClassName, |
| | | wrapRef, |
| | | ...tableAction, |
| | | }; |
| | | }, |