BasicTable.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <template>
  2. <div ref="wrapRef" :class="getWrapperClass">
  3. <BasicForm
  4. submitOnReset
  5. v-bind="getFormProps"
  6. v-if="getBindValues.useSearchForm"
  7. :tableAction="tableAction"
  8. @register="registerForm"
  9. @submit="handleSearchInfoChange"
  10. @advanced-change="redoHeight"
  11. >
  12. <template #[replaceFormSlotKey(item)]="data" v-for="item in getFormSlotKeys">
  13. <slot :name="item" v-bind="data || {}"></slot>
  14. </template>
  15. </BasicForm>
  16. <Table
  17. ref="tableElRef"
  18. v-bind="getBindValues"
  19. :rowClassName="getRowClassName"
  20. v-show="getEmptyDataIsShowTable"
  21. @change="handleTableChange"
  22. >
  23. <template #[item]="data" v-for="item in Object.keys($slots)" :key="item">
  24. <slot :name="item" v-bind="data || {}"></slot>
  25. </template>
  26. <template #[`header-${column.dataIndex}`] v-for="column in columns" :key="column.dataIndex">
  27. <HeaderCell :column="column" />
  28. </template>
  29. </Table>
  30. </div>
  31. </template>
  32. <script lang="ts">
  33. import type {
  34. BasicTableProps,
  35. TableActionType,
  36. SizeType,
  37. ColumnChangeParam,
  38. } from './types/table';
  39. import { defineComponent, ref, computed, unref, toRaw, inject, watchEffect } from 'vue';
  40. import { Table } from 'ant-design-vue';
  41. import { BasicForm, useForm } from '/@/components/Form/index';
  42. import { PageWrapperFixedHeightKey } from '/@/components/Page';
  43. import HeaderCell from './components/HeaderCell.vue';
  44. import { InnerHandlers } from './types/table';
  45. import { usePagination } from './hooks/usePagination';
  46. import { useColumns } from './hooks/useColumns';
  47. import { useDataSource } from './hooks/useDataSource';
  48. import { useLoading } from './hooks/useLoading';
  49. import { useRowSelection } from './hooks/useRowSelection';
  50. import { useTableScroll } from './hooks/useTableScroll';
  51. import { useTableScrollTo } from './hooks/useTableScrollTo';
  52. import { useCustomRow } from './hooks/useCustomRow';
  53. import { useTableStyle } from './hooks/useTableStyle';
  54. import { useTableHeader } from './hooks/useTableHeader';
  55. import { useTableExpand } from './hooks/useTableExpand';
  56. import { createTableContext } from './hooks/useTableContext';
  57. import { useTableFooter } from './hooks/useTableFooter';
  58. import { useTableForm } from './hooks/useTableForm';
  59. import { useDesign } from '/@/hooks/web/useDesign';
  60. import { omit } from 'lodash-es';
  61. import { basicProps } from './props';
  62. import { isFunction } from '/@/utils/is';
  63. import { warn } from '/@/utils/log';
  64. export default defineComponent({
  65. components: {
  66. Table,
  67. BasicForm,
  68. HeaderCell,
  69. },
  70. props: basicProps,
  71. emits: [
  72. 'fetch-success',
  73. 'fetch-error',
  74. 'selection-change',
  75. 'register',
  76. 'row-click',
  77. 'row-dbClick',
  78. 'row-contextmenu',
  79. 'row-mouseenter',
  80. 'row-mouseleave',
  81. 'edit-end',
  82. 'edit-cancel',
  83. 'edit-row-end',
  84. 'edit-change',
  85. 'expanded-rows-change',
  86. 'change',
  87. 'columns-change',
  88. ],
  89. setup(props, { attrs, emit, slots, expose }) {
  90. const tableElRef = ref(null);
  91. const tableData = ref<Recordable[]>([]);
  92. const wrapRef = ref(null);
  93. const innerPropsRef = ref<Partial<BasicTableProps>>();
  94. const { prefixCls } = useDesign('basic-table');
  95. const [registerForm, formActions] = useForm();
  96. const getProps = computed(() => {
  97. return { ...props, ...unref(innerPropsRef) } as BasicTableProps;
  98. });
  99. const isFixedHeightPage = inject(PageWrapperFixedHeightKey, false);
  100. watchEffect(() => {
  101. unref(isFixedHeightPage) &&
  102. props.canResize &&
  103. warn(
  104. "'canResize' of BasicTable may not work in PageWrapper with 'fixedHeight' (especially in hot updates)",
  105. );
  106. });
  107. const { getLoading, setLoading } = useLoading(getProps);
  108. const {
  109. getPaginationInfo,
  110. getPagination,
  111. setPagination,
  112. setShowPagination,
  113. getShowPagination,
  114. } = usePagination(getProps);
  115. const {
  116. getRowSelection,
  117. getRowSelectionRef,
  118. getSelectRows,
  119. clearSelectedRowKeys,
  120. getSelectRowKeys,
  121. deleteSelectRowByKey,
  122. setSelectedRowKeys,
  123. } = useRowSelection(getProps, tableData, emit);
  124. const {
  125. handleTableChange: onTableChange,
  126. getDataSourceRef,
  127. getDataSource,
  128. getRawDataSource,
  129. setTableData,
  130. updateTableDataRecord,
  131. deleteTableDataRecord,
  132. insertTableDataRecord,
  133. findTableDataRecord,
  134. fetch,
  135. getRowKey,
  136. reload,
  137. getAutoCreateKey,
  138. updateTableData,
  139. } = useDataSource(
  140. getProps,
  141. {
  142. tableData,
  143. getPaginationInfo,
  144. setLoading,
  145. setPagination,
  146. getFieldsValue: formActions.getFieldsValue,
  147. clearSelectedRowKeys,
  148. },
  149. emit,
  150. );
  151. function handleTableChange(...args) {
  152. onTableChange.call(undefined, ...args);
  153. emit('change', ...args);
  154. // 解决通过useTable注册onChange时不起作用的问题
  155. const { onChange } = unref(getProps);
  156. onChange && isFunction(onChange) && onChange.call(undefined, ...args);
  157. }
  158. const {
  159. getViewColumns,
  160. getColumns,
  161. setCacheColumnsByField,
  162. setColumns,
  163. getColumnsRef,
  164. getCacheColumns,
  165. } = useColumns(getProps, getPaginationInfo);
  166. const { getScrollRef, redoHeight } = useTableScroll(
  167. getProps,
  168. tableElRef,
  169. getColumnsRef,
  170. getRowSelectionRef,
  171. getDataSourceRef,
  172. );
  173. const { scrollTo } = useTableScrollTo(tableElRef, getDataSourceRef);
  174. const { customRow } = useCustomRow(getProps, {
  175. setSelectedRowKeys,
  176. getSelectRowKeys,
  177. clearSelectedRowKeys,
  178. getAutoCreateKey,
  179. emit,
  180. });
  181. const { getRowClassName } = useTableStyle(getProps, prefixCls);
  182. const { getExpandOption, expandAll, collapseAll } = useTableExpand(getProps, tableData, emit);
  183. const handlers: InnerHandlers = {
  184. onColumnsChange: (data: ColumnChangeParam[]) => {
  185. emit('columns-change', data);
  186. // support useTable
  187. unref(getProps).onColumnsChange?.(data);
  188. },
  189. };
  190. const { getHeaderProps } = useTableHeader(getProps, slots, handlers);
  191. const { getFooterProps } = useTableFooter(
  192. getProps,
  193. getScrollRef,
  194. tableElRef,
  195. getDataSourceRef,
  196. );
  197. const { getFormProps, replaceFormSlotKey, getFormSlotKeys, handleSearchInfoChange } =
  198. useTableForm(getProps, slots, fetch, getLoading);
  199. const getBindValues = computed(() => {
  200. const dataSource = unref(getDataSourceRef);
  201. let propsData: Recordable = {
  202. ...attrs,
  203. customRow,
  204. ...unref(getProps),
  205. ...unref(getHeaderProps),
  206. scroll: unref(getScrollRef),
  207. loading: unref(getLoading),
  208. tableLayout: 'fixed',
  209. rowSelection: unref(getRowSelectionRef),
  210. rowKey: unref(getRowKey),
  211. columns: toRaw(unref(getViewColumns)),
  212. pagination: toRaw(unref(getPaginationInfo)),
  213. dataSource,
  214. footer: unref(getFooterProps),
  215. ...unref(getExpandOption),
  216. };
  217. if (slots.expandedRowRender) {
  218. propsData = omit(propsData, 'scroll');
  219. }
  220. propsData = omit(propsData, ['class', 'onChange']);
  221. return propsData;
  222. });
  223. const getWrapperClass = computed(() => {
  224. const values = unref(getBindValues);
  225. return [
  226. prefixCls,
  227. attrs.class,
  228. {
  229. [`${prefixCls}-form-container`]: values.useSearchForm,
  230. [`${prefixCls}--inset`]: values.inset,
  231. },
  232. ];
  233. });
  234. const getEmptyDataIsShowTable = computed(() => {
  235. const { emptyDataIsShowTable, useSearchForm } = unref(getProps);
  236. if (emptyDataIsShowTable || !useSearchForm) {
  237. return true;
  238. }
  239. return !!unref(getDataSourceRef).length;
  240. });
  241. function setProps(props: Partial<BasicTableProps>) {
  242. innerPropsRef.value = { ...unref(innerPropsRef), ...props };
  243. }
  244. const tableAction: TableActionType = {
  245. reload,
  246. getSelectRows,
  247. clearSelectedRowKeys,
  248. getSelectRowKeys,
  249. deleteSelectRowByKey,
  250. setPagination,
  251. setTableData,
  252. updateTableDataRecord,
  253. deleteTableDataRecord,
  254. insertTableDataRecord,
  255. findTableDataRecord,
  256. redoHeight,
  257. setSelectedRowKeys,
  258. setColumns,
  259. setLoading,
  260. getDataSource,
  261. getRawDataSource,
  262. setProps,
  263. getRowSelection,
  264. getPaginationRef: getPagination,
  265. getColumns,
  266. getCacheColumns,
  267. emit,
  268. updateTableData,
  269. setShowPagination,
  270. getShowPagination,
  271. setCacheColumnsByField,
  272. expandAll,
  273. collapseAll,
  274. scrollTo,
  275. getSize: () => {
  276. return unref(getBindValues).size as SizeType;
  277. },
  278. };
  279. createTableContext({ ...tableAction, wrapRef, getBindValues });
  280. expose(tableAction);
  281. emit('register', tableAction, formActions);
  282. return {
  283. tableElRef,
  284. getBindValues,
  285. getLoading,
  286. registerForm,
  287. handleSearchInfoChange,
  288. getEmptyDataIsShowTable,
  289. handleTableChange,
  290. getRowClassName,
  291. wrapRef,
  292. tableAction,
  293. redoHeight,
  294. getFormProps: getFormProps as any,
  295. replaceFormSlotKey,
  296. getFormSlotKeys,
  297. getWrapperClass,
  298. columns: getViewColumns,
  299. };
  300. },
  301. });
  302. </script>
  303. <style lang="less">
  304. @border-color: #cecece4d;
  305. @prefix-cls: ~'@{namespace}-basic-table';
  306. [data-theme='dark'] {
  307. .ant-table-tbody > tr:hover.ant-table-row-selected > td,
  308. .ant-table-tbody > tr.ant-table-row-selected td {
  309. background-color: #262626;
  310. }
  311. }
  312. .@{prefix-cls} {
  313. max-width: 100%;
  314. &-row__striped {
  315. td {
  316. background-color: @app-content-background;
  317. }
  318. }
  319. &-form-container {
  320. padding: 16px;
  321. .ant-form {
  322. padding: 12px 10px 6px;
  323. margin-bottom: 16px;
  324. background-color: @component-background;
  325. border-radius: 2px;
  326. }
  327. }
  328. .ant-tag {
  329. margin-right: 0;
  330. }
  331. .ant-table-wrapper {
  332. padding: 6px;
  333. background-color: @component-background;
  334. border-radius: 2px;
  335. .ant-table-title {
  336. min-height: 40px;
  337. padding: 0 0 8px !important;
  338. }
  339. .ant-table.ant-table-bordered .ant-table-title {
  340. border: none !important;
  341. }
  342. }
  343. .ant-table {
  344. width: 100%;
  345. overflow-x: hidden;
  346. &-title {
  347. display: flex;
  348. padding: 8px 6px;
  349. border-bottom: none;
  350. justify-content: space-between;
  351. align-items: center;
  352. }
  353. //.ant-table-tbody > tr.ant-table-row-selected td {
  354. //background-color: fade(@primary-color, 8%) !important;
  355. //}
  356. }
  357. .ant-pagination {
  358. margin: 10px 0 0;
  359. }
  360. .ant-table-footer {
  361. padding: 0;
  362. .ant-table-wrapper {
  363. padding: 0;
  364. }
  365. table {
  366. border: none !important;
  367. }
  368. .ant-table-body {
  369. overflow-x: hidden !important;
  370. // overflow-y: scroll !important;
  371. }
  372. td {
  373. padding: 12px 8px;
  374. }
  375. }
  376. &--inset {
  377. .ant-table-wrapper {
  378. padding: 0;
  379. }
  380. }
  381. }
  382. </style>