addForm.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <view class="p-form">
  3. <view v-for='item,index in extForm' class="p-form-item" :key="index">
  4. <view class="p-label ddflex" style="justify-content: space-between;">
  5. {{item.chineseName}}
  6. <view class="ddflex" style="font-size: 24rpx;">
  7. <text style="color: blue;" @click="editItem(index)">编辑</text>
  8. <text style="margin-left: 20rpx;color: red;" @click="removeItem(index)">删除</text>
  9. </view>
  10. </view>
  11. <!-- 输入框 -->
  12. <view v-if="item.type==1" class="p-input">
  13. <input :placeholder="'请输入'+item.chineseName" placeholder-class="p-placeholder" />
  14. </view>
  15. <!-- 单选 -->
  16. <view v-if="item.type==2" class="p-select">
  17. <view class="ddflex">
  18. <view class="ddflex p-select-item" v-for="it in item.selectValues">
  19. <image src="/static/images/select.png"></image>
  20. <view>{{it.name}}</view>
  21. </view>
  22. </view>
  23. </view>
  24. <!-- 多选 -->
  25. <view v-if="item.type==3" class="p-select-group">
  26. <view class="ddflex">
  27. <view class="ddflex p-select-group-item" v-for="it in item.selectValues">
  28. <view>{{it.name}}</view>
  29. </view>
  30. </view>
  31. </view>
  32. <!-- 图片 -->
  33. <view v-if="item.type==4">
  34. <view class="ddflex" style="margin-top: 30rpx;">
  35. <view class="upload-photo ddflex">
  36. <image src="../static/images/photo.png"></image>
  37. </view>
  38. </view>
  39. </view>
  40. <!-- 多图片 -->
  41. <view v-if="item.type==5">
  42. <view class="ddflex" style="margin-top: 30rpx;">
  43. <view class="upload-photo ddflex">
  44. <image src="../static/images/photo.png"></image>
  45. </view>
  46. </view>
  47. </view>
  48. </view>
  49. <view v-if="extForm.length==0" style="text-align: center;margin: 30rpx 0;color: #999;">添加报名字段</view>
  50. <view class="add-btn" @click="showPop">
  51. <image src="../static/images/add.png"></image>
  52. </view>
  53. <view class="next-btn" @click="save">
  54. <view>确定</view>
  55. </view>
  56. <view class="ceng" v-if="isShowPop" @click="hidePop()"></view>
  57. <view class="hx-pop" :style="'bottom:' + popBottom">
  58. <view class="pop-tit">添加报名信息</view>
  59. <view class="pop-list">
  60. <view class="form">
  61. <view class="form-item-title">名称</view>
  62. <view class="form-input-box">
  63. <input v-model="addObj.chineseName" placeholder-class="form-input-placeholder"
  64. placeholder="请输入名称(如 姓名)" />
  65. </view>
  66. <view class="form-item-title">字段名</view>
  67. <view class="form-input-box">
  68. <input v-model="addObj.englishName" placeholder-class="form-input-placeholder"
  69. placeholder="请输入字段名(如 name)" />
  70. </view>
  71. <view class="form-item-title">字段类型</view>
  72. <view class="form-input-box form-input-placeholder">
  73. <picker :range="formTypeOptions" range-key="label" v-model="formTypeIndex" @change="changeType" style="color: #333;">
  74. {{formTypeOptions[formTypeIndex].label}}
  75. <view class="more ddflex">
  76. <image src="/static/pages/images/more.png"></image>
  77. </view>
  78. </picker>
  79. <view v-if="formTypeOptions[formTypeIndex].value==2||formTypeOptions[formTypeIndex].value==3">
  80. <view class="ddflex option-input" v-for="it,idx in addObj.selectValues">
  81. <input v-model="it.name" class="fflex" placeholder-class="option-input-placeholder" placeholder="请输入选项值"/>
  82. <view class="option-input-delete" @click="removeOption(idx)">删除</view>
  83. </view>
  84. <view class="add-option-btn" @click="addOption">添加选项</view>
  85. </view>
  86. </view>
  87. <view class="form-item-title ddflex" style="justify-content: space-between;">
  88. 是否必填
  89. <switch v-model="addObj.required" :checked="addObj.required==1" style="transform:scale(0.7)" color="var(--main)"
  90. @change="requiredChange"></switch>
  91. </view>
  92. </view>
  93. </view>
  94. <view class="pop-cancel" @click="confirmPop()">确定</view>
  95. </view>
  96. </view>
  97. </template>
  98. <script>
  99. const app = getApp();
  100. const req = require("../../utils/request.js");
  101. export default {
  102. components: {},
  103. props: {},
  104. data() {
  105. return {
  106. isShowPop: false,
  107. popBottom: '-100%',
  108. addObj: {
  109. englishName: '',
  110. chineseName: '',
  111. type: 1,
  112. selectValues: null,
  113. required: 1,
  114. value: ''
  115. },
  116. extForm: [],
  117. formTypeOptions: [{
  118. value: '1',
  119. label: '输入框'
  120. }, {
  121. value: '2',
  122. label: '单选框'
  123. }, {
  124. value: '3',
  125. label: '多选框'
  126. }, {
  127. value: '4',
  128. label: '单图片'
  129. }, {
  130. value: '5',
  131. label: '多图片'
  132. }],
  133. formTypeIndex:0,
  134. editIndex:-1
  135. }
  136. },
  137. onLoad(options) {
  138. let pages = getCurrentPages(); //获取所有页面栈实例列表
  139. let prevPage = pages[pages.length - 2]; //上一页页面实例
  140. if(prevPage.$vm.extForm){
  141. this.extForm = JSON.parse(prevPage.$vm.extForm);
  142. }
  143. },
  144. onShow() {
  145. },
  146. methods: {
  147. showPop() {
  148. this.isShowPop = true;
  149. this.popBottom = 0;
  150. },
  151. hidePop() {
  152. this.editIndex = -1
  153. this.isShowPop = false;
  154. this.popBottom = '-100%';
  155. this.clearPop()
  156. },
  157. clearPop(){
  158. this.editIndex = -1
  159. this.formTypeIndex = 0
  160. this.addObj = {
  161. englishName: '',
  162. chineseName: '',
  163. type: 1,
  164. selectValues: null,
  165. required: 1,
  166. value: ''
  167. }
  168. },
  169. confirmPop(){
  170. if(!this.addObj.chineseName) return req.msg('请输入名称')
  171. if(!this.addObj.englishName) return req.msg('请输入字段名')
  172. if(!this.addObj.type) return req.msg('请选择字段类型')
  173. if(this.addObj.selectValues) {
  174. for(let i=0;i<this.addObj.selectValues.length;i++){
  175. if(!this.addObj.selectValues[i].name) return req.msg('请输入选项值')
  176. }
  177. }
  178. console.log(this.addObj)
  179. if(this.editIndex>-1){
  180. this.extForm[this.editIndex] = this.addObj
  181. }else{
  182. this.extForm.push(this.addObj)
  183. }
  184. console.log(this.addObj)
  185. this.hidePop()
  186. },
  187. editItem(index){
  188. this.editIndex = index
  189. this.addObj = JSON.parse(JSON.stringify(this.extForm[index]))
  190. this.formTypeIndex = this.formTypeOptions.findIndex(item=>item.value==this.extForm[index].type)
  191. this.showPop()
  192. },
  193. removeItem(index){
  194. this.extForm.splice(index,1)
  195. },
  196. changeType(e) {
  197. console.log(e)
  198. let index = e.detail.value
  199. this.formTypeIndex = index
  200. this.addObj.type = this.formTypeOptions[index].value
  201. if (this.formTypeOptions[index].value == 2 || this.formTypeOptions[index].value == 3) {
  202. this.addObj.selectValues = [{name:''}]
  203. } else {
  204. this.addObj.selectValues = null
  205. }
  206. console.log(this.addObj)
  207. this.addObj = JSON.parse(JSON.stringify(this.addObj))
  208. },
  209. addOption(){
  210. this.addObj.selectValues.push({name:''})
  211. this.addObj = JSON.parse(JSON.stringify(this.addObj))
  212. },
  213. removeOption(index){
  214. console.log(this.addObj.selectValues,index)
  215. this.addObj.selectValues.splice(index,1)
  216. },
  217. requiredChange(e){
  218. this.addObj.required = e.detail.value?1:0
  219. },
  220. save(){
  221. let pages = getCurrentPages();
  222. let prevPage = pages[pages.length - 2];
  223. prevPage.$vm.extForm = this.extForm&&this.extForm.length>0?JSON.stringify(this.extForm):'';
  224. uni.navigateBack({
  225. delta: 1
  226. });
  227. }
  228. },
  229. mounted() {},
  230. }
  231. </script>
  232. <style>
  233. @import "./addForm.css";
  234. </style>