vue?+?ele?實(shí)現(xiàn)下拉選擇框和下拉多選選擇框處理方案
效果圖如下:
<!-- 添加項(xiàng)目的彈框 --> <el-dialog v-model="addDlg" class="pro_dialog" title="添加項(xiàng)目" width="40%"> <el-form :model="addForm"> <el-form-item label="項(xiàng)目名"><el-input v-model="addForm.name" autocomplete="off" class="input-field"/></el-form-item> <el-form-item label="部門(mén)"> <el-tree ref="departmentTreeRef" :data="departmentTree" :props="departmentTreeProps" show-checkbox highlight-current node-key="id" v-model="addForm.org" ></el-tree> </el-form-item> <el-form-item label="負(fù)責(zé)人" > <el-select v-model="addForm.leader" autocomplete="off" multiple filterable class="input-field"> <el-option v-for="leader in filterLeaders" :key="leader.id" :label="leader.userName" :value="leader.id"></el-option> </el-select> </el-form-item> </el-form> <template #footer> <span class="dialog-footer"> <el-button @click="addDlg = false" size="mini">取消</el-button> <el-button type="primary" @click="addPro" size="mini">確定</el-button> </span> </template> </el-dialog>
// 獲取所有用戶(hù) async getLeaders() { try { const response = await this.$api.getUsers(); this.leaders = response.data.result.records; } catch (error) { console.error(error); }}, // 獲取所有機(jī)構(gòu) async getorgInfo() { try { const response = await this.$api.getOrgInfo(); if (response.data.code === "0"){ this.departmentTree = [response.data.result]; } } catch (error) { console.error(error); }}, // 點(diǎn)擊進(jìn)入項(xiàng)目 clickView(pro) { // 將選中的項(xiàng)目信息保存的vuex this.selectPro(pro); // 路由跳轉(zhuǎn) this.$router.push({ name: 'home' }); }, // 點(diǎn)擊添加項(xiàng)目按鈕 clickAdd() { // 將添加表單置空 this.addForm = { name: '', leader: [], org:[] }; // 顯示模態(tài)框 this.addDlg = true; }, // 點(diǎn)擊編輯項(xiàng)目按鈕 clickEdit(item) { this.updateForm = { id:item.id, name:item.name, leader:item.leader.map(leader => leader.userId), org:item.org.map(org => this.findOrgNodeById(this.departmentTree,org.orgId)) }; this.defaultCheckedKeys = this.updateForm.org.map(org => org.id); // 保存默認(rèn)值 const temp = [...this.defaultCheckedKeys]; this.updateDlg = true; this.$nextTick(() => { if (this.$refs.departmentTreeRef){ // 用$refs清空已勾選的,否則不生效,重新勾選, this.$refs.departmentTreeRef.setCheckedKeys([]); this.defaultCheckedKeys = temp; } }) }, // 點(diǎn)擊刪除按鈕 clickDelete(id) { ElMessageBox.confirm('確定要?jiǎng)h除該項(xiàng)目嗎?', '提示', { confirmButtonText: '確定', cancelButtonText: '取消', type: 'warning' }) .then(() => { this.deletePro(id); }) .catch(() => { ElMessage({ type: 'info', message: '取消刪除' }); }); }, //在機(jī)構(gòu)列表中根據(jù)id查找對(duì)應(yīng)節(jié)點(diǎn)的label findOrgNodeById(nodes, id) { for (const node of nodes) { if (node.id === id) { return node; } // 遞歸查找子節(jié)點(diǎn) if (node.childOrg && node.childOrg.length > 0) { const foundNode = this.findOrgNodeById(node.childOrg, id); if (foundNode) { return foundNode; }}} return null; }, // 添加項(xiàng)目 async addPro() { // 獲取選中的部門(mén)id const selectedOrgIds = this.$refs.departmentTreeRef.getCheckedNodes().map(node => node.id); //根據(jù)部門(mén)查找部門(mén)名稱(chēng) const selectedOrgLabels = selectedOrgIds.map(orgId => { const orgNode = this.findOrgNodeById(this.departmentTree, orgId); return orgNode && orgNode.name ? orgNode.name : ""; }); //組裝部門(mén)數(shù)據(jù) const orgData = selectedOrgIds.map((id, index) => { return { orgId: id, orgName: selectedOrgLabels[index] }; }); //組裝請(qǐng)求數(shù)據(jù) const data = { name: this.addForm.name, org:orgData, // 根據(jù)id獲取用戶(hù)名 leader: this.addForm.leader.map(id => { const leaderinfo = this.leaders.find(leader => leader.id==id); return{ userId:id, userName:leaderinfo.userName }})}; const response = await this.$api.createProjects(data); if (response.status === 201) { this.$message({ message: '添加成功!', type: 'success', duration: 1000 }); // 刷新頁(yè)面數(shù)據(jù) this.getAllPro(); this.addDlg = false; }},
接口返回的數(shù)據(jù)結(jié)構(gòu)如下:
到此這篇關(guān)于vue + ele 下拉選擇框和下拉多選選擇框處理的文章就介紹到這了,更多相關(guān)vue 下拉選擇框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Vue+ELement搭建動(dòng)態(tài)樹(shù)與數(shù)據(jù)表格實(shí)現(xiàn)分頁(yè)模糊查詢(xún)實(shí)戰(zhàn)全過(guò)程
這篇文章主要給大家介紹了關(guān)于如何基于Vue+ELement搭建動(dòng)態(tài)樹(shù)與數(shù)據(jù)表格實(shí)現(xiàn)分頁(yè)模糊查詢(xún)的相關(guān)資料,Vue Element UI提供了el-pagination組件來(lái)實(shí)現(xiàn)表格數(shù)據(jù)的分頁(yè)功能,需要的朋友可以參考下2023-10-10Vue 中使用lodash對(duì)事件進(jìn)行防抖和節(jié)流操作
這篇文章主要介紹了Vue 中使用lodash對(duì)事件進(jìn)行防抖和節(jié)流操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07Vue實(shí)現(xiàn)開(kāi)關(guān)按鈕拖拽效果
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)開(kāi)關(guān)按鈕拖拽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09詳解Vue組件之間的數(shù)據(jù)通信實(shí)例
本篇文章主要介紹了詳解Vue組件之間的數(shù)據(jù)通信實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06基于Vue.js的文件選擇與多選對(duì)話(huà)框組件Dccfile的使用教程
在現(xiàn)代前端開(kāi)發(fā)中,Vue.js 提供了強(qiáng)大的組件化開(kāi)發(fā)能力,使得我們可以輕松構(gòu)建復(fù)雜且可復(fù)用的用戶(hù)界面,本文將介紹一個(gè)基于 Vue.js 的文件選擇與多選對(duì)話(huà)框組件——Dccfile,并詳細(xì)解析其功能和實(shí)現(xiàn)細(xì)節(jié)2025-04-04Vue build過(guò)程取消console debugger控制臺(tái)信息輸出方法詳解
這篇文章主要為大家介紹了Vue build過(guò)程取消console debugger控制臺(tái)信息輸出方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09