wk-field-table.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <view class="wk-field-table">
  3. <view v-for="(item, index) in dataList" :key="index">
  4. <view class="wk-field__label">
  5. <!-- <view v-if="field.isNull === 1" class="line" /> -->
  6. <text class="title">
  7. {{ _label }}({{ index + 1 }})
  8. </text>
  9. <text
  10. v-if="dataList.length > 1"
  11. class="control"
  12. @click="handleDelete(index)">
  13. 删除
  14. </text>
  15. </view>
  16. <view class="wk-field__body">
  17. <wk-form
  18. v-if="showForm"
  19. ref="childForm"
  20. :fields="item"
  21. :is-detail-table="true"
  22. @change="(data) => handleValueChange(data, index)" />
  23. </view>
  24. </view>
  25. <view v-if="!field.disabled" class="wk-field__footer">
  26. <view @click="handleAdd">
  27. <text class="wk wk-plus add-icon" />
  28. <text>{{ field.remark || '' }}</text>
  29. </view>
  30. </view>
  31. <view v-if="field.disabled" class="mask" />
  32. <!-- <view class="mask"></view> -->
  33. </view>
  34. </template>
  35. <script>
  36. import mixins from './mixins'
  37. import { deepCopy } from '@/utils/lib.js'
  38. import formMixins from '@/mixins/formMixins.js'
  39. export default {
  40. name: 'WkFieldTable',
  41. components: {
  42. 'wk-form': () => import('./wk-form.vue')
  43. },
  44. mixins: [mixins, formMixins],
  45. data() {
  46. return {
  47. defaultVal: null,
  48. dataList: [],
  49. showForm: true
  50. }
  51. },
  52. mounted() {
  53. // console.log('this.field: ', this.field)
  54. this.defaultVal = deepCopy(this.field.fieldExtendList).map(o => {
  55. return {
  56. ...o,
  57. value: this.mixinsFormatFieldValue(o)
  58. }
  59. })
  60. if (this.$isEmpty(this.field.value)) {
  61. this.dataList.push(deepCopy(this.defaultVal))
  62. } else {
  63. // console.log('this.field: ', this.field)
  64. this.dataList = this.field.value.map(childArr => {
  65. // console.log('childArr', childArr)
  66. return childArr.map(o => {
  67. return {
  68. ...o,
  69. value: this.mixinsFormatFieldValue(o)
  70. }
  71. })
  72. })
  73. }
  74. },
  75. methods: {
  76. /**
  77. * 修改
  78. * @param {Object} data
  79. * @param {Object} index
  80. */
  81. handleValueChange(data, index) {
  82. console.log('value change: ', data, index)
  83. this.$refs.childForm[index].getForm(false, false).then(res => {
  84. this.dataList[index] = res.field
  85. console.log('dataList: ', this.dataList)
  86. this.$emit('input', this.dataList)
  87. this.$emit('change', {
  88. index: this.index,
  89. field: this.field,
  90. value: this.dataList
  91. })
  92. })
  93. },
  94. /**
  95. * 添加事项
  96. */
  97. handleAdd() {
  98. this.dataList.push(deepCopy(this.defaultVal))
  99. },
  100. /**
  101. * 删除事项
  102. * @param {Object} index
  103. */
  104. handleDelete(index) {
  105. this.showForm = false
  106. this.dataList.splice(index, 1)
  107. this.$nextTick(() => {
  108. this.showForm = true
  109. })
  110. }
  111. }
  112. }
  113. </script>
  114. <style scoped lang="scss">
  115. .wk-field-table {
  116. position: relative;
  117. .mask {
  118. position: absolute;
  119. top: 0;
  120. left: 0;
  121. width: 100%;
  122. height: 100%;
  123. z-index: 100;
  124. background-color: transparent;
  125. }
  126. .wk-field__label {
  127. font-size: 28rpx;
  128. color: $gray;
  129. padding: 18rpx 30rpx;
  130. background-color: #F3F5FA;
  131. margin: 0 -30rpx;
  132. @include left;
  133. .title {
  134. flex: 1;
  135. }
  136. .control {
  137. color: $theme-color;
  138. }
  139. }
  140. .wk-field__body {
  141. margin: 0 -32rpx;
  142. }
  143. .wk-field__footer {
  144. height: 80rpx;
  145. font-size: 26rpx;
  146. color: $theme-color;
  147. background-color: #F3F5FA;
  148. margin: 0 -30rpx;
  149. @include center;
  150. .add-icon {
  151. font-size: 28rpx;
  152. color: $theme-color;
  153. margin-right: 15rpx;
  154. }
  155. }
  156. }
  157. </style>