customTable.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. }
  114. };
  115. </script>
  116. <style lang="scss" scoped>
  117. .custom-table-container {
  118. width: 100%;
  119. // overflow: hidden;
  120. .table-wrapper {
  121. width: 100%;
  122. // margin-top: 20rpx;
  123. .table {
  124. display: flex;
  125. min-width: 100%;
  126. .table-body {
  127. display: flex;
  128. .column {
  129. display: inline-block;
  130. .table-body-item {
  131. position: relative;
  132. display: flex;
  133. align-items: center;
  134. justify-content: center;
  135. border: 1rpx solid #ddd;
  136. background-color: #fff;
  137. overflow: hidden;
  138. &.is-header {
  139. // background-color: #f2f2f2;
  140. .template-input {
  141. // background-color: #f2f2f2;
  142. }
  143. }
  144. &.is-selected {
  145. // background-color: #e6f7ff;
  146. border-color: #1890ff;
  147. }
  148. &.is-readonly {
  149. background-color: #f9f9f9;
  150. }
  151. .cell-actions {
  152. position: absolute;
  153. top: 0;
  154. right: 0;
  155. display: none;
  156. z-index: 10;
  157. .action-btn {
  158. width: 40rpx;
  159. height: 40rpx;
  160. line-height: 40rpx;
  161. text-align: center;
  162. background-color: rgba(0, 0, 0, 0.5);
  163. color: #fff;
  164. border-radius: 50%;
  165. font-size: 28rpx;
  166. &.delete {
  167. color: #f56c6c;
  168. }
  169. &.add {
  170. color: #409eff;
  171. margin-left: 5rpx;
  172. }
  173. &.deleteRow {
  174. color: #f56c6c;
  175. }
  176. &.addRow {
  177. color: #409eff;
  178. margin-left: 5rpx;
  179. }
  180. }
  181. }
  182. &:active .cell-actions {
  183. display: flex;
  184. }
  185. .input-wrapper {
  186. width: 100%;
  187. height: 100%;
  188. display: flex;
  189. align-items: center;
  190. justify-content: center;
  191. }
  192. .template-input {
  193. width: 100%;
  194. height: 100%;
  195. border: none;
  196. text-align: center;
  197. background-color: transparent;
  198. font-size: 24rpx;
  199. padding: 0;
  200. line-height: 1.5;
  201. }
  202. textarea.template-input {
  203. padding: 10rpx;
  204. resize: none;
  205. }
  206. }
  207. }
  208. }
  209. }
  210. }
  211. .merge-tip {
  212. position: fixed;
  213. top: 50%;
  214. left: 50%;
  215. transform: translate(-50%, -50%);
  216. padding: 20rpx 40rpx;
  217. background-color: rgba(0, 0, 0, 0.7);
  218. color: #fff;
  219. border-radius: 8rpx;
  220. font-size: 28rpx;
  221. z-index: 1000;
  222. }
  223. .bottom-actions {
  224. position: fixed;
  225. bottom: 0;
  226. left: 0;
  227. right: 0;
  228. z-index: 999;
  229. .action-sheet {
  230. background-color: #fff;
  231. border-top: 1rpx solid #e0e0e0;
  232. .sheet-header {
  233. display: flex;
  234. justify-content: space-between;
  235. align-items: center;
  236. padding: 30rpx;
  237. border-bottom: 1rpx solid #e0e0e0;
  238. .title {
  239. font-size: 32rpx;
  240. font-weight: bold;
  241. color: #333;
  242. }
  243. .cancel {
  244. font-size: 28rpx;
  245. color: #999;
  246. }
  247. }
  248. .sheet-content {
  249. padding: 30rpx;
  250. .sheet-btn {
  251. width: 100%;
  252. height: 80rpx;
  253. line-height: 80rpx;
  254. text-align: center;
  255. border-radius: 8rpx;
  256. font-size: 32rpx;
  257. &.primary {
  258. background-color: #157a2c;
  259. color: #fff;
  260. &:active {
  261. background-color: #0f5e20;
  262. }
  263. }
  264. }
  265. }
  266. }
  267. }
  268. }
  269. </style>