vue選擇下拉框動(dòng)態(tài)變化表單方式
vue選擇下拉框動(dòng)態(tài)變化表單
場(chǎng)景
一個(gè)表單,在選擇某個(gè)下拉框時(shí),動(dòng)態(tài)添加選中該下拉框值對(duì)應(yīng)的輸入框或下拉框等。相當(dāng)于有部分輸入框或下拉框是動(dòng)態(tài)變化的。
? ? <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm"> ? ? ? <el-form-item label="名稱(chēng)" prop="name"> ? ? ? ? <el-input v-model="ruleForm.name" maxLength="20" "></el-input> ? ? ? </el-form-item> ? ? ? <el-form-item label="方式" prop="type"> ? ? ? ? <el-select v-model="ruleForm.type" placeholder="請(qǐng)選擇方式" style="width:100%;" @change="changeType" > ? ? ? ? ? <el-option v-for="(item, index) in typeList" :key="index" :label="item.name" :value="item.type"></el-option> ? ? ? ? </el-select> ? ? ? </el-form-item> ? ? ? <template v-for="(item, index) in Object.keys(ruleForm.info)"> ? ? ? ? <el-form-item v-if="getNameByOptions(item)" :label="getNameByKey(item)" :prop="`info.${item}`" :key="index" ? ? ? ? ? :rules="{ required: true, message: `${getNameByKey(item)}不能為空`, trigger: 'change' }"> ? ? ? ? ? <el-select v-model="ruleForm.info[item]" :placeholder="`請(qǐng)選擇${getNameByKey(item)}`" style="width:100%;" > ? ? ? ? ? ? <el-option v-for="(ele, key) in getNameByOptions(item)" :key="key" :label="ele.label" :value="ele.value"></el-option> ? ? ? ? ? </el-select> ? ? ? ? </el-form-item> ? ? ? ? <el-form-item v-else :label="getNameByKey(item)" :prop="`info.${item}`" :key="index" ? ? ? ? :rules="{ required: true, message: `${getNameByKey(item)}不能為空`, trigger: 'blur' }"> ? ? ? ? ? <el-input v-model="ruleForm.info[item]" maxLength="20"></el-input> ? ? ? ? </el-form-item> ? ? ? </template> ? ? </el-form>
注意
如果要驗(yàn)證動(dòng)態(tài)生成的表單,可以單獨(dú)添加 :rules="{ required: true, message:KaTeX parse error: Expected 'EOF', got '}' at position 46: …gger: 'change' }?"`。`prop` 要寫(xiě)成對(duì)應(yīng)…{item}" 才會(huì)生效。
使用 Object.keys(ruleForm.info) 來(lái)遍歷是為了防止動(dòng)態(tài)生成的表單不具有唯一性,即輸入某一項(xiàng)值,其余輸入框也會(huì)填入相同的值。
? data() { ? ? return { ? ? ? ruleForm: { ? ? ? ? type: '', ? ? ? ? name: '', ? ? ? ? remark: '', ? ? ? ? info: {} ? ? ? }, ? ? ? rules: { ? ? ? ? name: [ ? ? ? ? ? { required: true, message: '請(qǐng)輸入名稱(chēng)', trigger: 'blur' } ? ? ? ? ] ? ? ? }, ? ? ? curForm: [], ? ? ? typeList: [] // 數(shù)據(jù)源類(lèi)型 ? ? } ? }, ? methods: { ?? ?// 選擇不同的接入方式,輸出不同的輸入框 ? ? changeType(val) { ? ? ? this.ruleForm.info = {} ? ? ? this.typeList.forEach(item => { ? ? ? ? if (item.type === val) { ? ? ? ? ? this.curForm = item.props ? ? ? ? } ? ? ? }) ? ? ? this.curForm.forEach(item => { ? ? ? ? this.$set(this.ruleForm.info, item.field, '') ? ? ? }) ? ? }, ? ? ?// 根據(jù) key 獲取 label ? ? getNameByKey (key) { ? ? ? return this.curForm.find(item => item.field === key).label ? ? }, ? ? // 根據(jù) key 獲取 options ? ? getNameByOptions (key) { ? ? ? return this.curForm.find(item => item.field === key).options ? ? } ? }
數(shù)據(jù)格式不同的話(huà),學(xué)會(huì)靈活變換
vue動(dòng)態(tài)增減表單項(xiàng)
如圖,實(shí)現(xiàn)以下效果
通過(guò)點(diǎn)擊新增就會(huì)新增一個(gè)表單,刪除會(huì)刪除最后一個(gè)表單,也可以指定刪除哪個(gè)表單
代碼實(shí)現(xiàn)
<el-form ref="shopArr" :model="shopArr" :rules="shopRules"> <table v-for="(item,index) in shopArr" :key="index" class="addTableShop"> <tr> <td class="addTable-td1"><div class="table-item"><el-form-item label="1:" prop="settledPlatfoem" label-width="120px" /></div></td> <td class="addTable-td2"> <el-radio-group v-model="item.settledType"> <el-radio :label="1">淘寶</el-radio> <el-radio :label="2">京東</el-radio> <el-radio :label="3">天貓</el-radio> <el-radio :label="4">拼多多</el-radio> <el-radio :label="5">其他 <input v-if="item.settledType === 5" v-model="item.settledPlatfoem" placeholder="請(qǐng)輸入其他平臺(tái)" style="width: 105px; margin-left: 10px"/> </el-radio> </el-radio-group> </td> <td class="addTable-td1"><div class="table-item"><el-form-item label="2:" prop="shopName" label-width="120px" /></div></td> <td class="addTable-td2"><el-input v-model="item.shopName" /></td> </tr> <tr> <td class="addTable-td1"><div class="table-item"><el-form-item label="3:" prop="shopUrl" label-width="120px" /></div></td> <td class="addTable-td2"><el-input v-model="item.shopUrl" /></td> <td class="addTable-td1"><div class="table-item"><el-form-item label="4:" prop="operatingLicense" label-width="120px" /></div></td> <td class="addTable-td2"><el-input v-model="item.operatingLicense" /></td> </tr> </table> <el-button type="text" style="float: right" size="small" @click="addShop">新增</el-button> <el-button type="text" style="float: right" size="small" @click="deleteShop">刪除</el-button> </el-form>
method中
// 添加表單 add() { const item = { settledType: '', settledPlatfoem: '', shopName: '', shopUrl: '', operatingLicense: '' } this.shopArr.push(item) }, // 刪除表單 deleteShop() { this.shopArr.splice(this.shopArr.length - 1, 1) },
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
前端配合后端實(shí)現(xiàn)Vue路由權(quán)限的方法實(shí)例
一開(kāi)始我還以為vue的路由只能用在工程化的項(xiàng)目里面,其實(shí)不然,下面這篇文章主要給大家介紹了關(guān)于前端配合后端實(shí)現(xiàn)Vue路由權(quán)限的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05vuejs數(shù)據(jù)超出單行顯示更多,點(diǎn)擊展開(kāi)剩余數(shù)據(jù)實(shí)例
這篇文章主要介紹了vuejs數(shù)據(jù)超出單行顯示更多,點(diǎn)擊展開(kāi)剩余數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05element ui的el-input-number修改數(shù)值失效的問(wèn)題及解決
這篇文章主要介紹了element ui的el-input-number修改數(shù)值失效的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05快速解決vue-cli在ie9+中無(wú)效的問(wèn)題
今天小編就為大家分享一篇快速解決vue-cli在ie9+中無(wú)效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09Vue實(shí)現(xiàn)預(yù)覽文件(Word/Excel/PDF)功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)Vue實(shí)現(xiàn)預(yù)覽文件(Word/Excel/PDF)的功能,文中的實(shí)現(xiàn)步驟講解詳細(xì),需要的小伙伴可以參考一下2023-03-03vue實(shí)現(xiàn)消息的無(wú)縫滾動(dòng)效果的示例代碼
本篇文章主要介紹了vue實(shí)現(xiàn)消息的無(wú)縫滾動(dòng)效果的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12