vue elementui 動(dòng)態(tài)追加下拉框、輸入框功能
vue elementui 動(dòng)態(tài)追加下拉框、輸入框
上代碼:
<template>
<div>
<el-dialog
append-to-body
:close-on-click-modal="false"
:close-on-press-escape="false"
width="65%"
@close="onClose"
:modal-append-to-body="true"
title="新建"
custom-class="dialogBox"
:visible.sync="dialogVisible"
:lock-scroll="true"
:destroy-on-close="true">
<el-form :model="combinationInfo" label-width="90px" ref="combinationForm" :rules="rules" :inline="true" size="small">
<el-row>
<el-col :span="12">
<el-form-item prop="benchMarks" label="名稱">
<div style="color: #fb6b3f;width: 230px;">比例之和需為100%(當(dāng)前
<span>{{benchmarkTotal}}</span>
<span></span>%)
</div>
<div v-for="(item,index) in combinationInfo.benchMarks" :key="index">
<el-select
style="margin-bottom: 10px;"
clearable
filterable
collapse-tags
placeholder="請(qǐng)選擇"
class="benchmarkSelectWidth"
@change="changeBenchmark"
v-model="item.code"
>
<el-option
v-for="(item1, idx) in list"
:key="idx"
:label="item1.name"
:value="item1.code"
>
</el-option>
</el-select>
<el-input v-model="item.percentage" @input="changePercentage" placeholder="請(qǐng)輸入" class="benchmarkInputWidth"></el-input>
<span style="padding-left: 2px;">%</span>
<i v-if="index !== 0" style="font-size: 18px;cursor: pointer;padding: 0 0 0 10px;color: #F56C6C;" @click="deleteIndex(index)" class="el-icon-remove-outline"></i>
</div>
<div v-if="benchmarkRule" style="color: #F56C6C;font-size: 12px;margin-top: -17px">請(qǐng)選擇名稱</div>
<el-button @click="addIndex" size="mini" type="primary" icon="el-icon-plus">添加</el-button>
</el-form-item>
</el-col>
<el-col :span="12">
</el-col>
</el-row>
</el-form>
<div style="text-align: right;margin-top: 20px;">
<el-button size="mini" @click="onClose()">取 消</el-button>
<el-button
size="mini"
type="primary"
@click="onConfirm()"
>提 交</el-button
>
</div>
</el-dialog>
</div>
</template>data() {
return {
dialogVisible: false,
combinationInfo: {
benchMarks: [
{
code: '',
name: '',
percentage: '',
}
]
},
rules: {
benchMarks: [{ required: true }],
},
benchmarkRule: false,
benchmarkTotal: 0,
list: [
{
name: 'aaa',
code: '111',
},
{
name: 'bbb',
code: '222',
},
{
name: 'ccc',
code: '333',
},
],
}
},
methods: {
// 添加
addIndex () {
this.combinationInfo.benchMarks.push({
code: '',
percentage: '',
})
},
// 刪除
deleteIndex (index) {
this.combinationInfo.benchMarks.splice(index,1)
this.changePercentage()
},
changeBenchmark (val) {
if (this.combinationInfo.benchMarks.length && this.combinationInfo.benchMarks.length > 1) {
if (!this.isRepeat(this.combinationInfo.benchMarks,'code')) {
this.$message.warning('所選名稱不能重復(fù)!')
return
}
}
},
// 判斷數(shù)組中是否有重復(fù)數(shù)據(jù)(true 不存在;false 存在重復(fù))
isRepeat(arr, key) {
var obj = {};
for (let i = 0; i < arr.length; i ++) {
if (obj[arr[i][key]]) {
return false; // 存在
} else {
obj[arr[i][key]] = arr[i];
}
}
return true;
},
// 名稱值變化時(shí)
changePercentage (val) {
this.benchmarkTotal = 0
if (this.combinationInfo.benchMarks.length && this.combinationInfo.benchMarks.length > 0) {
for(let i = 0; i < this.combinationInfo.benchMarks.length; i++) {
if (this.combinationInfo.benchMarks[i].percentage === '') {
break
}
this.benchmarkTotal+=parseFloat(this.combinationInfo.benchMarks[i].percentage)
}
}
},
// 提交
onConfirm() {
if (this.combinationInfo.benchMarks) {
for(let i = 0; i< this.combinationInfo.benchMarks.length; i++) {
if (this.combinationInfo.benchMarks[i].code) {
this.benchmarkRule = false
} else {
this.benchmarkRule = true
return
}
}
}
if (this.benchmarkTotal !== 100) {
this.$message.warning('名稱比例之和需為100%!')
return
}
},
// 取消
onClose() {
this.benchmarkRule = false
this.dialogVisible = false
},
},展示效果圖:

到此這篇關(guān)于vue elementui 動(dòng)態(tài)追加下拉框、輸入框的文章就介紹到這了,更多相關(guān)vue elementui 追加下拉框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue中輸入框僅支持?jǐn)?shù)字輸入的四種方法
- vue如何設(shè)置輸入框只能輸入數(shù)字且只能輸入小數(shù)點(diǎn)后兩位,并且不能輸入減號(hào)
- 基于vue3實(shí)現(xiàn)一個(gè)簡(jiǎn)單的輸入框效果
- Vue中禁止編輯的常見(jiàn)方法(以禁止編輯輸入框?yàn)槔?
- vue之input輸入框防抖debounce函數(shù)的使用方式
- vue實(shí)現(xiàn)輸入框只允許輸入數(shù)字
- vue3+elementUI實(shí)現(xiàn)懸浮多行文本輸入框效果
- 基于vue+h5實(shí)現(xiàn)車牌號(hào)輸入框功能(demo)
相關(guān)文章
如何啟動(dòng)一個(gè)Vue.js項(xiàng)目
這篇文章主要介紹了如何啟動(dòng)一個(gè)Vue.js項(xiàng)目,對(duì)Vue.js感興趣的同學(xué),可以參考下2021-04-04
在vue中根據(jù)光標(biāo)的顯示與消失實(shí)現(xiàn)下拉列表
這篇文章主要介紹了在vue中根據(jù)光標(biāo)的顯示與消失實(shí)現(xiàn)下拉列表,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Vue實(shí)現(xiàn)Echarts圖表寬高自適應(yīng)的實(shí)踐
本文主要介紹了Vue實(shí)現(xiàn)Echarts圖表寬高自適應(yīng)的實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
解決vue打包報(bào)錯(cuò)Unexpected token: punc的問(wèn)題
這篇文章主要介紹了解決vue打包報(bào)錯(cuò)Unexpected token: punc的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
使用Vue3+ts?開(kāi)發(fā)ProTable源碼教程示例
這篇文章主要為大家介紹了使用Vue3+ts?開(kāi)發(fā)ProTable源碼示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Vue中虛擬DOM的簡(jiǎn)單實(shí)現(xiàn)
Vue中的虛擬DOM是通過(guò)JavaScript對(duì)象來(lái)描述真實(shí)DOM結(jié)構(gòu)的一種方式,本文將介紹Vue中虛擬DOM的簡(jiǎn)單實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-05-05
Vue+element使用row-class-name修改el-table某一行解決背景色無(wú)效的方法
本文主要介紹了Vue+element使用row-class-name修改el-table某一行解決背景色無(wú)效的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

