elementUI動態(tài)表單?+?el-select?按要求禁用問題
項目通過 vue+elementUI 實現(xiàn)
近期開發(fā)過程中遇到一個需求,對于從事兩年的“小白”來說,確實費了點腦子,才發(fā)現(xiàn),好像是自己一開始想太多了,各種情況設(shè)想了一溜夠,發(fā)現(xiàn)只要反過來想就OK了 ╮(╯▽╰)╭
需求大概是這樣的:
在動態(tài)增減的表單項中,有一個下拉菜單,要求每選擇一項,就把選中過的那一個選項禁用(簡單來說就是,已經(jīng)選過的就不能再選了),且增加的條數(shù)不能超過下拉菜單中的選項數(shù)量
直接上圖吧(label不重要,主要看效果。。。)
先實現(xiàn)最簡單的:限制新增數(shù)量
判斷已新增的數(shù)量是否小于下拉菜單中選項的數(shù)量
如果小于就新增,否則可以提示一些信息(這里就忽略不寫了)
// 新增按鈕綁定的 的方法 addType () { if (this.otherForm.other.length < this.typeList.length) { this.otherForm.other.push({ type: '', key: Date.now() }) } }
下拉菜單已選中的選項 禁用
邏輯很簡單,當下拉菜單 change 時,先把所有下拉菜單選項的 disabled 賦值為 false(這里用到排他思想,每次change 都先不禁用,選了哪個禁用哪個),遍歷存儲表單數(shù)據(jù)的數(shù)組,在下拉菜單的 list 中找到對應的當前被選中的項,將該項的 disabled 設(shè)為 true(簡單來說就是 現(xiàn)在都有哪項被選擇了 就禁用它 )
changeType (index, Id) { this.typeList.forEach(v => { v.disabled = false for (var i = 0; i < this.otherForm.other.length; i++) { if (this.otherForm.other[i].type === v.Id) { v.disabled = true } } }) }
移除后要把移除的那條選中項的disabled 設(shè)為false
// 移除按鈕 綁定事件 removeType (item) { var index = this.otherForm.other.indexOf(item) if (index !== -1) { this.otherForm.other.splice(index, 1) } // 在下拉菜單數(shù)據(jù)中找到移除的那條的選中項 賦值為false this.typeList.forEach(v => { if (v.Id === item.type && v.disabled) { v.disabled = false } }) }
完整代碼
<template> <div> <el-form :model="otherForm" ref="otherForm" label-width="100px"> <el-form-item v-for="(other, index) in otherForm.other" :label="'類型' + index" :key="index" :prop="'other.' + index + '.type'"> <el-select v-model="other.type" placeholder="請選擇" @change="changeType(index, other.type)"> <el-option v-for="item in typeList" :key="item.Id" :label="item.label" :value="item.Id" :disabled="item.disabled"> </el-option> </el-select> <el-button @click.prevent="removeType(other)">刪除</el-button> </el-form-item> <el-form-item> <el-button @click="addType">新增</el-button> </el-form-item> </el-form> </div> </template>
<script> export default { data () { return { otherForm: { other: [{ type: '' }] }, typeList: [{ Id: 1, label: '報名費' }, { Id: 2, label: '飯費' }, { Id: 3, label: '餐費' }, { Id: 4, label: '書本費' }] } }, methods: { // 刪除 removeType (item) { var index = this.otherForm.other.indexOf(item) if (index !== -1) { this.otherForm.other.splice(index, 1) } this.typeList.forEach(v => { if (v.Id === item.type && v.disabled) { v.disabled = false } }) }, // 新增 addType () { if (this.otherForm.other.length < this.typeList.length) { this.otherForm.other.push({ type: '', key: Date.now() }) } }, changeType (index, Id) { this.typeList.forEach(v => { v.disabled = false for (var i = 0; i < this.otherForm.other.length; i++) { if (this.otherForm.other[i].type === v.Id) { v.disabled = true } } }) } } } </script>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3+Spring Framework框架開發(fā)實戰(zhàn)
這篇文章主要為大家介紹了Vue3+Spring Framework框架開發(fā)實戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04Vue3?echarts組件化及使用hook進行resize方式
這篇文章主要介紹了Vue3?echarts組件化及使用hook進行resize方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04vue-router?導航完成后獲取數(shù)據(jù)的實現(xiàn)方法
這篇文章主要介紹了vue-router?導航完成后獲取數(shù)據(jù),通過使用生命周期的 created() 函數(shù),在組件創(chuàng)建完成后調(diào)用該方法,本文結(jié)合實例代碼給大家講解的非常詳細需要的朋友可以參考下2022-11-11