Element+Vue實現(xiàn)動態(tài)表單多個下拉框組件功能
需求
表單的內(nèi)容為巡檢計劃,巡檢計劃可以選擇多個巡檢點位,每個巡檢點位可以選擇多個設(shè)備和對應(yīng)操作。
最終效果圖
- 點擊加號圖標(biāo)增加一個下拉框
- 減號圖標(biāo)刪除對應(yīng)下拉框
- 下拉框備選項目相同
- 點擊設(shè)置動作按鈕,彈出可編輯表格,可以為該巡檢點位設(shè)置多個動作

- 表格每行內(nèi)容可編
- 設(shè)別名稱下拉框和設(shè)備動作下拉框聯(lián)動
- 操作按鈕可對選項進(jìn)行上下排序

代碼實現(xiàn)
1.定義表單結(jié)構(gòu)
<el-dialog :title="dialogTitle" :visible.sync="addDialogOpen" width="650px">
<div class="dialogForm">
<el-form :rules="rules" :model="addForm" ref="addForm" label-width="80px">
精簡了無關(guān)代碼
<el-form-item label="巡檢內(nèi)容" prop="patrolContent">
<div class="patrol-content">
<div class="point">
patrolContent元素個數(shù)控制下拉框的個數(shù)
循環(huán)渲染
每個div中包含一個下拉框、三個按鈕
<div class="point-item"
v-for="(item,index) in addForm.patrolContent"
:key="index">
<div class="point-select">
<el-select v-model="item.point" class="selectItem" placeholder="請選擇巡檢點位(先選擇地圖)">
<el-option v-for="(item,index) in pointOptions"
:key="index"
:label="item.point_name"
:value="item.point_name">
</el-option>
</el-select>
<el-button size="mini" icon="el-icon-circle-plus" type="primary"
@click="addPointSelectItem(index)"/>
<el-button size="mini" icon="el-icon-remove" type="info"
@click="removePointSelectItem(index)"/>
<el-button size="mini" type="success"
@click="addDeviceAction(index)">設(shè)置動作
</el-button>
</div>
</div>
</div>
</div>
</el-form-item>
</el-form>
</div>
</el-dialog>2.在data()中定義需要綁定的模板變量
addForm: {
patrolContent: [ //有多少個元素就渲染多少個下拉框
{
point: '', //點位
children: [], //對應(yīng)每個點位的具體動作
}
]
}3.定義增減下拉框的方法,實現(xiàn)動態(tài)增減
// 增加一個巡檢點下拉框
addPointSelectItem(index) {
this.addForm.patrolContent.splice(index + 1, 0,
{
point: '',
children: [] //為空表示該點還未設(shè)置動作
}
)
},
// 刪除一個巡檢點下拉框
removePointSelectItem(index) {
if (this.addForm.patrolContent.length > 1) {
this.addForm.patrolContent.splice(index, 1)
}
},至此,已經(jīng)實現(xiàn)了下拉框的動態(tài)增減,且下拉框的選項之間互不影響。接下來繼續(xù)實現(xiàn)每個點位可以配置不同的動作。
4.定義打開動作彈窗的函數(shù)
這里在打開彈窗時,首先獲取到所點擊的下拉框索引值index,index代表了該元素在patrolContent中的位置,通過index獲取表格要綁定的變量(該元素中的children)。
// 在該點位添加動作:打開彈窗
addDeviceAction(index) {
this.dialogTableData = this.addForm.patrolContent[index].children
this.deviceDialogOpen = true
}5.定義動作彈窗
在彈窗打開時,表格會根據(jù)children中的元素進(jìn)行渲染。
表格借助v-if實現(xiàn)了編輯和確認(rèn)編輯邏輯。
實現(xiàn)表格項的增刪較為簡單,增刪children列表中的元素即可。
<el-dialog title="選擇動作" :visible.sync="deviceDialogOpen" width="800px">
<div class="device-dialog">
<el-table :data="dialogTableData" border>
<el-table-column align="center" label="序號" type="index" width="50"/>
<el-table-column align="center" label="設(shè)備名稱" prop="device">
<template v-slot="scope">
<el-select v-show="scope.row.edit" v-model="scope.row.device"
class="selectItem"
placeholder="選擇設(shè)備"
@change="deviceSelectChange">
<el-option value="機(jī)械臂" label="機(jī)械臂"/>
<el-option value="升降臺" label="升降臺"/>
<el-option value="攝像頭" label="攝像頭"/>
</el-select>
<span v-show="!scope.row.edit">
{{ scope.row.device }}
</span>
</template>
</el-table-column>
<el-table-column align="center" label="設(shè)備動作" prop="action">
<template v-slot="scope">
<el-select v-show="scope.row.edit"
value-key="id"
v-model="scope.row.action"
class="selectItem"
placeholder="選擇動作">
<el-option v-for="item in actionOptions"
:key="item.label"
:value="item.value"
:label="item.label">
</el-option>
</el-select>
<span v-show="!scope.row.edit">
{{ scope.row.action['action_name'] }}
</span>
</template>
</el-table-column>
<el-table-column align="center" label="操作" width="300">
<template v-slot="scope">
<el-button size="mini" type="primary" v-show="!scope.row.edit" @click="editAction(scope.row)">編輯
</el-button>
<el-button size="mini" type="success" v-show="scope.row.edit" @click="scope.row.edit=!scope.row.edit">
確定
</el-button>
<el-button size="mini" type="danger" @click="delAction(scope.$index)">刪除</el-button>
<el-button size="mini" type="primary" plain @click="upMoveItem(scope.$index)"
icon="el-icon-caret-top"></el-button>
<el-button size="mini" type="warning" plain @click="downMoveItem(scope.$index)"
icon="el-icon-caret-bottom"></el-button>
</template>
</el-table-column>
</el-table>
<div class="footer">
<el-button type="warning" @click="addAction">添加</el-button>
<el-button type="primary" @click="deviceDialogOpen=false">確定</el-button>
</div>
</div>
</el-dialog>6.實現(xiàn)表格項增減方法以及排序方法
// 彈窗-表格中增加一條數(shù)據(jù)
addAction() {
this.dialogTableData.push({
device: '', //綁定設(shè)備選項
action: '', //綁定設(shè)備動作選項
edit: true, // 默認(rèn)可編輯
})
},
// 彈窗-刪除表格中的一條數(shù)據(jù)
delAction(index) {
this.dialogTableData.splice(index, 1)
},
// 上移元素
upMoveItem(index) {
let table = this.dialogTableData
if (index !== 0) {
table[index] = table.splice(index - 1, 1, table[index])[0]
} else {
table.push(table.shift())
}
},
// 下移元素
downMoveItem(index) {
let table = this.dialogTableData
if (index !== table.length - 1) {
table[index] = table.splice(index + 1, 1, table[index])[0];
} else {
table.unshift(table.splice(index, 1)[0]);
}
},總結(jié)
- 需要明確嵌套層級,vue組件要綁定到正確的變量上
- 為某個選項增加具體動作時,要獲取到索引,這樣才能根據(jù)索引獲取該選項下的數(shù)據(jù)
- 選項的增刪和排序都利用了
splice()函數(shù)
到此這篇關(guān)于Element+Vue實現(xiàn)動態(tài)表單(多個下拉框組件)的文章就介紹到這了,更多相關(guān)Element Vue動態(tài)表單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項目優(yōu)雅實現(xiàn)自動引入組件的方法詳解
在我們寫vue項目的時候,都會引入一些組件庫,有時候有可能還不止一個組件庫,那么如何優(yōu)雅的實現(xiàn)自動引入組件呢,下面小編就來和大家詳細(xì)講講吧2023-09-09
vue實現(xiàn)彈框遮罩點擊其他區(qū)域彈框關(guān)閉及v-if與v-show的區(qū)別介紹
vue如何簡單的實現(xiàn)彈框,遮罩,點擊其他區(qū)域關(guān)閉彈框, 簡單的思路是以一個div作為遮罩,這篇文章給大家詳細(xì)介紹了vue實現(xiàn)彈框遮罩點擊其他區(qū)域彈框關(guān)閉及v-if與v-show的區(qū)別介紹,感興趣的朋友一起看看吧2018-09-09
ElementUI如何修改Dialog的標(biāo)題樣式
這篇文章主要介紹了ElementUI如何修改Dialog的標(biāo)題樣式問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue+ElementUI容器無法鋪滿網(wǎng)頁的問題解決
這篇文章主要介紹了Vue+ElementUI容器無法鋪滿網(wǎng)頁的問題解決,文章通過圖文結(jié)合的方式給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-08-08

