vue+element实现动态加载表单
程序员文章站
2022-03-02 12:26:37
本文实例为大家分享了vue+element实现动态加载表单的具体代码,供大家参考,具体内容如下一、问卷动态加载表单//html
本文实例为大家分享了vue+element实现动态加载表单的具体代码,供大家参考,具体内容如下
一、问卷动态加载表单
//html <el-form :model="quespaper" status-icon label-width="100px" label-position="top" size="small" v-loading="paperloading" > <el-form-item v-for="n in paperform.answerlist" :label="n.questionrepository.question" :key="n.key" > <!-- 单选 --> <el-radio-group v-model="n.optionid" v-if="n.questionrepository.type === 1"> <el-radio v-for="(i, idx) in n.questionoption" :label="i.id" :key="idx" class="mb5">{{ i.questionoption }}</el-radio> </el-radio-group> <!-- 多选 --> <el-checkbox-group v-model="n.optionid" v-if="n.questionrepository.type === 2"> <el-checkbox v-for="(i, idx) in n.questionoption" :label="i.id" :key="idx">{{ i.questionoption }}</el-checkbox> </el-checkbox-group> <!-- 填空 --> <el-input type="textarea" style="width: 600px" class="fl" v-model="n.answer" v-if="n.questionrepository.type === 3" :rows="4" placeholder="请输入内容" ></el-input> </el-form-item> <el-row class="mt20" type="flex" justify="start"> <el-col :span="5"> <el-button type="primary" size="small" @click.stop="savestageanswer">保存问卷</el-button> </el-col> </el-row> </el-form> //data数据 paperform: { // 问卷表单 answerlist:[{ questionrepository:'', questionoption:[], questionid: '', optionid:'', answer: '' }] }, //接口 getpaperbydrugstatus(drugstatus, id){ // 根据用药状态获取问卷 this.paperloading = true this.$axios.get(this.$api + 'xxx/xxx?paperid='+this.selectstage.paperid + '&drugstatus=' + drugstatus).then(res => { if(res.data.code === 200){ let answerarr = [] let questionarr = [] res.data.data.questiontolist.map((item)=>{ item.optionlist.map((n)=>{ questionarr.push({ id:n.id, questionoption:n.questionoption }) }) answerarr.push({ questionoption:questionarr, questionrepository:item.questionrepository, questionid:item.questionrepository.id, optionid:item.questionrepository.type === 2?[]:'', answer: '' }) }) console.log(answerarr) this.paperform = { // 问卷表单 answerlist:answerarr } settimeout(() => { this.paperloading = false }, 300) } }) },
二、批量数据动态加载表单
特点:
1.每一行的输入框,根据后台返回值,动态生成,数量可增可减。
2.行数根据自己设置的值 5 ,自动循环生成。
3.验证需要:prop和:rules,如果不验证,就删了
<template> <!-- 录入数据 --> <div class="dialyinputdetail" > <div class="fw fz16 color6 mb20 mt10">批量录入<span class="colorred">{{tabheader.monthstr}}</span>数据</div> <el-form status-icon label-width="100px" :model="newform" ref='newform' label-position="top" size="small"> <table border="1" class="fw fz14 color6 table"> <tr> <td rowspan='2'>患者请用姓名<br/>或病历号模糊筛选</td> <td :colspan="tabheader.firsttables.length" class="tc colorred lh40">以下信息每1个月登记一次</td> <td :colspan="tabheader.secondtables.length" class="tc colorred lh40">以下信息每3个月登记一次</td> <td :colspan="tabheader.thirdtables.length" class="tc colorred lh40">以下信息每6个月登记一次</td> </tr> <tr class="lh40"> <td v-for="item in tabheader.firsttables" :key="item.name" class="tc">{{item.name}}</td> <td v-for="n in tabheader.secondtables" :key="n.name" class="tc">{{n.name}}</td> <td v-for="z in tabheader.thirdtables" :key="z.nam" class="tc">{{z.name}}</td> </tr> <tr v-for="(w,index) in newform.collist" :key="index+'a'" > <td> <el-form-item :prop="'collist.'+index+'.patientid'" > <!--- :rules="{required: true, message: '不能为空', trigger: 'blur'}"--> <el-select v-model="w.patientid" size="small" style="width: 100px" filterable clearable> <el-option v-for="(ite, idx) in patientarr" :label="ite.patient" :key="idx" :value="ite.id"> </el-option> </el-select> </el-form-item> </td> <td class="tc" v-for="(n,j) in w.itemdatalist" :key="j"> <el-form-item :prop="'collist.'+index+'.itemdatalist.' + j + '.itemvalue'" > <!-- :rules="{required: true, message: '不能为空', trigger: 'blur'}"--> <el-select v-model="n.itemvalue" size="small" style="width: 100px" v-if="n.type == 2" filterable clearable> <el-option v-for="(i, idx) in n.opts" :label="i" :key="idx" :value="i"> </el-option> </el-select> <el-input style="width: 100px" size="small" v-model="n.itemvalue" v-if="n.type == 1" ></el-input> </el-form-item> </td> </tr> </table> <el-row class="mt20" type="flex" justify="start"> <el-col :span="5"> <el-button type="primary" size="small" @click="submitnew('newform')">提交数据</el-button> </el-col> </el-row> </el-form> </div> </template> <script> import vue from 'vue' export default { name: 'dialyinputdetail', props:['dialysisid','dialysiscenter'], data() { return { tabheader:{ firsttables:[], secondtables:[], thirdtables:[], colnub:[] }, dialydetail:{}, newform:{ id:'', collist:[ // { // patientid:123, //患者id // createuserid:123, //当前用户id // createusername:"管理员" , // itemdatalist:[{ // itemid:'', // itemvalue:'', // type:1 // }] // } ], }, patientarr:[], } }, created () { this.getmedrdtables(this.dialysisid) this.getpatient(this.dialysisid) }, methods: { getmedrdtables(id){//获取录入tab this.$axios.get(this.$my.api + '/bms/input/getmedrdtables?dialysisid='+id).then(res => { if(res.data&&res.data.code === 200){ this.tabheader = res.data.data this.tabheader.colnub = [...res.data.data.firsttables,...res.data.data.secondtables,...res.data.data.thirdtables] this.newform.collist = [] for(let i=0;i<5;i++){//要push的对象,必须写在循环体内 let arr = [] let obj = { patientid:'', dialysisid:res.data.data.auth.dialysisid, //透析中心id dialysiscenter:res.data.data.auth.dialysiscenter, //透析中心名称 //patientid:'', //患者id //patient:'', //患者名称 inputauthid:res.data.data.auth.id, //透析中心权限id year:res.data.data.auth.inputyear, //录入年份 month:res.data.data.auth.inputmonth, createuserid:json.parse(localstorage.getitem('userinfo')).session.id, createusername:json.parse(localstorage.getitem('userinfo')).session.name, } this.tabheader.colnub.map(n=>{ arr.push({itemid:n.id ,opts:n.opts ,itemvalue:'',type:n.type}) }) obj.itemdatalist = arr this.newform.collist.push(obj) } }else{ this.$message({ message: res.data.message, type: 'error', duration: 1500 }) return false } }).catch(function (error) {}) }, getdialydetail(id){//获取透析中心详情 this.$axios.get(this.$my.api + '/bms/input/getdialydetail?id='+id).then(res => { if(res.data&&res.data.code === 200){ this.dialydetail = res.data.data }else{ this.$message({ message: res.data.message, type: 'error', duration: 1500 }) return false } }).catch(function (error) {}) }, getpatient(id){ this.$axios.get(this.$my.api + '/bms/input/getallpatlist?dialysisid='+id).then(res => { if(res.data&&res.data.code === 200){ this.patientarr = res.data.data }else{ this.$message({ message: res.data.message, type: 'error', duration: 1500 }) return false } }).catch(function (error) {}) }, submitnew(formname){ //新增-原本所有都是必填项,后改为name和hb值必填 this.$refs[formname].validate((valid) => { if (valid) { let ok = false this.newform.collist.map((item)=>{ if(item.patientid){ let name = item.itemdatalist.find((n)=>n.itemid == 33) if(name&&name.itemvalue=='') ok = true } }) if(this.newform.collist.every((item)=>item.patientid == '')){ this.$message({ message: '至少填写一个患者', type: 'error', duration: 1500 }) return false } if(ok){ this.$message({ message: '透析前舒张压(mmhg)不能为空', type: 'error', duration: 1500 }) return false } this.$axios.post(this.$my.api + '/bms/input/bathsaverecord', this.newform.collist).then(res => { if(res.data&&res.data.code === 200){ this.$message({ message: res.data.message, type: 'success', duration: 1500 }) this.$refs[formname].resetfields(); }else{ this.$message({ message: res.data.message, type: 'error', duration: 1500 }) } }).catch( error =>{}) } else { console.log('error submit!!'); return false; } }); } }, } </script> <style scoped> .table{ border-color: #aaa; width: 1000px; overflow: auto; } .table .el-form-item--small.el-form-item{ margin: 8px; } </style>
部分必选:
data(){ return { formlist:[{ patient:'', casenumber:'', year:'', sex:'', dialysisage:'', primarydisease:'', diagnosis:'', creatinine:'', gfr:'', weektreattimes:'', weekdialysishours:'' },{ patient:'', casenumber:'', year:'', sex:'', dialysisage:'', primarydisease:'', diagnosis:'', creatinine:'', gfr:'', weektreattimes:'', weekdialysishours:'' },{ patient:'', casenumber:'', year:'', sex:'', dialysisage:'', primarydisease:'', diagnosis:'', creatinine:'', gfr:'', weektreattimes:'', weekdialysishours:'' }] }, methods:{ submitdata(){ let param={ dialysisid:this.$route.query.id, dialysiscenter:this.$route.query.name, createusername:json.parse(localstorage.getitem('userinfo')).session.name, createuserid:json.parse(localstorage.getitem('userinfo')).session.id, patientlist:nwearr } // 部分必选 start let ok = false // 需要必选的值 let rules = [{name:'patient',msg:'姓名'},{name:'casenumber',msg:'身份证'},{name:'year',msg:'年龄'},{name:'sex',msg:'性别'}] this.formlist.map((item)=>{ //每一行,是否有值 let hangnoval = object.keys(item).every(n=>item[n] == '') if(!hangnoval){ //任意一个有值 for(let i of rules){ if(item[i.name] == ''){//必填项是否有值 this.$message({ message: i.msg+'不能为空!', type: 'error', duration: 1500 }) break } } }else{ ok = true } }) if(ok){ this.$message({ message: '请填写,再提交', type: 'error', duration: 1500 }) return false } // 部分必选 end this.$axios.post(this.$my.api + '/bms/input/bathsavepat',param).then(res => { if(res.data&&res.data.code === 200){ //ok } }).catch(function (error) {}) }, } } //情况二:有输入,才必填 // 部分必选 start let ok = [] let no = '' // 需要必选的值 let rules = [{name:'patient',msg:'姓名'},{name:'casenumber',msg:'身份证'},{name:'year',msg:'年龄'},{name:'sex',msg:'性别'}] this.formlist.map((item)=>{ //每一行,是否有值 let hangnoval = object.keys(item).every(n=>item[n] == '') if(!hangnoval){ //任意一个有值 ok.push(false) for(let i of rules){ if(item[i.name] == ''){//必填项是否有值 no = true this.$message({ message: i.msg+'不能为空!', type: 'error', duration: 1500 }) break } } }else{ ok.push(true) } }) if(ok.every(n=>n == true)){ this.$message({ message: '请填写,再提交', type: 'error', duration: 1500 }) return false } if(no){ return false } // 部分必选 end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。