vue + ele 实现下拉选择框和下拉多选选择框处理方案
更新时间:2023年08月01日 12:06:06 作者:牧羊人の冬天
这篇文章主要介绍了vue + ele 实现下拉选择框和下拉多选选择框处理方案,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
效果图如下:

<!-- 添加项目的弹框 --> <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="负责人" > <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);
}},
// 获取所有机构
async getorgInfo() {
try {
const response = await this.$api.getOrgInfo();
if (response.data.code === "0"){
this.departmentTree = [response.data.result];
}
} catch (error) {
console.error(error);
}},
// 点击进入项目
clickView(pro) {
// 将选中的项目信息保存的vuex
this.selectPro(pro);
// 路由跳转
this.$router.push({ name: 'home' });
},
// 点击添加项目按钮
clickAdd() {
// 将添加表单置空
this.addForm = {
name: '',
leader: [],
org:[]
};
// 显示模态框
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);
// 保存默认值
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: '取消删除'
});
});
},
//在机构列表中根据id查找对应节点的label
findOrgNodeById(nodes, id) {
for (const node of nodes) {
if (node.id === id) {
return node;
}
// 递归查找子节点
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);
//根据部门查找部门名称
const selectedOrgLabels = selectedOrgIds.map(orgId => {
const orgNode = this.findOrgNodeById(this.departmentTree, orgId);
return orgNode && orgNode.name ? orgNode.name : "";
});
//组装部门数据
const orgData = selectedOrgIds.map((id, index) => {
return { orgId: id, orgName: selectedOrgLabels[index] };
});
//组装请求数据
const data = {
name: this.addForm.name,
org:orgData,
// 根据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
});
// 刷新页面数据
this.getAllPro();
this.addDlg = false;
}},接口返回的数据结构如下:

到此这篇关于vue + ele 下拉选择框和下拉多选选择框处理的文章就介绍到这了,更多相关vue 下拉选择框内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
基于Vue+ELement搭建动态树与数据表格实现分页模糊查询实战全过程
这篇文章主要给大家介绍了关于如何基于Vue+ELement搭建动态树与数据表格实现分页模糊查询的相关资料,Vue Element UI提供了el-pagination组件来实现表格数据的分页功能,需要的朋友可以参考下2023-10-10
基于Vue.js的文件选择与多选对话框组件Dccfile的使用教程
在现代前端开发中,Vue.js 提供了强大的组件化开发能力,使得我们可以轻松构建复杂且可复用的用户界面,本文将介绍一个基于 Vue.js 的文件选择与多选对话框组件——Dccfile,并详细解析其功能和实现细节2025-04-04
Vue build过程取消console debugger控制台信息输出方法详解
这篇文章主要为大家介绍了Vue build过程取消console debugger控制台信息输出方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-09-09


最新评论