表单绑定复选框的值和图片上传

本文探讨了如何在表单中实现与复选框值的绑定,特别是当后端要求传code时,如何处理input绑定。此外,重点讨论了图片上传的流程,包括loadImagePath方法在onLoad生命周期中的应用,以及图片上传前设置请求头的注意事项。遗憾的是,部分代码未展示,作者承诺后续会完善。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

表单如何实现与复选框的值绑定一致
在这里插入图片描述

<u-form-item :label-position="labelPosition" label="提供服务" prop="provideServices" :label-width="labelWidth" :required="true">
				<u-popup v-model="provideServicesShow" mode="bottom">
					<view class="multi-check">
						<u-checkbox-group shape="square" class="group" @change="provideServicesCheckbox">
							<u-checkbox v-for="(item,index) in provideServicesList" :key="index" v-model="item.checked" :name="item.code"
							 class="checkbox">{{item.name}}</u-checkbox>
						</u-checkbox-group>
						<u-button @click="provideServicesConfirm">确定</u-button>
					</view>
				</u-popup>
				<u-input :border="border" :placeholder="rules.provideServices[0].message" v-model="model.provideServicesName" type="select"
				 :select-open="provideServicesShow" @click="provideServicesShow = true"></u-input>
			</u-form-item>

这是表单的代码,没什么好记录的

provideServicesCheckbox(e) {
			console.log('confirm', e);
			this.model.provideServices = e.join(',');
			this.provideServices
			// this.$set(this.model,'provideServicesName',_.includes(this.))
		},
provideServicesConfirm(e) {
			this.provideServicesShow = false;
			let list = this.model.provideServices.split(',');
			let str = '';
			console.log('themeList', list);
			for (var i = 0; i < list.length; i++) {
				for (var j = 0; j < this.provideServicesList.length; j++) {
					if (list[i] === this.provideServicesList[j].code) {
						if(i === list.length-1){
							str += this.provideServicesList[j].name;
						}else{
							str += this.provideServicesList[j].name + "、";
						}
					}
				}
			};
			console.log('str', str);
			this.model.provideServicesName = str;

		},

js方法代码其实也没什么难点,就是当时开发的时候,后端要求传code过去,那么input输入框绑定的数据只能显示为code,所以我们要写一个双重循环,将code与list中的<key,value> 一 一对应找出来,并赋值给input的v-model
其实这个表单重点、难点在于图片上传,我在这大概讲下 !代码如下

<u-form-item :label-position="labelPosition" label="书店照片" prop="storePhoto" :label-width="labelWidth" :required="true">
				<u-upload ref="uUpload" width="160" height="160" max-count="3" :action="uploadAction" :file-list="image.bookImgPathList"
				 :before-upload="beforeUpload(bizType.bookImgPath1,)" :form-data="uploadFormData" :header="uploadHeader"
				 :auto-upload="true" @on-success="onUploadSuccess" @on-error="onUploadError"></u-upload>
			</u-form-item>
uploadAction: this.$ajax.getUploadUrl(),
uploadFormData: {},
uploadHeader: {},
bizType: {
					bookImgPath1: Constants.fileBizType._71,
					bookImgPath2: Constants.fileBizType._71,
					bookImgPath3: Constants.fileBizType._71,
					imgPath1: Constants.fileBizType._61,
					imgPath2: Constants.fileBizType._62,
				},
				image: {
					bookImgPathList: [],
					// bookImgPath1: [],
					// bookImgPath2: [],
					// bookImgPath3: [],
					imgPath1: [],
					imgPath2: [],
				},
loadImagePath() {
				_.each(this.model, (val, key) => {
					if (key.includes('imgPath')  && !_.isEmpty(val)) {
						this.image[key] = [{
							url: $ajax.getThumbnailUrl(val)
						}]
					}
				}),
				_.each(this.model, (val, key) => {
					if (key.includes('bookImgPath')  && !_.isEmpty(val)) {
						this.image.bookImgPathList.push({
							url: $ajax.getThumbnailUrl(val)});
					}
				})
			},
beforeUpload(bizType) {
				return function(index, lists) {
					if (bizType === '71'){
						_.each({
							"bizId": this.model.id,
							"bizType": bizType,
							"seqNo": index+1,
							"delete": true
						}, (val, key) => this.$set(this.uploadFormData, key, val));
						_.each(this.$ajax.getHeader(this.uploadFormData), (val, key) => this.$set(this.uploadHeader, key, val));
						return true;
					}
					_.each({
						"bizId": this.model.id,
						"bizType": bizType,
						"delete": true
					}, (val, key) => this.$set(this.uploadFormData, key, val));
					_.each(this.$ajax.getHeader(this.uploadFormData), (val, key) => this.$set(this.uploadHeader, key, val));
					return true;
				}
			},
onUploadSuccess(resp) {
				this.$refs.uToast.show({
					title: resp.message,
					position: 'top',
					type: resp.success ? 'success' : 'error'
				});
			},

onUploadError(resp, index, lists) {
				this.$refs.uToast.show({
					title: resp.message,
					position: 'top',
					type: 'error'
				});
			},

loadImagePath方法要写在onLoad生命周期中,页面一加载,图片就开始渲染。可能有些方法我没贴出来,那些事图片上传之前给请求设置请求头的方法,不是我写的,我也写不出来,也不是每个公司都需要这个,所以就没打算贴出来。
希望我下次来完善博客的时候,能贴出完整的代码把,这也代表我是有进步的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值