element+vue 之动态form

1.页面部分

javascript 复制代码
   <div v-for="(item,index) in formList" :key="index">
                        <el-col :span="6" v-if="item.inputType==0">
                            <el-form-item :label="item.conditionName" :prop="item.conditionCode"
                                :rules="{required: item.required==0?false:true, message:item.placeHolder?item.placeHolder:'请输入', trigger: 'change' }">
                                <el-input v-model.trim="detailForm[item.conditionCode]" :placeholder="item.placeHolder"
                                    clearable></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6" v-else-if="item.inputType==1">
                            <el-form-item :label="item.conditionName" :prop="item.conditionCode"
                                :rules="{required: item.required==0?false:true, message: item.placeHolder?item.placeHolder:'请输入', trigger: 'change' }">
                                <el-input v-model.number="detailForm[item.conditionCode]"
                                    :placeholder="item.placeHolder" clearable></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24" v-else-if="item.inputType==2">
                            <el-form-item :label="item.conditionName" :prop="item.conditionCode" :rows="3"
                                :rules="{required: item.required==0?false:true, message:item.placeHolder?item.placeHolder:'请输入', trigger: 'change' }">
                                <el-input clearable style="width: 100%" v-model.trim="detailForm[item.conditionCode]"
                                    autocomplete="off" placeholder="请输入" type="textarea" maxlength="100"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :span="24" v-else-if="item.inputType==3">
                            <el-form-item :label="item.conditionName" :prop="item.conditionCode"
                                :rules="{required: item.required==0?false:true, message:item.placeHolder?item.placeHolder:'请选择', trigger: 'change' }">
                                <!-- <el-input v-model="detailForm[item.conditionCode]" v-show="false" /> -->
                                <el-upload list-type="picture-card" action="#" :headers="headers" multiple
                                    :before-upload="beforeUpload" :http-request="f_upload" :on-remove="handleRemoveUrl"
                                    :on-preview="handlePictureCardPreview" :file-list="fileImgurl"
                                    accept=".jpg,.jpeg,.png,.bmp">
                                    <i slot="default" class="el-icon-plus"></i>
                                    <div class="el-upload__tip" slot="tip">
                                        仅支持JPG、JPEG、PNG、BMP格式文件
                                    </div>
                                </el-upload>
                                <el-dialog :visible.sync="dialogVisible" :modal-append-to-body="false">
                                    <img width="100%" :src="dialogImageUrl" alt="">
                                </el-dialog>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24" v-else-if="item.inputType==4 && item.singleton==0">
                            <el-form-item :label="item.conditionName" :prop="item.conditionCode"
                                :rules="{required: item.required==0?false:true, message:item.placeHolder?item.placeHolder:'请选择', trigger: 'change' }">
                                <el-checkbox-group v-model="detailForm[item.conditionCode]">
                                    <el-col :span="24" v-if="item.optionList">
                                        <el-checkbox v-for="(it,ind) in item.optionList" :label="it.name"
                                            :key="ind"></el-checkbox>
                                    </el-col>
                                </el-checkbox-group>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6" v-else-if="item.inputType==4 && item.singleton==1">
                            <el-form-item :label="item.conditionName" :prop="item.conditionCode"
                                :rules="{required: item.required==0?false:true, message:item.placeHolder?item.placeHolder:'请选择', trigger: 'change' }">
                                <el-radio-group v-model="detailForm[item.conditionCode]">
                                    <el-col :span="24" v-if="item.optionList">
                                        <el-radio v-for="(it,ind)  in item.optionList" :label="it.name"
                                            :key="ind">{{it.name}}</el-radio>
                                    </el-col>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6" v-else-if="item.inputType==5">
                            <el-form-item :label="item.conditionName" :prop="item.conditionCode"
                                :rules="{required: item.required==0?false:true, message:item.placeHolder?item.placeHolder:'请选择', trigger: 'change' }">
                                <el-select v-model="detailForm[item.conditionCode]" placeholder="请选择"
                                    style="width: 100%" v-if="item.optionList"
                                    :multiple="item.inputType==5 && item.singleton==0">
                                    <el-option v-for="(it,ind) in item.optionList" :key="ind" :label="it.name"
                                        :value="it.name">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </div>

2.数据格式

javascript 复制代码
 "decisionConditionList": [
            {
                "conditionCode": "carHygiene",//表单键
                "conditionName": "车辆卫生",//表单名称
                "inputType": 5,//表单类型
                "conditionValueList": [
                    "是"
                ],//表单值
                "required": 1,//是否必填
                "singleton": 0,
                "placeHolder": "请选择",
                "optionList": [
                    {
                        "name": "是",
                        "defaultType": 0
                    },
                    {
                        "name": "否",
                        "defaultType": null
                    }
                ]
            },
            {
                "conditionCode": "productSpotCheck",
                "conditionName": "是否抽检",
                "inputType": 4,
                "conditionValueList": [
                    "否"
                ],
                "required": 1,
                "singleton": 1,
                "placeHolder": "请选择",
                "optionList": [
                    {
                        "name": "是",
                        "defaultType": 0
                    },
                    {
                        "name": "否",
                        "defaultType": 1
                    }
                ]
            },
            {
                "conditionCode": "carPlateNum",
                "conditionName": "车辆号",
                "inputType": 0,
                "conditionValueList": [
                    "1"
                ],
                "required": 1,
                "singleton": null,
                "placeHolder": "请输入",
                "optionList": null
            },
            {
                "conditionCode": "carPlate",
                "conditionName": "车辆数量",
                "inputType": 1,
                "conditionValueList": [
                    "1"
                ],
                "required": 0,
                "singleton": null,
                "placeHolder": "请输入",
                "optionList": null
            },
            {
                "conditionCode": "problemDesc",
                "conditionName": "问题类型",
                "inputType": 4,
                "conditionValueList": [
                    "问题一"
                ],
                "required": 1,
                "singleton": 0,
                "placeHolder": "请选择",
                "optionList": [
                    {
                        "name": "问题一",
                        "defaultType": 0
                    },
                    {
                        "name": "问题二",
                        "defaultType": 0
                    },
                    {
                        "name": "问题三",
                        "defaultType": 0
                    },
                    {
                        "name": "问题四",
                        "defaultType": 0
                    }
                ]
            },
            {
                "conditionCode": "imgUrl",
                "conditionName": "图片",
                "inputType": 3,
                "conditionValueList": [
                    "https://warehouse-permanent.obs.cn-east-3.myhuaweicloud.com/74cc681c-fe39-429c-886e-8e086378c2ef.png",
                    "https://warehouse-permanent.obs.cn-east-3.myhuaweicloud.com/5b312d59-8c40-431b-ad99-ac908bbb7707.png"
                ],
                "required": 1,
                "singleton": null,
                "placeHolder": "请选择",
                "optionList": null
            },
            {
                "conditionCode": "remarkTwo",
                "conditionName": "备注",
                "inputType": 2,
                "conditionValueList": [
                    "1"
                ],
                "required": 0,
                "singleton": null,
                "placeHolder": "1",
                "optionList": null
            }
        ],

3.数据处理 因为后端给的数据都是数组 表单有些需要数组有些需要字符串

javascript 复制代码
 this.formList = res.data.data.decisionConditionList;
                        if (this.formList) {
                            this.formList.forEach(a => {
                                if (a.inputType == 4 && a.singleton == 0 || a.inputType == 5 && a.singleton == 0) {
                                    this.$set(this.detailForm, a.conditionCode, a.conditionValueList ? a.conditionValueList : [])
                                } else if (a.inputType == 3) {
                                    var fileImgurl = []
                                    if (a.conditionValueList) {
                                        a.conditionValueList.forEach((item, index) => {
                                            fileImgurl.push(
                                                Object.assign({}, { url: item })
                                            )
                                        })
                                       this.fileImgurl=fileImgurl
                                    }
                                    this.$set(this.detailForm, a.conditionCode, a.conditionValueList ? a.conditionValueList : [])
                                }
                                else {
                                    this.$set(this.detailForm, a.conditionCode, a.conditionValueList ? a.conditionValueList.toString() : '')
                                }
                            })
                        }

4.关于图片部分的js

javascript 复制代码
  // 上传之前
            beforeUpload(file) {
                if (!this.checkFileType(file)) {
                    return false
                }
                this.$forceUpdate()
            },

            // 效验文件类型
            checkFileType(file) {
                let allowTypesSet = new Set(["jpg", "jpeg", "png", "bmp", "pdf"])
                let fileType = file.name.replace(/.+\./, "")
                if (!allowTypesSet.has(fileType)) {
                    this.$message({
                        type: "warning",
                        message: "不支持该文件类型"
                    })
                    return false
                }
                return true
            },
            //预览图片
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            //删除图片
            handleRemoveUrl(file, fileList) {
                if (fileList.length) {
                    this.fileImgurl.filter((item, index) => {
                        if (item.url == file.url) {
                            this.fileImgurl.splice(index, 1);
                        }
                    });
                    let results = this.fileImgurl.map(n => n.url);
                    this.formList.forEach(index => {
                        if (index.inputType == 3) {
                            this.$set(this.detailForm, index.conditionCode, results)
                        }
                    })
                } else {
                    this.formList.forEach(index => {
                        if (index.inputType == 3) {
                            this.$set(this.detailForm, index.conditionCode, [])
                        }
                    })
                    this.fileImgurl = []
                }
            },
            // 导入
            f_upload(data) {
                const formData = new FormData()
                formData.append("file", data.file)
                this.handlefileUpload(formData)
            },
            //上传方法
            handlefileUpload(formData) {
                fileUpload(formData)
                    .then((res) => {
                        const { code, msg } = res.data
                        if (code == 200) {
                            this.formList.forEach(a => {
                                if (a.inputType == 3) {
                                    var arr = a.conditionValueList ? a.conditionValueList : [];
                                    arr.push(res.data.data)
                                    this.$set(this.detailForm, a.conditionCode, arr)
                                }
                            })
                            this.fileImgurl.push({ url: res.data.data })
                        } else {
                            const title = "操作失败"
                            const type = "error"
                            this.$notification(title, type, msg)
                        }
                    })
                    .catch(() => { })
            },

4.提交

//提交这里还是要按照上面的格式提交

javascript 复制代码
 this.formList.forEach((item, index) => {
          this.detailForm.decisionConditionList.push(
                 Object.assign({},item, { conditionValueList: item.inputType == 4 && item.singleton == 0 || item.inputType == 5 && item.singleton == 0 || item.inputType == 3 ? this.detailForm[item.conditionCode] : [this.detailForm[item.conditionCode]] })
            )
        })
相关推荐
EricWang13583 分钟前
[OS] 项目三-2-proc.c: exit(int status)
服务器·c语言·前端
September_ning3 分钟前
React.lazy() 懒加载
前端·react.js·前端框架
web行路人13 分钟前
React中类组件和函数组件的理解和区别
前端·javascript·react.js·前端框架
番茄小酱00114 分钟前
Expo|ReactNative 中实现扫描二维码功能
javascript·react native·react.js
子非鱼92133 分钟前
【Ajax】跨域
javascript·ajax·cors·jsonp
超雄代码狂35 分钟前
ajax关于axios库的运用小案例
前端·javascript·ajax
长弓三石44 分钟前
鸿蒙网络编程系列44-仓颉版HttpRequest上传文件示例
前端·网络·华为·harmonyos·鸿蒙
小马哥编程1 小时前
【前端基础】CSS基础
前端·css
嚣张农民1 小时前
推荐3个实用的760°全景框架
前端·vue.js·程序员
周亚鑫1 小时前
vue3 pdf base64转成文件流打开
前端·javascript·pdf