BasicTable.vue 11 KB

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