customTable.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <!-- 搜索表单 -->
  2. <template>
  3. <div style="margin-top: 10px">
  4. <el-button type="primary" @click="addColumn" v-if="edit">新增列</el-button>
  5. <el-button type="primary" @click="addRow" v-if="edit">新增行</el-button>
  6. <table style="margin-top: 10px" :id="id">
  7. <thead>
  8. <tr>
  9. <th
  10. v-for="(item, index) in columns"
  11. class="tableTh"
  12. :style="{ width: item.width + 'px' }"
  13. >
  14. <i
  15. class="el-icon-delete delete"
  16. style="display: none"
  17. @click="removeColumn(index)"
  18. v-if="edit"
  19. ></i>
  20. <input
  21. v-model="item.value"
  22. type="text"
  23. class="templateInput"
  24. :id="item.id"
  25. :readonly="item.readonly == 2 || !edit"
  26. @click="inputClick(item, 'columns')"
  27. />
  28. </th>
  29. <th v-if="edit"></th>
  30. </tr>
  31. </thead>
  32. <tbody>
  33. <tr v-for="(row, rowIndex) in rows">
  34. <td v-for="item in row.cells"
  35. ><input
  36. v-model="item.value"
  37. type="text"
  38. class="templateInput"
  39. :id="item.id"
  40. :readonly="item.readonly == 2 || !edit"
  41. @click="inputClick(item)"
  42. @input="calculation"
  43. /></td>
  44. <td v-if="edit">
  45. <i
  46. class="sort-handle el-icon-delete delete"
  47. style="color: #f56c6c"
  48. @click="removeRow(rowIndex)"
  49. ></i
  50. ></td>
  51. </tr>
  52. </tbody>
  53. </table>
  54. </div>
  55. </template>
  56. <script>
  57. import dictMixins from '@/mixins/dictMixins';
  58. import { generateRandomString } from '@/utils/util';
  59. export default {
  60. props: {},
  61. mixins: [dictMixins],
  62. props: {
  63. id: '',
  64. edit: {
  65. default: true,
  66. type: Boolean
  67. }
  68. },
  69. data() {
  70. return {
  71. form: null,
  72. valueObj: {},
  73. domId: '',
  74. rightClickShow: false,
  75. columns: [],
  76. rows: [],
  77. equation: {}
  78. };
  79. },
  80. created() {},
  81. methods: {
  82. // 方法:添加新列
  83. addColumn() {
  84. this.columns.push({
  85. value: `列${this.columns.length + 1}`,
  86. id: generateRandomString(5),
  87. readonly: 1
  88. });
  89. this.rows.forEach((row) => {
  90. row.cells.push({
  91. value: '',
  92. readonly: 1,
  93. id: generateRandomString(5)
  94. });
  95. });
  96. },
  97. // 方法:删除指定列
  98. removeColumn(index) {
  99. this.columns.splice(index, 1);
  100. this.rows.forEach((row) => {
  101. row.cells.splice(index, 1);
  102. });
  103. },
  104. // 方法:添加新行
  105. addRow() {
  106. const newRow = {
  107. cells: Array(this.columns.length)
  108. .fill(null)
  109. .map(() => ({
  110. readonly: 1,
  111. value: '',
  112. id: generateRandomString(5)
  113. }))
  114. };
  115. this.rows.push(newRow);
  116. },
  117. // 方法:删除指定行
  118. removeRow(index) {
  119. this.rows.splice(index, 1);
  120. },
  121. objInit() {
  122. if (Object.keys(this.valueObj).length) {
  123. for (let key in this.valueObj) {
  124. this.$nextTick(() => {
  125. let dom = document.getElementById(key);
  126. dom.value = this.valueObj[key];
  127. });
  128. }
  129. }
  130. },
  131. calculation() {
  132. this.$emit('calculation');
  133. },
  134. equationValue({ domId, value }) {
  135. this.rows.forEach((item, index) => {
  136. let cellIndex = item.cells.findIndex((cell) => cell.id == domId);
  137. if (cellIndex != '-1') {
  138. this.$set(this.rows[index].cells[cellIndex], 'value', value);
  139. }
  140. });
  141. },
  142. getValue() {
  143. return {
  144. form: null,
  145. equation: this.equation,
  146. valueObj: {
  147. columns: this.columns,
  148. rows: this.rows
  149. }
  150. };
  151. },
  152. init({ form, valueObj, equation }) {
  153. this.form = form;
  154. this.columns = valueObj.columns;
  155. this.rows = valueObj.rows;
  156. this.equation = equation || {};
  157. },
  158. editInputChange(domObj) {
  159. if (domObj.equation) {
  160. this.equation[this.domId] = domObj.equation;
  161. }
  162. let columnsIndex = this.columns.findIndex(
  163. (item) => item.id == this.domId
  164. );
  165. if (columnsIndex >= 0) {
  166. this.$set(this.columns, columnsIndex, domObj);
  167. return;
  168. }
  169. this.rows.forEach((item, index) => {
  170. let rowsIndex = item.cells.findIndex(
  171. (cells) => cells.id == this.domId
  172. );
  173. if (rowsIndex >= 0) {
  174. this.$set(this.rows[index].cells, rowsIndex, domObj);
  175. }
  176. });
  177. },
  178. inputClick(item, type) {
  179. if (!this.edit) {
  180. return;
  181. }
  182. let dom = document.getElementById(item.id);
  183. this.domId = item.id;
  184. this.$emit('editShow', {
  185. templateDivRef: 'customTextRef' + this.id,
  186. domObj: {
  187. width: dom.parentElement.offsetWidth,
  188. isNoWidth: type == 'columns' ? false : true,
  189. id: item.id,
  190. readonly: item.readonly,
  191. value: item.value,
  192. equation: this.equation[item.id]
  193. }
  194. });
  195. }
  196. }
  197. };
  198. </script>
  199. <style lang="scss" scoped>
  200. :deep(.templateInput) {
  201. width: 100%;
  202. height: 100%;
  203. border: none;
  204. text-align: center;
  205. background-color: #fff;
  206. }
  207. table {
  208. width: 100%;
  209. border-collapse: collapse;
  210. }
  211. th,
  212. td {
  213. border: 1px solid #ddd;
  214. padding: 0;
  215. text-align: center;
  216. height: 30px;
  217. }
  218. th {
  219. background-color: #f2f2f2;
  220. }
  221. tr:nth-child(even) {
  222. background-color: #f9f9f9;
  223. }
  224. input:focus {
  225. border-color: #66afe9; /* 改变边框颜色 */
  226. outline: none; /* 移除默认的轮廓线 */
  227. }
  228. .tableTh:hover {
  229. position: relative;
  230. .delete {
  231. display: block !important;
  232. position: absolute;
  233. right: 0;
  234. top: 0;
  235. color: #f56c6c;
  236. }
  237. }
  238. </style>