vue?+?ele?實現(xiàn)下拉選擇框和下拉多選選擇框處理方案
更新時間:2023年08月01日 12:06:06 作者:牧羊人の冬天
這篇文章主要介紹了vue?+?ele?實現(xiàn)下拉選擇框和下拉多選選擇框處理方案,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
效果圖如下:
<!-- 添加項目的彈框 --> <el-dialog v-model="addDlg" class="pro_dialog" title="添加項目" width="40%"> <el-form :model="addForm"> <el-form-item label="項目名"><el-input v-model="addForm.name" autocomplete="off" class="input-field"/></el-form-item> <el-form-item label="部門"> <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>
// 獲取所有用戶 async getLeaders() { try { const response = await this.$api.getUsers(); this.leaders = response.data.result.records; } catch (error) { console.error(error); }}, // 獲取所有機構(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); }}, // 點擊進(jìn)入項目 clickView(pro) { // 將選中的項目信息保存的vuex this.selectPro(pro); // 路由跳轉(zhuǎn) this.$router.push({ name: 'home' }); }, // 點擊添加項目按鈕 clickAdd() { // 將添加表單置空 this.addForm = { name: '', leader: [], org:[] }; // 顯示模態(tài)框 this.addDlg = true; }, // 點擊編輯項目按鈕 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; } }) }, // 點擊刪除按鈕 clickDelete(id) { ElMessageBox.confirm('確定要刪除該項目嗎?', '提示', { confirmButtonText: '確定', cancelButtonText: '取消', type: 'warning' }) .then(() => { this.deletePro(id); }) .catch(() => { ElMessage({ type: 'info', message: '取消刪除' }); }); }, //在機構(gòu)列表中根據(jù)id查找對應(yīng)節(jié)點的label findOrgNodeById(nodes, id) { for (const node of nodes) { if (node.id === id) { return node; } // 遞歸查找子節(jié)點 if (node.childOrg && node.childOrg.length > 0) { const foundNode = this.findOrgNodeById(node.childOrg, id); if (foundNode) { return foundNode; }}} return null; }, // 添加項目 async addPro() { // 獲取選中的部門id const selectedOrgIds = this.$refs.departmentTreeRef.getCheckedNodes().map(node => node.id); //根據(jù)部門查找部門名稱 const selectedOrgLabels = selectedOrgIds.map(orgId => { const orgNode = this.findOrgNodeById(this.departmentTree, orgId); return orgNode && orgNode.name ? orgNode.name : ""; }); //組裝部門數(shù)據(jù) const orgData = selectedOrgIds.map((id, index) => { return { orgId: id, orgName: selectedOrgLabels[index] }; }); //組裝請求數(shù)據(jù) const data = { name: this.addForm.name, org:orgData, // 根據(jù)id獲取用戶名 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 }); // 刷新頁面數(shù)據(jù) this.getAllPro(); this.addDlg = false; }},
接口返回的數(shù)據(jù)結(jié)構(gòu)如下:
到此這篇關(guān)于vue + ele 下拉選擇框和下拉多選選擇框處理的文章就介紹到這了,更多相關(guān)vue 下拉選擇框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Vue+ELement搭建動態(tài)樹與數(shù)據(jù)表格實現(xiàn)分頁模糊查詢實戰(zhàn)全過程
這篇文章主要給大家介紹了關(guān)于如何基于Vue+ELement搭建動態(tài)樹與數(shù)據(jù)表格實現(xiàn)分頁模糊查詢的相關(guān)資料,Vue Element UI提供了el-pagination組件來實現(xiàn)表格數(shù)據(jù)的分頁功能,需要的朋友可以參考下2023-10-10Vue 中使用lodash對事件進(jìn)行防抖和節(jié)流操作
這篇文章主要介紹了Vue 中使用lodash對事件進(jìn)行防抖和節(jié)流操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07基于Vue.js的文件選擇與多選對話框組件Dccfile的使用教程
在現(xiàn)代前端開發(fā)中,Vue.js 提供了強大的組件化開發(fā)能力,使得我們可以輕松構(gòu)建復(fù)雜且可復(fù)用的用戶界面,本文將介紹一個基于 Vue.js 的文件選擇與多選對話框組件——Dccfile,并詳細(xì)解析其功能和實現(xiàn)細(xì)節(jié)2025-04-04Vue build過程取消console debugger控制臺信息輸出方法詳解
這篇文章主要為大家介紹了Vue build過程取消console debugger控制臺信息輸出方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09