customTable.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <!-- 移动端表格组件 -->
  2. <template>
  3. <view class="custom-table-container">
  4. <!-- 表格容器 -->
  5. <div class="table-wrapper" >
  6. <view class="table" :id="id">
  7. <view class="table-body">
  8. <template v-for="(row, index) in columns">
  9. <view class="column" :style="{ width: (row[0] && row[0].width) ? row[0].width + 'rpx' : '200rpx' }">
  10. <view class="table-body-item" v-for="(item, rowIndex) in row" :key="item.id" :style="{
  11. height: item.rowspan > 1 ? (item.rowspan * 60) + 'rpx' : '60rpx',
  12. display: item.rowspan ? 'flex' : 'none',
  13. ...item.style,
  14. width: item.colspan ? getWidth(item) : 'auto'
  15. }" :class="{
  16. 'is-header': rowIndex === 0,
  17. 'is-readonly': equation[item.id]
  18. }">
  19. <!-- 输入框 -->
  20. <view class="input-wrapper">
  21. <textarea v-if="item.rowspan > 1" v-model="item.value" class="template-input"
  22. :id="item.id" :ref="item.id + 'ref'" :readonly="item.readonly == 2 || readonly||equation[item.id]"
  23. @input="calculation" :auto-height="true" :maxlength="200" />
  24. <input v-else v-model="item.value" class="template-input" :id="item.id"
  25. :ref="item.id + 'ref'" :disabled="item.readonly == 2 || readonly||equation[item.id]"
  26. @input="calculation" type="text" />
  27. </view>
  28. </view>
  29. </view>
  30. </template>
  31. </view>
  32. </view>
  33. </div>
  34. </view>
  35. </template>
  36. <script>
  37. import dictMixins from '@/mixins/dictMixins';
  38. export default {
  39. mixins: [dictMixins],
  40. props: {
  41. id: {
  42. type: String,
  43. default: ''
  44. },
  45. readonly: {
  46. type: Boolean,
  47. default: false
  48. }
  49. },
  50. data() {
  51. return {
  52. form: null,
  53. valueObj: {},
  54. domId: '',
  55. columns: [],
  56. units: {},
  57. equation: {},
  58. };
  59. },
  60. computed: {
  61. },
  62. methods: {
  63. getWidth(item) {
  64. let width = Number(item.style.width);
  65. if (item.colspanKey.length) {
  66. this.columns.forEach(cell => {
  67. cell.forEach(row => {
  68. if (item.colspanKey.includes(row.id)) {
  69. width += Number(row.style.width);
  70. }
  71. });
  72. });
  73. }
  74. return width + 'rpx';
  75. },
  76. calculation() {
  77. this.$emit('calculation');
  78. },
  79. equationValue({
  80. domId,
  81. value
  82. }) {
  83. this.columns.forEach((item, index) => {
  84. let cellIndex = item.findIndex(cell => cell.id == domId);
  85. if (cellIndex != '-1') {
  86. this.$set(this.columns[index][cellIndex], 'value', value);
  87. }
  88. });
  89. },
  90. getValue() {
  91. return {
  92. form: null,
  93. equation: this.equation,
  94. units: this.units,
  95. valueObj: {
  96. columns: this.columns
  97. }
  98. };
  99. },
  100. init({
  101. form,
  102. valueObj,
  103. equation,
  104. units
  105. }) {
  106. this.form = form;
  107. this.columns = valueObj.columns;
  108. this.equation = equation || {};
  109. this.units = units || {};
  110. console.log(this.units);
  111. console.log(this.equation);
  112. },
  113. editInputChange(domObj) {
  114. let data = JSON.parse(JSON.stringify(domObj));
  115. if (data.equation) {
  116. this.equation[data.id] = data.equation;
  117. }
  118. if (data.units) {
  119. this.units[data.id] = data.units;
  120. }
  121. // 移动端简化处理
  122. this.columns.forEach((item, index) => {
  123. let rowsIndex = item.findIndex(cells => cells.id == this.domId);
  124. this.$set(this.columns[index], rowsIndex, data);
  125. rowsIndex = item.findIndex(cells => cells.id == data.id);
  126. if (rowsIndex >= 0) {
  127. let width = data.width;
  128. let newWidth = this.columns[index][rowsIndex].style.width;
  129. this.columns[index][0].width = newWidth;
  130. item.forEach((cell, _index) => {
  131. this.$set(this.columns[index][_index].style, 'width', newWidth);
  132. this.$set(this.columns[index][_index], 'width', newWidth);
  133. });
  134. }
  135. });
  136. },
  137. }
  138. };
  139. </script>
  140. <style lang="scss" scoped>
  141. .custom-table-container {
  142. width: 100%;
  143. // overflow: hidden;
  144. .table-wrapper {
  145. width: 100%;
  146. // margin-top: 20rpx;
  147. .table {
  148. display: flex;
  149. min-width: 100%;
  150. .table-body {
  151. display: flex;
  152. .column {
  153. display: inline-block;
  154. .table-body-item {
  155. position: relative;
  156. display: flex;
  157. align-items: center;
  158. justify-content: center;
  159. border: 1rpx solid #ddd;
  160. background-color: #fff;
  161. overflow: hidden;
  162. &.is-header {
  163. // background-color: #f2f2f2;
  164. .template-input {
  165. // background-color: #f2f2f2;
  166. }
  167. }
  168. &.is-selected {
  169. // background-color: #e6f7ff;
  170. border-color: #1890ff;
  171. }
  172. &.is-readonly {
  173. background-color: #f9f9f9;
  174. }
  175. .cell-actions {
  176. position: absolute;
  177. top: 0;
  178. right: 0;
  179. display: none;
  180. z-index: 10;
  181. .action-btn {
  182. width: 40rpx;
  183. height: 40rpx;
  184. line-height: 40rpx;
  185. text-align: center;
  186. background-color: rgba(0, 0, 0, 0.5);
  187. color: #fff;
  188. border-radius: 50%;
  189. font-size: 28rpx;
  190. &.delete {
  191. color: #f56c6c;
  192. }
  193. &.add {
  194. color: #409eff;
  195. margin-left: 5rpx;
  196. }
  197. &.deleteRow {
  198. color: #f56c6c;
  199. }
  200. &.addRow {
  201. color: #409eff;
  202. margin-left: 5rpx;
  203. }
  204. }
  205. }
  206. &:active .cell-actions {
  207. display: flex;
  208. }
  209. .input-wrapper {
  210. width: 100%;
  211. height: 100%;
  212. display: flex;
  213. align-items: center;
  214. justify-content: center;
  215. }
  216. .template-input {
  217. width: 100%;
  218. height: 100%;
  219. border: none;
  220. text-align: center;
  221. background-color: transparent;
  222. font-size: 24rpx;
  223. padding: 0;
  224. line-height: 1.5;
  225. }
  226. textarea.template-input {
  227. padding: 10rpx;
  228. resize: none;
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. .merge-tip {
  236. position: fixed;
  237. top: 50%;
  238. left: 50%;
  239. transform: translate(-50%, -50%);
  240. padding: 20rpx 40rpx;
  241. background-color: rgba(0, 0, 0, 0.7);
  242. color: #fff;
  243. border-radius: 8rpx;
  244. font-size: 28rpx;
  245. z-index: 1000;
  246. }
  247. .bottom-actions {
  248. position: fixed;
  249. bottom: 0;
  250. left: 0;
  251. right: 0;
  252. z-index: 999;
  253. .action-sheet {
  254. background-color: #fff;
  255. border-top: 1rpx solid #e0e0e0;
  256. .sheet-header {
  257. display: flex;
  258. justify-content: space-between;
  259. align-items: center;
  260. padding: 30rpx;
  261. border-bottom: 1rpx solid #e0e0e0;
  262. .title {
  263. font-size: 32rpx;
  264. font-weight: bold;
  265. color: #333;
  266. }
  267. .cancel {
  268. font-size: 28rpx;
  269. color: #999;
  270. }
  271. }
  272. .sheet-content {
  273. padding: 30rpx;
  274. .sheet-btn {
  275. width: 100%;
  276. height: 80rpx;
  277. line-height: 80rpx;
  278. text-align: center;
  279. border-radius: 8rpx;
  280. font-size: 32rpx;
  281. &.primary {
  282. background-color: #157a2c;
  283. color: #fff;
  284. &:active {
  285. background-color: #0f5e20;
  286. }
  287. }
  288. }
  289. }
  290. }
  291. }
  292. }
  293. </style>