batch.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. <!--
  2. * @Description: 动态表格 用于批量填入数据
  3. -->
  4. <template>
  5. <div>
  6. <a-form ref="dynamicValidateForm" :model="dynamicValidateForm">
  7. <a-table v-if="!isMobile"
  8. class="batch-table"
  9. :rowKey="(column) => column.key"
  10. :columns="columns"
  11. :dataSource="dynamicValidateForm.domains"
  12. bordered
  13. :pagination="pagination"
  14. @change="tablePaginationChange"
  15. :scroll="{x: 'max-content'}"
  16. >
  17. <template #headerCell="{ column }">
  18. <div>
  19. <span
  20. v-if="
  21. record.list.find((item) => item.model == column.dataIndex) &&
  22. record.list.find((item) => item.model == column.dataIndex).rules[0].required
  23. "
  24. style="color: #ff4d4f; margin-right: 4px"
  25. >*</span
  26. >
  27. <span>{{ column.title }}</span>
  28. </div>
  29. </template>
  30. <template #bodyCell="scope">
  31. <div v-if="scope.column.dataIndex === 'sequence_index_number'">
  32. {{ getSerialNumber(scope.index) }}
  33. </div>
  34. <div v-else-if="record.list.find((item) =>item.disableColumn&& item.disableColumn ==scope.column.dataIndex)&&
  35. record.list.find((item) =>Array.isArray(item.disableIndex)&&item.disableIndex.includes(getSerialNumber(scope.index)))
  36. ">
  37. <StFormModelItem
  38. :record="record.list.find((item) => item.model == scope.column.dataIndex)"
  39. :config="config"
  40. :parentDisabled="true"
  41. :index="record.list.findIndex((item) => item.model == scope.column.dataIndex)"
  42. :domains="dynamicValidateForm.domains"
  43. :dynamicData="dynamicData"
  44. :formData="formData"
  45. :formConfig="formConfig"
  46. :childTableFieldRecord="record.list"
  47. :rowIndex="scope.index"
  48. v-model:value="scope.record[record.list.find((item) => item.model == scope.column.dataIndex).model]"
  49. @input="handleInput"
  50. :tableScope="scope"
  51. :columns="columns"
  52. :pagination="pagination"
  53. :currentTableData="currentTableData"
  54. />
  55. </div>
  56. <div v-else-if="record.list.find((item) => item.model == scope.column.dataIndex)">
  57. <StFormModelItem
  58. :record="record.list.find((item) => item.model == scope.column.dataIndex)"
  59. :config="config"
  60. :parentDisabled="disabled"
  61. :index="record.list.findIndex((item) => item.model == scope.column.dataIndex)"
  62. :domains="dynamicValidateForm.domains"
  63. :dynamicData="dynamicData"
  64. :formData="formData"
  65. :formConfig="formConfig"
  66. :childTableFieldRecord="record.list"
  67. :rowIndex="scope.index"
  68. v-model:value="scope.record[record.list.find((item) => item.model == scope.column.dataIndex).model]"
  69. @input="handleInput"
  70. :tableScope="scope"
  71. :columns="columns"
  72. :pagination="pagination"
  73. :currentTableData="currentTableData"
  74. />
  75. </div>
  76. <div v-if="scope.column.dataIndex === 'dynamic-opr-button'">
  77. <a-button v-if="!disabled" type="dashed" @click="removeDomain(scope.record)" danger>
  78. <template #icon>
  79. <DeleteOutlined/>
  80. </template>
  81. </a-button>
  82. </div>
  83. </template>
  84. </a-table>
  85. <div v-if="isMobile">
  86. <a-card v-for="(item, rowIdx) in firstData" :key="rowIdx" style="margin-bottom: 10px"
  87. class="mobile-table">
  88. <div v-for="(col, colIdx) in columns" :key="colIdx" class="mobile-table-item">
  89. <div class="mobile-table-item-content" v-if="col.dataIndex === 'sequence_index_number'">
  90. <div>
  91. <span>
  92. {{ col.title }}
  93. </span>
  94. </div>
  95. <div>
  96. <a-tag>{{ rowIdx + 1 }}</a-tag>
  97. </div>
  98. </div>
  99. <div class="mobile-table-item-content" v-if="col.dataIndex === 'dynamic-opr-button'">
  100. <div class="mob-label">
  101. <span>
  102. {{ col.title }}
  103. </span>
  104. </div>
  105. <a-button v-if="!disabled" type="dashed" @click="removeDomain(item)" danger>
  106. <template #icon>
  107. <DeleteOutlined/>
  108. </template>
  109. </a-button>
  110. </div>
  111. <div class="mobile-table-item-content"
  112. v-if="['sequence_index_number', 'dynamic-opr-button'].indexOf(col.dataIndex) === -1">
  113. <div class="mob-label">
  114. <span
  115. v-if="
  116. record.list.find((item) => item.model == col.dataIndex) &&
  117. record.list.find((item) => item.model == col.dataIndex).rules[0].required
  118. "
  119. style="color: #ff4d4f; margin-right: 4px"
  120. >*</span
  121. >
  122. <span>
  123. {{ col.title }}
  124. </span>
  125. </div>
  126. <div class="mobile-tanle-form-item">
  127. <StFormModelItem
  128. :record="record.list.find((item) => item.model == col.dataIndex)"
  129. :config="config"
  130. :parentDisabled="disabled"
  131. :index="record.list.findIndex((item) => item.model == col.dataIndex)"
  132. :domains="dynamicValidateForm.domains"
  133. :dynamicData="dynamicData"
  134. :formData="formData"
  135. :formConfig="formConfig"
  136. :childTableFieldRecord="record.list"
  137. :rowIndex="rowIdx"
  138. v-model:value="item[record.list.find((item) => item.model == col.dataIndex).model]"
  139. @input="handleInput"
  140. :columns="columns"
  141. :pagination="pagination"
  142. :currentTableData="currentTableData"
  143. :colIndex="colIdx"
  144. />
  145. </div>
  146. </div>
  147. </div>
  148. </a-card>
  149. <a-button type="dashed" style="width: 100%"
  150. v-show="this.dynamicValidateForm.domains&&this.dynamicValidateForm.domains.length>1"
  151. @click="openChild=true">查看更多 (共有{{ this.dynamicValidateForm.domains.length }}条数据)
  152. </a-button>
  153. </div>
  154. <a-button type="dashed" :disabled="disabled" @click="addDomain" v-if="!disabled">
  155. <template #icon>
  156. <PlusOutlined/>
  157. </template>
  158. 增加
  159. </a-button>
  160. </a-form>
  161. <a-modal
  162. v-if="isMobile"
  163. v-model:open="openChild"
  164. :title="null"
  165. width="100%"
  166. wrapClassName="fullscreen-modal"
  167. :footer="null"
  168. style="top: 0; padding: 0"
  169. :closable="false"
  170. >
  171. <!-- 悬浮在顶部的按钮 -->
  172. <div class="floating-top-button">
  173. <a-button
  174. type="text"
  175. @click="openChild = false"
  176. style="color: #1890ff"
  177. >
  178. <template #icon>
  179. <LeftOutlined/> <!-- 返回图标 -->
  180. </template>
  181. 返回
  182. </a-button>
  183. </div>
  184. <div class="modal-content-wrapper">
  185. <div class="scrollable-content">
  186. <div v-if="isMobile">
  187. <a-card v-for="(item, rowIdx) in dynamicValidateForm.domains" :key="rowIdx" style="margin-bottom: 10px"
  188. class="mobile-table">
  189. <div v-for="(col, colIdx) in columns" :key="colIdx" class="mobile-table-item">
  190. <div class="mobile-table-item-content" v-if="col.dataIndex === 'sequence_index_number'">
  191. <div>
  192. <span>
  193. {{ col.title }}
  194. </span>
  195. </div>
  196. <div>
  197. <a-tag>{{ rowIdx + 1 }}</a-tag>
  198. </div>
  199. </div>
  200. <div class="mobile-table-item-content" v-if="col.dataIndex === 'dynamic-opr-button'">
  201. <div class="mob-label">
  202. <span>
  203. {{ col.title }}
  204. </span>
  205. </div>
  206. <a-button v-if="!disabled" type="dashed" @click="removeDomain(item)" danger>
  207. <template #icon>
  208. <DeleteOutlined/>
  209. </template>
  210. </a-button>
  211. </div>
  212. <div class="mobile-table-item-content"
  213. v-if="['sequence_index_number', 'dynamic-opr-button'].indexOf(col.dataIndex) === -1">
  214. <div class="mob-label">
  215. <span
  216. v-if="
  217. record.list.find((item) => item.model == col.dataIndex) &&
  218. record.list.find((item) => item.model == col.dataIndex).rules[0].required
  219. "
  220. style="color: #ff4d4f; margin-right: 4px"
  221. >*</span
  222. >
  223. <span>
  224. {{ col.title }}
  225. </span>
  226. </div>
  227. <div class="mobile-tanle-form-item">
  228. <StFormModelItem
  229. :record="record.list.find((item) => item.model == col.dataIndex)"
  230. :config="config"
  231. :parentDisabled="disabled"
  232. :index="record.list.findIndex((item) => item.model == col.dataIndex)"
  233. :domains="dynamicValidateForm.domains"
  234. :dynamicData="dynamicData"
  235. :formData="formData"
  236. :formConfig="formConfig"
  237. :childTableFieldRecord="record.list"
  238. :rowIndex="rowIdx"
  239. v-model:value="item[record.list.find((item) => item.model == col.dataIndex).model]"
  240. @input="handleInput"
  241. :columns="columns"
  242. :pagination="pagination"
  243. :currentTableData="currentTableData"
  244. :colIndex="colIdx"
  245. />
  246. </div>
  247. </div>
  248. </div>
  249. </a-card>
  250. </div>
  251. <a-button type="dashed" :disabled="disabled" @click="addDomain" v-if="!disabled">
  252. <template #icon>
  253. <PlusOutlined/>
  254. </template>
  255. 增加
  256. </a-button>
  257. </div>
  258. </div>
  259. </a-modal>
  260. </div>
  261. </template>
  262. <script>
  263. import StFormModelItem from './module/StFormModelItem.vue';
  264. import {v1 as uuidv1} from 'uuid';
  265. import {DeleteOutlined, CopyOutlined, PlusOutlined} from '@ant-design/icons-vue';
  266. import {isMobile} from '/@/utils';
  267. export default {
  268. name: 'StBatch',
  269. props: {
  270. record: {
  271. type: Object,
  272. },
  273. value: {},
  274. dynamicData: {},
  275. config: {},
  276. parentDisabled: {},
  277. formData: {},
  278. formConfig: {},
  279. },
  280. components: {
  281. StFormModelItem,
  282. DeleteOutlined,
  283. CopyOutlined,
  284. PlusOutlined,
  285. },
  286. watch: {
  287. value: {
  288. // value 需要深度监听及默认先执行handler函数
  289. handler(val) {
  290. this.dynamicValidateForm.domains = val || [];
  291. },
  292. immediate: true,
  293. deep: true,
  294. },
  295. },
  296. mounted() {
  297. this.initTable();
  298. },
  299. data() {
  300. return {
  301. top: 0,
  302. openChild: false,
  303. dynamicValidateForm: {
  304. domains: [],
  305. },
  306. page: 1, //初始页
  307. size: 5, //每页数据
  308. pagination: {
  309. current: 1, //分页的索引
  310. total: 0,
  311. pageSize: 10, //每页中显示10条数据
  312. size: 'small',
  313. pageSizeOptions: ['10', '20', '50', '100'], //每页中显示的数据
  314. showSizeChanger: true,
  315. showQuickJumper: false,
  316. showTotal: (total) => `共 ${total} 条数据`,
  317. },
  318. currentTableData: [],
  319. mobileTableData: [],
  320. mobilePage: {
  321. page: 1,
  322. size: 5
  323. },
  324. };
  325. },
  326. computed: {
  327. firstData() {
  328. if (this.dynamicValidateForm.domains.length == 0) {
  329. return []
  330. } else {
  331. return this.dynamicValidateForm.domains.slice(0, 1)
  332. }
  333. },
  334. listLength() {
  335. return this.record.list.filter((item) => !item.options.hidden).length;
  336. },
  337. columns() {
  338. const columns = [];
  339. if (!this.record.options.hideSequence) {
  340. columns.push({
  341. title: '序号',
  342. dataIndex: 'sequence_index_number',
  343. width: '80px',
  344. align: 'center',
  345. });
  346. }
  347. columns.push(
  348. ...this.record.list
  349. .filter((item) => !item.options.hidden)
  350. .map((item, index) => {
  351. return {
  352. title: item.label,
  353. dataIndex: item.model,
  354. width: index === this.record.list.length - 1 ? '' : (item.options.childWidth ? item.options.childWidth : '300px'),
  355. //customCell:item.customCell,//仅能修改控件底部的样式,没啥用
  356. };
  357. })
  358. );
  359. if (!this.disabled) {
  360. columns.push({
  361. title: '操作',
  362. dataIndex: 'dynamic-opr-button',
  363. fixed: 'right',
  364. width: '80px',
  365. align: 'center',
  366. });
  367. }
  368. return columns;
  369. },
  370. disabled() {
  371. return this.record.options.disabled || this.parentDisabled;
  372. },
  373. isMobile() {
  374. return isMobile();
  375. },
  376. },
  377. methods: {
  378. getSerialNumber(index) {
  379. const {current, pageSize} = this.pagination;
  380. return (current - 1) * pageSize + index + 1;
  381. },
  382. loadMoreHandle() {
  383. const start = (this.mobilePage.page) * this.mobilePage.size;
  384. const end = start + this.mobilePage.size;
  385. this.mobileTableData = this.dynamicValidateForm.domains.slice(0, end);
  386. this.mobilePage.page++;
  387. },
  388. initTable() {
  389. this.page = this.pagination.current;
  390. this.size = this.pagination.pageSize;
  391. this.refreshCurrentTableData();
  392. },
  393. initMobileTable() {
  394. this.mobilePage.page = 1;
  395. this.mobilePage.size = 5;
  396. this.mobileTableData = this.dynamicValidateForm.domains.length > this.mobilePage.size ? this.dynamicValidateForm.domains.slice(0, this.mobilePage.size) : this.dynamicValidateForm.domains;
  397. },
  398. getPageData(array, pageNumber, pageSize) {
  399. // 计算开始索引
  400. const start = (pageNumber - 1) * pageSize;
  401. // 计算结束索引
  402. const end = start + pageSize;
  403. // 返回该页的数据
  404. return array.slice(start, end);
  405. },
  406. tablePaginationChange(pagination) {
  407. this.pagination.current = pagination.current;
  408. this.pagination.pageSize = pagination.pageSize;
  409. this.page = pagination.current;
  410. this.size = pagination.pageSize;
  411. this.refreshCurrentTableData();
  412. this.pagination.total = pagination.total
  413. },
  414. validationSubform() {
  415. return new Promise((resolve, reject) => {
  416. this.$refs.dynamicValidateForm
  417. .validateFields()
  418. .then((res) => {
  419. resolve(res);
  420. })
  421. .catch((err) => {
  422. reject(err);
  423. });
  424. });
  425. },
  426. resetForm() {
  427. this.$refs.dynamicValidateForm.resetFields();
  428. },
  429. removeDomain(item) {
  430. const index = this.dynamicValidateForm.domains.indexOf(item);
  431. if (index !== -1) {
  432. this.dynamicValidateForm.domains.splice(index, 1);
  433. }
  434. this.refreshCurrentTableData();
  435. this.handleInput('DELETE');
  436. },
  437. addDomain() {
  438. const data = {};
  439. this.record.list.forEach((item) => {
  440. data[item.model] = item.options.defaultValue;
  441. });
  442. this.dynamicValidateForm.domains.push({
  443. ...data,
  444. key: uuidv1(),
  445. });
  446. this.refreshCurrentTableData();
  447. // 跳转至最后一页;
  448. if (this.dynamicValidateForm.domains.length % this.pagination.pageSize === 0) {
  449. this.gotoPageNum(Math.floor(this.dynamicValidateForm.domains.length / this.pagination.pageSize));
  450. } else {
  451. this.gotoPageNum(Math.floor(this.dynamicValidateForm.domains.length / this.pagination.pageSize) + 1);
  452. }
  453. this.handleInput('ADD');
  454. },
  455. handleInput(...args) {
  456. let params = {};
  457. if (args.length == 1 && !['ADD', 'DELETE'].includes(args[0])) {
  458. return false;
  459. }
  460. if (args.length == 4) {
  461. let [value, key, record, tableInfo] = args;
  462. tableInfo.optType = 'UPDATE';
  463. params = {value, key, record, tableInfo};
  464. } else {
  465. params = {
  466. value: this.dynamicValidateForm.domains,
  467. key: this.record.model,
  468. record: this.record,
  469. tableInfo: {optType: args[0]}
  470. };
  471. }
  472. this.$emit('update:value', this.dynamicValidateForm.domains);
  473. this.$emit('change', params.value, this.record.model, this.record, params.tableInfo);
  474. },
  475. refreshCurrentTableData() {
  476. this.currentTableData = this.getPageData(this.dynamicValidateForm.domains, this.pagination.current, this.pagination.pageSize);
  477. },
  478. // 跳转至某一页
  479. gotoPageNum(num) {
  480. if (num == this.pagination.current) return;
  481. this.pagination.current = num;
  482. }
  483. },
  484. };
  485. </script>
  486. <style scoped lang="less">
  487. .dynamic-opr-button {
  488. margin-left: 0px;
  489. }
  490. :deep(.css-dev-only-do-not-override-1r8ax8d).ant-table-wrapper .ant-table-thead > tr > th,
  491. :deep(.css-dev-only-do-not-override-1r8ax8d).ant-table-wrapper .ant-table-tbody > tr > td,
  492. :deep(.css-dev-only-do-not-override-1r8ax8d).ant-table-wrapper tfoot > tr > th,
  493. :deep(.css-dev-only-do-not-override-1r8ax8d).ant-table-wrapper tfoot > tr > td {
  494. padding: 5px 5px;
  495. }
  496. :deep(.css-dev-only-do-not-override-1r8ax8d).ant-table-wrapper .ant-table-pagination.ant-pagination {
  497. margin: 6px 0;
  498. }
  499. .dynamic-opr-button {
  500. cursor: pointer;
  501. position: relative;
  502. top: 4px;
  503. font-size: 16px;
  504. color: #999;
  505. transition: all 0.3s;
  506. margin-left: 6px;
  507. }
  508. .dynamic-opr-button:hover {
  509. color: #e89;
  510. }
  511. .dynamic-opr-button[disabled] {
  512. cursor: not-allowed;
  513. opacity: 0.5;
  514. }
  515. .batch-table {
  516. display: flex;
  517. flex-direction: column;
  518. :deep(.ant-form-item) {
  519. margin: 0;
  520. }
  521. }
  522. .mobile-table {
  523. :deep(.ant-card-body) {
  524. padding: 15px;
  525. }
  526. .mobile-table-item {
  527. width: 100%;
  528. .mobile-table-item-content {
  529. width: 100%;
  530. display: flex;
  531. justify-content: space-between;
  532. align-items: center;
  533. padding: 10px 0;
  534. border-bottom: 1px solid #f0f0f0;
  535. .mob-label {
  536. width: 100px;
  537. }
  538. .mobile-tanle-form-item {
  539. width: calc(100% - 100px);
  540. }
  541. }
  542. }
  543. }
  544. /* 全屏模态框样式 */
  545. :deep(.fullscreen-modal) {
  546. .ant-modal {
  547. max-width: 100%;
  548. width: 100% !important;
  549. margin: 0;
  550. top: 0;
  551. padding-bottom: 0;
  552. height: 100vh;
  553. .ant-modal-content {
  554. height: 100vh;
  555. display: flex;
  556. flex-direction: column;
  557. .ant-modal-body {
  558. padding: 0; /* 移除默认 padding */
  559. }
  560. }
  561. }
  562. }
  563. /* 滚动容器 */
  564. .modal-scroll-container {
  565. height: 100vh;
  566. display: flex;
  567. flex-direction: column;
  568. }
  569. /* 可滚动的内容区域 */
  570. .scrollable-content {
  571. flex: 1;
  572. overflow-y: auto; /* 允许内容滚动 */
  573. padding: 1px;
  574. }
  575. /* 悬浮在顶部的按钮容器 */
  576. .floating-top-button {
  577. position: sticky;
  578. top: 0;
  579. z-index: 1000;
  580. background: #fff;
  581. padding: 6px 0px;
  582. border-bottom: 1px solid #f0f0f0;
  583. display: flex;
  584. justify-content: flex-end; /* 按钮靠右 */
  585. }
  586. </style>