customTable.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. <!-- 搜索表单 -->
  2. <template>
  3. <div style="margin-top: 10px">
  4. <div v-if="isSelecting" class="selection-box" :style="selectionStyle"></div>
  5. <el-button type="primary" @click="addColumn()" v-if="edit"
  6. >新增列</el-button
  7. >
  8. <el-button type="primary" @click="addRow(columns[0].length)" v-if="edit"
  9. >新增行</el-button
  10. >
  11. <el-checkbox v-model="isMerge" style="margin-left: 10px" v-if="edit"
  12. >合并单元格</el-checkbox
  13. >
  14. <div class="table" style="margin-top: 10px" :id="id">
  15. <div
  16. class="table-body"
  17. style="display: flex"
  18. @mousedown="startSelecting"
  19. @mousemove="updateSelection"
  20. @mouseup="stopSelecting"
  21. >
  22. <!-- <el-popover
  23. style="position: fixed; z-index: 2000"
  24. width="400"
  25. ref="popoverRef"
  26. v-model="rightClickShow"
  27. >
  28. <el-card class="box-card">
  29. <div slot="header" class="clearfix" @click="rightClickShow = false">
  30. <span>合并单元格</span>
  31. <i class="el-icon-close" style="float: right; padding: 3px 0"></i>
  32. </div>
  33. <div class="text item">
  34. 向前合并:
  35. <el-input v-model="beforeNum" style="width: 100px" type="number">
  36. </el-input>
  37. </div>
  38. <div class="text item" style="margin-top: 10px">
  39. 向后合并:
  40. <el-input v-model="laterNum" style="width: 100px" type="number">
  41. </el-input>
  42. </div>
  43. <div>
  44. <el-button
  45. style="float: right; margin-bottom: 5px"
  46. type="primary"
  47. @click="save"
  48. >确认</el-button
  49. >
  50. </div>
  51. </el-card>
  52. </el-popover> -->
  53. <template v-for="(row, index) in columns">
  54. <div class="column" :style="{ width: row[0].width + 'px' }">
  55. <div
  56. class="table-body-item tableTd"
  57. v-for="(item, rowIndex) in row"
  58. :style="{
  59. height: item.rowspan * 30 + 'px',
  60. display: item.rowspan ? 'block' : 'none',
  61. ...item.style,
  62. width: item.colspan ? getWidth(item) : 0
  63. }"
  64. @contextmenu.prevent="onRightClick($event, rowIndex, index)"
  65. >
  66. <i
  67. class="el-icon-delete delete"
  68. style="display: none"
  69. @click="removeColumn(item, index)"
  70. v-if="edit && rowIndex == 0"
  71. ></i>
  72. <i
  73. class="el-icon-circle-plus-outline add"
  74. style="display: none"
  75. @click="addColumn(item)"
  76. v-if="edit && rowIndex == 0"
  77. ></i>
  78. <i
  79. class="el-icon-delete deleteRow"
  80. @click="removeRow(rowIndex)"
  81. v-if="edit && rowIndex != 0 && item.style?.border != 'none'"
  82. ></i>
  83. <i
  84. class="el-icon-circle-plus-outline addRow"
  85. style="display: none"
  86. @click="addRow(rowIndex, item)"
  87. v-if="edit && rowIndex != 0 && index != columns.length - 1"
  88. ></i>
  89. <textarea
  90. v-if="item.rowspan > 1"
  91. v-model="item.value"
  92. class="templateInput"
  93. :id="item.id"
  94. :ref="item.id + 'ref'"
  95. :readonly="item.readonly == 2 || readonly"
  96. @click="inputClick(item)"
  97. @input="calculation"
  98. autocomplete="off"
  99. style="resize: none"
  100. ></textarea>
  101. <input
  102. v-else
  103. v-model="item.value"
  104. class="templateInput"
  105. :id="item.id"
  106. :ref="item.id + 'ref'"
  107. :readonly="item.readonly == 2 || readonly"
  108. @click="inputClick(item, rowIndex == 0 ? 'columns' : null)"
  109. @input="calculation"
  110. type="text"
  111. autocomplete="off"
  112. />
  113. </div>
  114. </div>
  115. </template>
  116. </div>
  117. </div>
  118. </div>
  119. </template>
  120. <script>
  121. import dictMixins from '@/mixins/dictMixins';
  122. import { generateRandomString } from '@/utils/util';
  123. export default {
  124. props: {},
  125. mixins: [dictMixins],
  126. props: {
  127. id: '',
  128. edit: {
  129. default: true,
  130. type: Boolean
  131. },
  132. readonly: {
  133. default: false,
  134. type: Boolean
  135. }
  136. },
  137. data() {
  138. return {
  139. form: null,
  140. valueObj: {},
  141. domId: '',
  142. rightClickShow: false,
  143. columns: [],
  144. units: {},
  145. equation: {},
  146. beforeNum: 0,
  147. laterNum: 0,
  148. currentRowIndex: 0,
  149. currentColumnIndex: 0,
  150. isSelecting: false,
  151. startX: 0,
  152. startY: 0,
  153. endX: 0,
  154. endY: 0,
  155. selectedItems: [],
  156. isMerge: false
  157. };
  158. },
  159. created() {},
  160. computed: {
  161. selectionStyle() {
  162. return {
  163. position: 'fixed',
  164. left: `${Math.min(this.startX, this.endX)}px`,
  165. top: `${Math.min(this.startY, this.endY)}px`,
  166. width: `${Math.abs(this.endX - this.startX)}px`,
  167. height: `${Math.abs(this.endY - this.startY)}px`,
  168. border: '2px dashed black'
  169. };
  170. }
  171. },
  172. methods: {
  173. startSelecting(event) {
  174. if (!this.isMerge) {
  175. return;
  176. }
  177. this.isSelecting = true;
  178. this.startX = event.clientX;
  179. this.startY = event.clientY;
  180. this.endX = this.startX;
  181. this.endY = this.startY;
  182. },
  183. updateSelection(event) {
  184. if (this.isSelecting) {
  185. this.endX = event.clientX;
  186. this.endY = event.clientY;
  187. }
  188. },
  189. stopSelecting() {
  190. this.isSelecting = false;
  191. // 在这里处理框选结果,例如计算被框选的元素
  192. this.calculateSelectedItems();
  193. },
  194. calculateSelectedItems() {
  195. this.selectedItems = [];
  196. this.columns.forEach((cell, columnIndex) => {
  197. cell.forEach((row, rowIndex) => {
  198. const rect = this.$refs[row.id + 'ref'][0].getBoundingClientRect();
  199. if (
  200. rect.left < this.endX &&
  201. rect.right > this.startX &&
  202. rect.top < this.endY &&
  203. rect.bottom > this.startY
  204. ) {
  205. row.columnIndex = columnIndex;
  206. row.rowIndex = rowIndex;
  207. this.selectedItems.push(row);
  208. }
  209. });
  210. });
  211. // console.log(this.selectedItems, 'this.selectedItems[0]');
  212. // return
  213. if (this.selectedItems.length < 1) {
  214. return;
  215. }
  216. let _columnIndex = this.selectedItems[0].columnIndex;
  217. let _rowIndex = this.selectedItems[0].rowIndex;
  218. let _colspan = this.selectedItems[0].colspan;
  219. let rowspan = this.selectedItems
  220. .filter((item) => item.columnIndex === _columnIndex)
  221. .reduce((acc, cur) => acc + cur.rowspan, 0);
  222. if (this.verify(_colspan, rowspan, this.selectedItems, _rowIndex)) {
  223. return;
  224. }
  225. for (let index = 0; index < this.selectedItems.length; index++) {
  226. let item = this.selectedItems[index];
  227. if (index != 0) {
  228. if (!this.selectedItems[0].style) {
  229. this.selectedItems[0].style = {};
  230. }
  231. if (
  232. this.selectedItems[0].rowIndex == item.rowIndex &&
  233. this.selectedItems[0].columnIndex != item.columnIndex
  234. ) {
  235. this.selectedItems[0].colspan += item.colspan;
  236. this.selectedItems[0].colspanKey.push(item.id);
  237. this.selectedItems[0].colspanKey.push(...item.colspanKey);
  238. }
  239. if (
  240. this.selectedItems[0].rowIndex != item.rowIndex &&
  241. this.selectedItems[0].columnIndex == item.columnIndex
  242. ) {
  243. this.selectedItems[0].rowspan += item.rowspan;
  244. item.rowspan = 0;
  245. }
  246. item.colspan = 0;
  247. item.style = {
  248. border: 'none'
  249. };
  250. this.$set(this.columns[item.columnIndex], item.rowIndex, item);
  251. }
  252. }
  253. this.$set(this.columns[_columnIndex], _rowIndex, this.selectedItems[0]);
  254. },
  255. colspanSum(rowIndex) {
  256. return this.selectedItems.filter((item) => item.rowIndex == rowIndex);
  257. },
  258. verify(_colspan, _rowspan, arr, _rowIndex) {
  259. for (let index = 0; index < arr.length; index++) {
  260. let item = arr[index];
  261. if (_rowIndex === 0 && _rowIndex != item.rowIndex) {
  262. //表头不能和其他行合并
  263. return true;
  264. }
  265. if (
  266. item.rowIndex != this.selectedItems[0].rowIndex &&
  267. _colspan > this.colspanSum(item.rowIndex).length &&
  268. item.colspan != 0
  269. ) {
  270. return true;
  271. }
  272. if (item.rowspan > _rowspan) {
  273. return true;
  274. }
  275. }
  276. },
  277. // 方法:添加新列
  278. addColumn(item) {
  279. let length = this.columns[0]?.length || 1;
  280. if (item) {
  281. let _columnIndex = item.columnIndex;
  282. if (item.colspan > 1) {
  283. _columnIndex += Number(item.colspan - 1);
  284. }
  285. this.columns.splice(
  286. _columnIndex + 1,
  287. 0,
  288. Array(length)
  289. .fill(null)
  290. .map(() => this.getInput())
  291. );
  292. } else {
  293. this.columns.push(
  294. Array(length)
  295. .fill(null)
  296. .map(() => this.getInput())
  297. );
  298. }
  299. },
  300. getInput() {
  301. return {
  302. readonly: 1,
  303. value: '',
  304. rowspan: 1,
  305. colspan: 1,
  306. colspanKey: [],
  307. width: 100,
  308. style: { width: 100 },
  309. id: generateRandomString(5)
  310. };
  311. },
  312. getIndex(id) {
  313. let columnIndex, rowIndex;
  314. console.log(id);
  315. this.columns.forEach((cell, _columnIndex) => {
  316. let cellIndex = cell.findIndex((data) => data.id == id);
  317. if (cellIndex != '-1') {
  318. columnIndex = _columnIndex;
  319. rowIndex = cellIndex;
  320. }
  321. });
  322. return { columnIndex, rowIndex };
  323. },
  324. // 方法:删除指定列
  325. removeColumn(item, index) {
  326. this.columns[index].forEach((cell, rowIndex) => {
  327. if (cell.colspanKey.length) {
  328. //当前删除的列其他行有合并过单元格的处理
  329. let data = this.columns[index + 1][rowIndex];
  330. data.colspan = cell.colspan - 1;
  331. data.rowspan = cell.rowspan;
  332. data.style = cell.style;
  333. data.colspanKey = cell.colspanKey.filter((item) => item != data.id);
  334. if (data.rowspan > 1) {
  335. for (let j = 1; j < data.rowspan; j++) {
  336. this.$set(this.columns[index + 1][rowIndex + j], 'rowspan', 0);
  337. }
  338. }
  339. this.$set(this.columns[index + 1], rowIndex, data);
  340. }
  341. });
  342. if (item?.colspanKey.length) {
  343. item.colspanKey.forEach((id) => {
  344. let { columnIndex } = this.getIndex(id);
  345. console.log(columnIndex, 'columnIndex');
  346. this.columns[columnIndex].forEach((cell, rowIndex) => {
  347. if (cell.colspanKey.length) {
  348. //当前删除的列其他行有合并过单元格的处理
  349. let data = this.columns[columnIndex + 1][rowIndex];
  350. data.colspan = cell.colspan - 1;
  351. data.rowspan = cell.rowspan;
  352. data.style = cell.style;
  353. data.colspanKey = cell.colspanKey.filter(
  354. (item) => item != data.id
  355. );
  356. if (data.rowspan > 1) {
  357. for (let j = 1; j < data.rowspan; j++) {
  358. this.$set(
  359. this.columns[columnIndex + 1][rowIndex + j],
  360. 'rowspan',
  361. 0
  362. );
  363. }
  364. }
  365. this.$set(this.columns[columnIndex + 1], rowIndex, data);
  366. }
  367. });
  368. this.columns.splice(index + 1, 1);
  369. });
  370. }
  371. this.columns.splice(index, 1);
  372. //当前删除的列合并过单元格的列都删除
  373. },
  374. // 方法:添加新行
  375. addRow(rowIndex, row) {
  376. let _rowIndex = rowIndex;
  377. if (row?.rowspan > 1) {
  378. _rowIndex += Number(row?.rowspan - 1);
  379. }
  380. this.columns.forEach((item, index) => {
  381. this.columns[index].splice(_rowIndex + 1, 0, this.getInput());
  382. });
  383. // }
  384. },
  385. //找到真正需要改变rowspan的行
  386. getPreventRowspan(columnIndex, rowIndex) {
  387. let preventRowspan = null;
  388. this.columns[columnIndex].forEach((item, newRowIndex) => {
  389. if (newRowIndex < rowIndex && item.rowspan > 1) {
  390. //向上找到当前列合并过单元格的行
  391. preventRowspan = newRowIndex;
  392. }
  393. });
  394. return preventRowspan;
  395. },
  396. // 方法:删除指定行
  397. removeRow(rowIndex) {
  398. this.columns.forEach((item, columnIndex) => {
  399. if (item[rowIndex].rowspan == 0) {
  400. // 如果当前列,删除的这一行rowspan为0,则需要找到真正需要改变rowspan的行
  401. let preventRowspanIndex = this.getPreventRowspan(
  402. columnIndex,
  403. rowIndex
  404. );
  405. if (preventRowspanIndex) {
  406. this.$set(
  407. this.columns[columnIndex][preventRowspanIndex],
  408. 'rowspan',
  409. this.columns[columnIndex][preventRowspanIndex].rowspan - 1
  410. );
  411. }
  412. } else if (item[rowIndex].rowspan > 1) {
  413. //rowspan大于1,代表合并过单元格,需要吧值继承给下一行
  414. let data = item[rowIndex];
  415. data.rowspan--;
  416. this.$set(this.columns[columnIndex], [rowIndex + 1], data);
  417. }
  418. item.splice(rowIndex, 1);
  419. });
  420. },
  421. getWidth(item) {
  422. let width = Number(item.style.width) || 100;
  423. if (item.colspanKey.length) {
  424. this.columns.forEach((cell) => {
  425. cell.forEach((row) => {
  426. if (item.colspanKey.includes(row.id)) {
  427. width += Number(row.style.width || 100);
  428. }
  429. });
  430. });
  431. }
  432. return width + 'px';
  433. },
  434. calculation() {
  435. this.$emit('calculation');
  436. },
  437. equationValue({ domId, value }) {
  438. this.columns.forEach((item, index) => {
  439. let cellIndex = item.findIndex((cell) => cell.id == domId);
  440. if (cellIndex != '-1') {
  441. this.$set(this.columns[index][cellIndex], 'value', value);
  442. }
  443. });
  444. },
  445. getValue() {
  446. return {
  447. form: null,
  448. equation: this.equation,
  449. units: this.units,
  450. valueObj: {
  451. columns: this.columns
  452. }
  453. };
  454. },
  455. init({ form, valueObj, equation, units }) {
  456. this.form = form;
  457. this.columns = valueObj.columns;
  458. this.equation = equation || {};
  459. this.units = units || {};
  460. },
  461. editInputChange(domObj) {
  462. if (domObj.equation) {
  463. this.equation[this.domId] = domObj.equation;
  464. }
  465. if (domObj.units) {
  466. this.units[this.domId] = domObj.units;
  467. }
  468. let dom = document.getElementById(domObj.id);
  469. this.columns.forEach((item, index) => {
  470. let rowsIndex = item.findIndex((cells) => cells.id == this.domId);
  471. if (rowsIndex >= 0) {
  472. let width = domObj.width - dom.parentElement.offsetWidth;
  473. let newWidth = this.columns[index][rowsIndex].style.width + width;
  474. this.columns[index][0].width = newWidth;
  475. this.$set(this.columns[index], rowsIndex, domObj);
  476. item.forEach((cell, _index) => {
  477. this.$set(this.columns[index][_index].style, 'width', newWidth);
  478. this.$set(this.columns[index][_index], 'width', newWidth);
  479. });
  480. }
  481. });
  482. },
  483. onRightClick(PointerEvent, rowIndex, columnIndex) {
  484. this.currentRowIndex = rowIndex;
  485. this.currentColumnIndex = columnIndex;
  486. if (rowIndex === 0) {
  487. this.rightClickShow = true;
  488. this.$nextTick(() => {
  489. let y = PointerEvent.pageY;
  490. let x = PointerEvent.pageX + 10;
  491. if (PointerEvent.screenY >= PointerEvent.view.innerHeight) {
  492. y -= 80;
  493. }
  494. this.$refs.popoverRef.$el.style.top = y + 'px';
  495. this.$refs.popoverRef.$el.style.left = x + 'px';
  496. });
  497. }
  498. },
  499. inputClick(item, type) {
  500. if (!this.edit) {
  501. return;
  502. }
  503. let dom = document.getElementById(item.id);
  504. this.domId = item.id;
  505. this.$emit('editShow', {
  506. templateDivRef: 'customTextRef' + this.id,
  507. domObj: {
  508. ...item,
  509. width: dom.parentElement.offsetWidth,
  510. isNoWidth: type != 'columns' ? false : true,
  511. equation: this.equation[item.id],
  512. units: this.units[item.id] || {}
  513. }
  514. });
  515. }
  516. }
  517. };
  518. </script>
  519. <style lang="scss" scoped>
  520. :deep(.templateInput) {
  521. width: 100%;
  522. height: 100%;
  523. border: none;
  524. text-align: center;
  525. background-color: #fff;
  526. }
  527. .table-header {
  528. span {
  529. display: inline-block;
  530. height: 30px;
  531. border: 1px solid #ddd;
  532. width: 100px;
  533. background-color: #f2f2f2 !important;
  534. }
  535. input {
  536. background-color: #f2f2f2 !important;
  537. }
  538. }
  539. .table-body {
  540. white-space: nowrap;
  541. .column {
  542. display: inline-block;
  543. > div {
  544. height: 30px;
  545. border: 1px solid #ddd;
  546. width: 100%;
  547. }
  548. > div:nth-of-type(1) {
  549. background-color: #f2f2f2 !important;
  550. > input {
  551. background-color: #f2f2f2 !important;
  552. }
  553. }
  554. }
  555. }
  556. // th,
  557. // td {
  558. // border: 1px solid #ddd;
  559. // padding: 2px;
  560. // text-align: center;
  561. // height: 30px;
  562. // }
  563. // th {
  564. // background-color: #f2f2f2;
  565. // > input {
  566. // background-color: #f2f2f2 !important;
  567. // }
  568. // }
  569. tr:nth-child(even) {
  570. background-color: #f9f9f9;
  571. }
  572. input:focus {
  573. border-color: #66afe9; /* 改变边框颜色 */
  574. outline: none; /* 移除默认的轮廓线 */
  575. }
  576. .tableTd {
  577. position: relative;
  578. }
  579. .deleteRow {
  580. display: block !important;
  581. position: absolute;
  582. bottom: 0px;
  583. right: -15px;
  584. color: #f56c6c;
  585. }
  586. .tableTd:hover {
  587. .delete {
  588. display: block !important;
  589. position: absolute;
  590. right: 0;
  591. top: 0;
  592. color: #f56c6c;
  593. }
  594. .add {
  595. display: block !important;
  596. position: absolute;
  597. right: 20px;
  598. top: 0;
  599. color: #409eff;
  600. }
  601. .addRow {
  602. display: block !important;
  603. position: absolute;
  604. bottom: 0px;
  605. right: 10px;
  606. color: #409eff;
  607. }
  608. }
  609. .selection-box {
  610. position: absolute;
  611. border: 2px dashed black;
  612. z-index: 999;
  613. pointer-events: none; /* 确保选择框不会干扰鼠标事件 */
  614. }
  615. </style>