BasicTable.vue 9.9 KB

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