TableModal.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <template>
  2. <BasicModal
  3. v-bind="$attrs"
  4. @register="registerModal"
  5. showFooter
  6. :title="getTitle"
  7. @ok="handleSubmit"
  8. >
  9. <Divider orientation="left" style="font-size: 14px">数据表信息</Divider>
  10. <BasicForm @register="registerForm" />
  11. <template v-if="!isUpdate">
  12. <Divider orientation="left" style="font-size: 14px">数据列信息</Divider>
  13. <BasicTable @register="registerEditColumn">
  14. <template #action="{ record, column }">
  15. <TableAction :actions="createActions(record, column)" />
  16. </template>
  17. </BasicTable>
  18. <a-row style="padding: 6px">
  19. <a-col :span="6">
  20. <a-button preIcon="carbon:add" type="dashed" style="width: 100%" @click="addPrimaryColumn"
  21. >新增主键字段</a-button
  22. >
  23. </a-col>
  24. <a-col :span="6">
  25. <a-button preIcon="carbon:add" type="dashed" style="width: 100%" @click="addColumn"
  26. >新增普通字段</a-button
  27. >
  28. </a-col>
  29. <a-col :span="6">
  30. <a-button preIcon="carbon:add" type="dashed" style="width: 100%" @click="addTenantColumn"
  31. >新增租户字段</a-button
  32. >
  33. </a-col>
  34. <a-col :span="6">
  35. <a-button preIcon="carbon:add" type="dashed" style="width: 100%" @click="addBaseColumn"
  36. >新增基础字段</a-button
  37. >
  38. </a-col>
  39. </a-row>
  40. </template>
  41. </BasicModal>
  42. </template>
  43. <script lang="ts">
  44. import { defineComponent, ref, computed, unref } from 'vue';
  45. import { Divider } from 'ant-design-vue';
  46. import { BasicForm, useForm } from '/@/components/Form/index';
  47. import { tableFormSchema, editColumn } from './database.data';
  48. import { BasicModal, useModalInner } from '/@/components/Modal';
  49. import {
  50. BasicTable,
  51. useTable,
  52. EditRecordRow,
  53. BasicColumn,
  54. ActionItem,
  55. TableAction,
  56. } from '/@/components/Table';
  57. import { addTable, updateTable } from '/@/api/sys/admin';
  58. export default defineComponent({
  59. name: 'TableModal',
  60. components: { BasicModal, BasicForm, BasicTable, TableAction, Divider },
  61. emits: ['success', 'register'],
  62. setup(_, { emit }) {
  63. const isUpdate = ref(true);
  64. const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
  65. labelWidth: 300,
  66. schemas: tableFormSchema,
  67. showActionButtonGroup: false,
  68. });
  69. const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  70. resetFields();
  71. setModalProps({ confirmLoading: false });
  72. isUpdate.value = !!data?.isUpdate;
  73. updateSchema([
  74. {
  75. field: 'oldName',
  76. ifShow: true,
  77. },
  78. ]);
  79. if (unref(isUpdate)) {
  80. setFieldsValue({
  81. oldName: data.record.name,
  82. ...data.record,
  83. });
  84. }
  85. });
  86. const getTitle = computed(() => (!unref(isUpdate) ? '新增表' : '编辑表'));
  87. var colIndex = 0;
  88. const [registerEditColumn, { getDataSource }] = useTable({
  89. columns: editColumn,
  90. showIndexColumn: false,
  91. bordered: false,
  92. pagination: false,
  93. maxHeight: 300,
  94. actionColumn: {
  95. width: 200,
  96. title: '操作',
  97. dataIndex: 'action',
  98. slots: { customRender: 'action' },
  99. },
  100. });
  101. function addPrimaryColumn() {
  102. const data = getDataSource();
  103. const addRow: EditRecordRow = {
  104. columnDescription: '主键Id',
  105. dataType: 'bigint',
  106. dbColumnName: 'Id',
  107. decimalDigits: 0,
  108. isIdentity: 0,
  109. isNullable: 0,
  110. isPrimarykey: 1,
  111. length: 0,
  112. key: colIndex,
  113. editable: true,
  114. isNew: true,
  115. };
  116. data.push(addRow);
  117. colIndex++;
  118. }
  119. function addColumn() {
  120. const data = getDataSource();
  121. const addRow: EditRecordRow = {
  122. columnDescription: '',
  123. dataType: '',
  124. dbColumnName: '',
  125. decimalDigits: 0,
  126. isIdentity: 0,
  127. isNullable: 1,
  128. isPrimarykey: 0,
  129. length: 0,
  130. key: colIndex,
  131. editable: true,
  132. isNew: true,
  133. };
  134. data.push(addRow);
  135. colIndex++;
  136. }
  137. function addTenantColumn() {
  138. const data = getDataSource();
  139. const addRow: EditRecordRow = {
  140. columnDescription: '租户Id',
  141. dataType: 'bigint',
  142. dbColumnName: 'TenantId',
  143. decimalDigits: 0,
  144. isIdentity: 0,
  145. isNullable: 1,
  146. isPrimarykey: 0,
  147. length: 0,
  148. key: colIndex,
  149. editable: true,
  150. isNew: true,
  151. };
  152. data.push(addRow);
  153. colIndex++;
  154. }
  155. function addBaseColumn() {
  156. const fileds = [
  157. {
  158. dataType: 'datetime',
  159. name: 'CreateTime',
  160. desc: '创建时间',
  161. },
  162. {
  163. dataType: 'datetime',
  164. name: 'UpdateTime',
  165. desc: '更新时间',
  166. },
  167. {
  168. dataType: 'bigint',
  169. name: 'CreateUserId',
  170. desc: '创建者Id',
  171. },
  172. {
  173. dataType: 'bigint',
  174. name: 'UpdateUserId',
  175. desc: '修改者Id',
  176. },
  177. {
  178. dataType: 'bit',
  179. name: 'IsDelete',
  180. desc: '软删除',
  181. isNullable: 0,
  182. },
  183. ];
  184. const data = getDataSource();
  185. fileds.forEach((m: any) => {
  186. data.push({
  187. columnDescription: m.desc,
  188. dataType: m.dataType,
  189. dbColumnName: m.name,
  190. decimalDigits: 0,
  191. isIdentity: 0,
  192. isNullable: m.isNullable === 0 ? 0 : 1,
  193. isPrimarykey: 0,
  194. length: m.length || 0,
  195. key: colIndex,
  196. editable: true,
  197. isNew: true,
  198. });
  199. colIndex++;
  200. });
  201. }
  202. function createActions(record: EditRecordRow, column: BasicColumn): ActionItem[] {
  203. return [
  204. {
  205. icon: 'fluent:delete-24-regular',
  206. color: 'error',
  207. label: '删除',
  208. onClick: handleColDelete.bind(null, record, column),
  209. },
  210. {
  211. icon: 'fluent:arrow-up-12-filled',
  212. label: '上移',
  213. onClick: handleColTop.bind(null, record, column),
  214. },
  215. {
  216. icon: 'fluent:arrow-down-12-filled',
  217. label: '下移',
  218. onClick: handleColDown.bind(null, record, column),
  219. },
  220. ];
  221. }
  222. function handleColDelete(record: EditRecordRow) {
  223. if (record.isNew) {
  224. const data = getDataSource();
  225. const index = data.findIndex((item) => item.key === record.key);
  226. data.splice(index, 1);
  227. }
  228. }
  229. function handleColTop(record: EditRecordRow) {
  230. if (record.isNew) {
  231. const data = getDataSource();
  232. const index = data.findIndex((item) => item.key === record.key);
  233. var data1 = ChangeExForArray(index, index - 1, data);
  234. return data1;
  235. }
  236. }
  237. function handleColDown(record: EditRecordRow) {
  238. if (record.isNew) {
  239. const data = getDataSource();
  240. const index = data.findIndex((item) => item.key === record.key);
  241. return ChangeExForArray(index, index + 1, data);
  242. }
  243. }
  244. function ChangeExForArray(index1, index2, array: EditRecordRow) {
  245. let temp = array[index1];
  246. array[index1] = array[index2];
  247. array[index2] = temp;
  248. return array;
  249. }
  250. async function handleSubmit() {
  251. try {
  252. const values = await validate();
  253. setModalProps({ confirmLoading: true });
  254. if (!unref(isUpdate)) {
  255. let tbData: any = [];
  256. getDataSource().forEach((item: any) => {
  257. tbData.push(item.editValueRefs);
  258. });
  259. const body: any = {
  260. dbColumnInfoList: tbData,
  261. ...values,
  262. };
  263. await addTable(body);
  264. } else {
  265. await updateTable(values);
  266. }
  267. closeModal();
  268. emit('success');
  269. } finally {
  270. setModalProps({ confirmLoading: false });
  271. }
  272. }
  273. return {
  274. registerModal,
  275. isUpdate,
  276. registerForm,
  277. registerEditColumn,
  278. getTitle,
  279. handleSubmit,
  280. addPrimaryColumn,
  281. addColumn,
  282. addTenantColumn,
  283. addBaseColumn,
  284. createActions,
  285. };
  286. },
  287. });
  288. </script>