当前位置:   article > 正文

uniapp uview循环子表单验证组件实现_u-form 循环校验

u-form 循环校验
  1. <template>
  2. <view class="container container15550">
  3. <u-form :model="form" :rules="formRules" :errorType="['message', 'toast']" ref="formRef" class="flex diygw-form diygw-col-24">
  4. <u-form-item class="diygw-col-24" :required="true" label="姓名" prop="input">
  5. <u-input :focus="formData.inputFocus" class="" placeholder="请输入提示信息" v-model="form.input" type="text"></u-input>
  6. </u-form-item>
  7. <view class="flex flex-wrap diygw-col-24 flex-direction-column">
  8. <view class="diygw-col-24" v-for="(subformItem, subformIndex) in form.subform" :key="subformIndex">
  9. <u-form :model="form.subform[subformIndex]" :errorType="['message', 'toast']" ref="subformRef" :rules="subformItemRules">
  10. <u-form-item class="diygw-col-24" :required="true" label="公司名" prop="input1">
  11. <u-input :focus="subformItemData.input1Focus" class="" placeholder="请输入提示信息" v-model="subformItem.input1" type="text"></u-input>
  12. </u-form-item>
  13. <u-form-item class="diygw-col-24" label="单选" prop="radio">
  14. <u-radio-group class="flex flex-wrap diygw-col-24 justify-between" wrapClass=" justify-between" activeColor="#39b54a" v-model="subformItem.radio" @change="changeSubformItemRadio($event, subformIndex, subformItem)">
  15. <u-radio shape="circle" v-for="(radioitem, radioindex) in subformItemData.radioDatas" :key="radioindex" :name="radioitem.value">
  16. {{ radioitem.label }}
  17. </u-radio>
  18. </u-radio-group>
  19. </u-form-item>
  20. <u-form-item class="diygw-col-24" label="复选" prop="checkbox">
  21. <u-checkbox-group class="flex flex-wrap diygw-col-24 justify-between" wrapClass=" justify-between" activeColor="#39b54a" v-model="subformItem.checkbox" @change="changeSubformItemCheckbox">
  22. <u-checkbox v-model="checkboxitem.checked" v-for="(checkboxitem, checkboxindex) in subformItemData.checkboxDatas" :key="checkboxindex" :name="checkboxitem.value">
  23. {{ checkboxitem.label }}
  24. </u-checkbox>
  25. </u-checkbox-group>
  26. </u-form-item>
  27. </u-form>
  28. <view class="padding-xs flex justify-end">
  29. <button @tap="upSubformItem" :data-index="subformIndex" class="diygw-btn green radius margin-xs">上移</button>
  30. <button @tap="downSubformItem" :data-index="subformIndex" class="diygw-btn green radius margin-xs">下移</button>
  31. <button @tap="delSubformItem" :data-index="subformIndex" class="diygw-btn red radius margin-xs">删除</button>
  32. </view>
  33. </view>
  34. <view class="padding-xs">
  35. <button @tap="addSubformItem" class="diygw-btn diygw-col-24 radius" style="background: #07c160; color: #fff">新增</button>
  36. </view>
  37. </view>
  38. <view class="flex diygw-col-24">
  39. <button @click="submitForm" class="diygw-btn green radius-xs flex-sub margin-xs button-button-clz">按钮</button>
  40. </view>
  41. </u-form>
  42. <view class="clearfix"></view>
  43. </view>
  44. </template>
  45. <script>
  46. export default {
  47. data() {
  48. return {
  49. //用户全局信息
  50. userInfo: {},
  51. //页面传参
  52. globalOption: {},
  53. //自定义全局变量
  54. globalData: {},
  55. dta: {
  56. code: 200,
  57. msg: '获取数据成功',
  58. data: [
  59. {
  60. title: '标题1',
  61. remark: '描述1',
  62. id: 1,
  63. attr: {
  64. title: '标题1'
  65. },
  66. img: 'https://libs.diygw.com/upload/xcx.jpg'
  67. },
  68. {
  69. title: '标题2',
  70. remark: '描述2',
  71. id: 2,
  72. attr: {
  73. title: '标题2'
  74. },
  75. img: 'https://libs.diygw.com/upload/xcx.jpg'
  76. },
  77. {
  78. title: '标题3',
  79. remark: '描述3',
  80. id: 3,
  81. attr: {
  82. title: '标题3'
  83. },
  84. img: 'https://libs.diygw.com/upload/xcx.jpg'
  85. },
  86. {
  87. title: '标题4',
  88. remark: '描述4',
  89. id: 4,
  90. attr: {
  91. title: '标题4'
  92. },
  93. img: 'https://libs.diygw.com/upload/xcx.jpg'
  94. },
  95. {
  96. title: '标题5',
  97. remark: '描述5',
  98. id: 5,
  99. attr: {
  100. title: '标题5'
  101. },
  102. img: 'https://libs.diygw.com/upload/xcx.jpg'
  103. }
  104. ]
  105. },
  106. subformItemData: {
  107. input1Focus: false,
  108. radioDatas: [
  109. { value: '1', label: '选项一', checked: true },
  110. { value: '2', label: '选项二', checked: false },
  111. { value: '3', label: '选项三', checked: false }
  112. ],
  113. checkboxDatas: [
  114. { value: '1', label: '选项一', checked: true },
  115. { value: '2', label: '选项二', checked: true },
  116. { value: '3', label: '选项三', checked: false }
  117. ]
  118. },
  119. subformItem: {
  120. input1: '',
  121. radio: '1',
  122. checkbox: ['1', '2']
  123. },
  124. formRules: {
  125. input: [
  126. {
  127. trigger: ['change', 'blur'],
  128. required: true,
  129. message: '姓名不能为空'
  130. }
  131. ]
  132. },
  133. form: {
  134. input: '',
  135. subform: []
  136. },
  137. subformItemRules: {
  138. input1: [
  139. {
  140. trigger: ['change', 'blur'],
  141. required: true,
  142. message: '公司名不能空'
  143. }
  144. ]
  145. },
  146. formData: {
  147. inputFocus: false
  148. }
  149. };
  150. },
  151. onShow() {
  152. this.setCurrentPage(this);
  153. },
  154. onLoad(option) {
  155. this.setCurrentPage(this);
  156. if (option) {
  157. this.setData({
  158. globalOption: this.getOption(option)
  159. });
  160. }
  161. this.init();
  162. },
  163. onReady() {
  164. this.$refs.formRef?.setRules(this.formRules);
  165. this.initSubformData();
  166. },
  167. methods: {
  168. async init() {},
  169. // 新增接口 API请求方法
  170. async dtaApi(param) {
  171. let thiz = this;
  172. param = param || {};
  173. //请求地址及请求数据,可以在加载前执行上面增加自己的代码逻辑
  174. let http_url = '/article.php';
  175. let http_data = {};
  176. let http_header = {
  177. 'Content-type': 'application/json'
  178. };
  179. let dta = await this.$http.post(http_url, http_data, http_header, 'json');
  180. this.dta = dta;
  181. },
  182. //初始化显示子表单数据条数
  183. initSubformData() {
  184. if (this.subformItem) {
  185. for (let i = 0; i < 1; i++) {
  186. this.form.subform.push(JSON.parse(JSON.stringify(this.subformItem)));
  187. }
  188. this.initSubformValid();
  189. } else {
  190. this.navigateTo({
  191. type: 'tip',
  192. tip: '请先设计子表单哟'
  193. });
  194. }
  195. },
  196. //子表单验证
  197. initSubformValid() {
  198. //如果没找到子表单验证规,不用验证
  199. if (this.subformItemRules) {
  200. this.$nextTick(() => {
  201. this.$refs['subformRef']?.forEach((subform) => {
  202. subform.setRules(this.subformItemRules);
  203. });
  204. });
  205. }
  206. },
  207. //上移子表单
  208. upSubformItem(evt) {
  209. let { index } = evt.currentTarget.dataset;
  210. if (index == 0) {
  211. this.navigateTo({
  212. type: 'tip',
  213. tip: '已经是第一个'
  214. });
  215. return false;
  216. }
  217. this.form.subform[index] = this.form.subform.splice(index - 1, 1, this.form.subform[index])[0];
  218. this.initSubformValid();
  219. },
  220. //下移子表单
  221. downSubformItem(evt) {
  222. let { index } = evt.currentTarget.dataset;
  223. if (index == this.form.subform.length - 1) {
  224. this.navigateTo({
  225. type: 'tip',
  226. tip: '已经是最后一个'
  227. });
  228. return false;
  229. }
  230. this.form.subform[index] = this.form.subform.splice(index + 1, 1, this.form.subform[index])[0];
  231. this.initSubformValid();
  232. },
  233. //删除子表单
  234. delSubformItem(evt) {
  235. let { index } = evt.currentTarget.dataset;
  236. this.form.subform.splice(index, 1);
  237. this.initSubformValid();
  238. },
  239. //增加子表单
  240. addSubformItem() {
  241. this.form.subform.push(JSON.parse(JSON.stringify(this.subformItem)));
  242. this.initSubformValid();
  243. },
  244. //验证所有的子表单
  245. checkSubformValid() {
  246. let formFlag = true;
  247. return new Promise((resolve, reject) => {
  248. let list = [];
  249. this.$refs['subformRef'].forEach((subform) => {
  250. const p = new Promise((reso, rej) => {
  251. subform.validate((valid) => {
  252. if (!valid) {
  253. formFlag = false;
  254. }
  255. reso(); //单个接口执行完毕
  256. });
  257. });
  258. list.push(p);
  259. });
  260. Promise.all(list).then((result) => {
  261. resolve(formFlag); //所有接口都执行完毕
  262. });
  263. });
  264. },
  265. changeSubformItemRadio(evt, subformIndex, subformItem) {},
  266. changeSubformItemCheckbox(evt, subformIndex, subformItem) {},
  267. async submitForm(e) {
  268. let valid = await this.$refs.formRef.validate();
  269. let subformvalid = await this.checkSubformValid();
  270. if (valid && subformvalid) {
  271. //保存数据
  272. let param = this.form;
  273. let header = {
  274. 'Content-type': 'application/json'
  275. };
  276. let url = '/sys/form/add';
  277. let res = await this.$http.post(url, param, header, 'json');
  278. if (res.code == 200) {
  279. this.showToast(res.msg, 'success');
  280. } else {
  281. this.showModal(res.msg, '提示', false);
  282. }
  283. } else {
  284. console.log('验证失败');
  285. }
  286. }
  287. }
  288. };
  289. </script>
  290. <style lang="scss" scoped>
  291. .button-button-clz {
  292. margin: 3px !important;
  293. }
  294. .container15550 {
  295. padding-left: 0px;
  296. padding-right: 0px;
  297. font-size: 12px;
  298. }
  299. .container15550 {
  300. }
  301. </style>

uniapp uview循环子表单验证组件实现,比如果我们最常见的简历功能实现,一个人会多个工作经验。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/115750
推荐阅读
相关标签
  

闽ICP备14008679号