个人记录
<template>
<div>
<el-container>
<!--头-->
<el-header style="background-color: #00c355" class="header">Header</el-header>
<!--中-->
<el-container>
<div width="100%">
<el-input placeholder="输入关键字进行过滤" v-model="filterText" style="width: 96%;padding: 3px 2px;">
</el-input>
<el-aside class="aside" style="background-color: #3a8ee6;padding: 4px 6px;">
<el-tree :data="cascadeData" :props="defaultProps" @node-click="handleNodeClick"
:filter-node-method="filterNode" node-key="id" :default-expanded-keys="[0]"
ref="tree"></el-tree>
</el-aside>
</div>
<el-main style="background-color: #ffffff" class="main">
<!--岗位级联单选组件 value:'id'作用:当change事件触发时,选中节点拿当前节点的id值!:show-all-levels="false" :输入框中是否显示选中值的完整路径 -->
<el-cascader :options="cascadeData" v-model="jobCacader"
:props="{ checkStrictly: true,...defaultProps2,value:'id'}" @change="jobCascaderChange" filterable
:show-all-levels="false" clearable>
</el-cascader>
</el-main>
</el-container>
<!--尾-->
<el-footer style="background-color: #eeeeee" class="footer">Footer</el-footer>
</el-container>
</div>
</template>
<script>
import Vue from '@/main';
export default {
name: "test.component.vue",
data() {
return {
cascadeData: [],//1.el-tree树数据 2. 岗位级联数据
defaultProps: {
children: 'children',//
label: 'nameCn'//el-tree显示标签
},
defaultProps2: {
children: 'children',//
label: 'nameCn2'//el-cacader级联菜单显示标签
},
filterText: '',//input输入框
jobCacader:[0,10859],//级联选择器的ngmodel值
}
},
watch: {
filterText(val) {
this.$refs.tree.filter(val);
},
jobCacader(){
//岗位ID,
}
},
methods: {
/**
* Auto:Greg_Zhong
* uodateDate:2022-1-22
* 查询所有岗位
* 岗位ID:id、岗位名称:nameCn、部门名称:departmentId、父部门ID:pId
*/
queryJob() {
Vue.$Request({
method: 'post',
url: '/api/hkc-mdm-organizational-structure/post/list',
data: {},
params: {}
}).then((res) => {
if (res.data.code === 200) {
// console.log("岗位数据:", JSON.stringify(res.data.data.dataList));
//将从服务器返回扁平化数据,转换成el-tree数据结构/ 级联数据
this.cascadeData = this.setTreeData(res.data.data.dataList);
//默认选中el-tree
// this.$nextTick(() => {
// document.querySelector('.el-tree-node__content').click();
// });
} else {
console.warn("请求岗位数据失败,服务器返回状态码:", res.data.code);
}
}).catch(error => {
this.original = [
{
"id": 10047,
"opCode": null,
"useStatus": 1,
"departmentId": 30758,
"nameCn": "测试数据1124",
"nameEn": null,
"pinyinSimple": null,
"pinyin": null,
"director": 0,
"postType": null,
"effectiveDate": null,
"expiryDate": null,
"memo": null,
"reportType": "HBLX01_SYS",
"pId": null,
"createTime": "2021-11-24 14:39:47",
"createUserId": null,
"updateTime": null,
"updateUserId": null,
"other": null,
"field1": null,
"field2": null,
"field3": null,
"departmentName": "数字化管理中心"
}, {
"id": 10011,
"opCode": null,
"useStatus": 1,
"departmentId": 30754,
"nameCn": "电商部长",
"nameEn": null,
"pinyinSimple": null,
"pinyin": null,
"director": 0,
"postType": null,
"effectiveDate": "2001-01-01 00:00:00",
"expiryDate": "2001-01-01 00:00:00",
"memo": "无",
"reportType": null,
"pId": null,
"createTime": "2021-11-24 14:37:05",
"createUserId": null,
"updateTime": null,
"updateUserId": null,
"other": null,
"field1": null,
"field2": null,
"field3": null,
"departmentName": "人力资源中心"
}, {
"id": 10010,
"opCode": null,
"useStatus": 1,
"departmentId": 30754,
"nameCn": "信息主管",
"nameEn": null,
"pinyinSimple": null,
"pinyin": null,
"director": 0,
"postType": null,
"effectiveDate": "2001-01-01 00:00:00",
"expiryDate": "2001-01-01 00:00:00",
"memo": null,
"reportType": null,
"pId": null,
"createTime": "2021-11-24 14:37:03",
"createUserId": null,
"updateTime": null,
"updateUserId": null,
"other": null,
"field1": null,
"field2": null,
"field3": null,
"departmentName": "人力资源中心"
}, {
"id": 10006,
"opCode": null,
"useStatus": 1,
"departmentId": 30754,
"nameCn": "行政管理",
"nameEn": null,
"pinyinSimple": null,
"pinyin": null,
"director": 1,
"postType": null,
"effectiveDate": null,
"expiryDate": null,
"memo": null,
"reportType": null,
"pId": null,
"createTime": "2021-11-24 14:37:01",
"createUserId": null,
"updateTime": null,
"updateUserId": null,
"other": null,
"field1": null,
"field2": null,
"field3": null,
"departmentName": "人力资源中心"
}, {
"id": 10005,
"opCode": null,
"useStatus": 1,
"departmentId": 30754,
"nameCn": "电商客服专员",
"nameEn": null,
"pinyinSimple": null,
"pinyin": null,
"director": 1,
"postType": null,
"effectiveDate": null,
"expiryDate": null,
"memo": null,
"reportType": null,
"pId": null,
"createTime": "2021-11-24 14:37:00",
"createUserId": null,
"updateTime": null,
"updateUserId": null,
"other": null,
"field1": null,
"field2": null,
"field3": null,
"departmentName": "人力资源中心"
}, {
"id": 10004,
"opCode": null,
"useStatus": 1,
"departmentId": 30758,
"nameCn": "信息专员",
"nameEn": null,
"pinyinSimple": null,
"pinyin": null,
"director": 1,
"postType": null,
"effectiveDate": null,
"expiryDate": null,
"memo": null,
"reportType": null,
"pId": 10000,
"createTime": "2021-11-16 11:13:46",
"createUserId": null,
"updateTime": null,
"updateUserId": null,
"other": null,
"field1": null,
"field2": null,
"field3": null,
"departmentName": "数字化管理中心"
}, {
"id": 10003,
"opCode": null,
"useStatus": 1,
"departmentId": 30758,
"nameCn": "软件测试",
"nameEn": null,
"pinyinSimple": null,
"pinyin": null,
"director": 1,
"postType": null,
"effectiveDate": null,
"expiryDate": null,
"memo": null,
"reportType": null,
"pId": 10000,
"createTime": "2021-11-16 11:13:44",
"createUserId": null,
"updateTime": null,
"updateUserId": null,
"other": null,
"field1": null,
"field2": null,
"field3": null,
"departmentName": "数字化管理中心"
}, {
"id": 10002,
"opCode": null,
"useStatus": 1,
"departmentId": 30758,
"nameCn": "软件开发",
"nameEn": null,
"pinyinSimple": null,
"pinyin": null,
"director": 1,
"postType": null,
"effectiveDate": null,
"expiryDate": null,
"memo": null,
"reportType": null,
"pId": 10000,
"createTime": "2021-11-16 11:13:43",
"createUserId": null,
"updateTime": null,
"updateUserId": null,
"other": null,
"field1": null,
"field2": null,
"field3": null,
"departmentName": "数字化管理中心"
}, {
"id": 10001,
"opCode": null,
"useStatus": 1,
"departmentId": 30758,
"nameCn": "软件架构",
"nameEn": null,
"pinyinSimple": null,
"pinyin": null,
"director": 1,
"postType": null,
"effectiveDate": null,
"expiryDate": null,
"memo": null,
"reportType": null,
"pId": 10000,
"createTime": "2021-11-16 11:13:41",
"createUserId": null,
"updateTime": null,
"updateUserId": null,
"other": null,
"field1": null,
"field2": null,
"field3": null,
"departmentName": "数字化管理中心"
}, {
"id": 10000,
"opCode": null,
"useStatus": 1,
"departmentId": 30758,
"nameCn": "信息总监",
"nameEn": null,
"pinyinSimple": null,
"pinyin": null,
"director": 1,
"postType": null,
"effectiveDate": null,
"expiryDate": null,
"memo": null,
"reportType": null,
"pId": null,
"createTime": null,
"createUserId": null,
"updateTime": null,
"updateUserId": null,
"other": null,
"field1": null,
"field2": null,
"field3": null,
"departmentName": "数字化管理中心"
}
];
alert("el-tree树菜单报error信息:", error);
console.log("岗位el-tree树菜单报error信息:", error);
})
},
//扁平化数据转el-tree树结构!
setTreeData(data) {
//第一步:实现岗位和子岗位层级关系
data.forEach(function (item) {
delete item.children; // 删除所有的children,以防止多次调用
});
let map = {}; //构建map
data.forEach(i => {
map[i.id] = i; //构建以id为键 当前数据为值
});
let treeData = [];
data.forEach((child, index) => {
child.nameCn2 = child.nameCn;// 岗位名称
child.nameCn = `【${child.departmentName}】` + child.nameCn;//形式:【部门名称】+岗位名称
var mapItem;
mapItem = map[child.pId]; //判断当前数据的pId是否存在map中
//mapItem引用了data,所以mapItem改变后,data也会改变! 所以:这里的mapItem与child对象存在相互引用,如果使用JSON.tostringify()则会出现循环引用问题!
if (mapItem) {
(mapItem.children || (mapItem.children = [])).push(child); //这里判断mapItem中是否存在child对象
// console.log("会当凌绝顶伊朗总是想!", (mapItem));//这里使用comsole打印没问题,
// console.log("会当凌绝顶伊朗总是想!", (JSON.stringify(mapItem)));//使用JSON.tostringify就会报循环引用问题!
} else {
treeData.push(child);//不存在则是顶层岗位
}
// console.log("会当凌绝顶!",(mapItem));
});
// console.log('br/' + "封装成el-tree数据结构的(岗位)数组:+br/", JSON.stringify(treeData));
//第二步:将岗位层级数组放到“恒康医药集团”下面作为children
let resultObject = [{id: 0, nameCn: '恒康医药集团', nameCn2: '恒康医药集团', pId: null}];
resultObject[0].children = treeData;//将封装好的岗位层级,嵌套到最外层;
return resultObject;
},
//获取el-tree选中的元素
handleNodeClick(data) {
console.log(data);
},
//配合input框实现过滤元素 | 对树节点进行筛选时执行的方法,返回 true 表示这个节点可以显示,返回 false 则表示这个节点会被隐藏
filterNode(value, data) {
if (!value) return true;
return data.nameCn.indexOf(value) !== -1;//注意这里data.nameCn的nameCn,需要和defaultProps.label里的键保持一致!
},
//岗位级联菜单,获取选中节点的元素
jobCascaderChange(val) {
console.log("天地广阔,任我翱翔", val);
},
},
mounted() {
//查询岗位列表
this.queryJob();
}
}
</script>
<!--scoped-->
<style>
.el-tree {
color: #000;
}
.el-menu--horizontal > .el-menu-item.is-active {
/* 顶部菜单项选中时的颜色 */
border-bottom: 2px solid #009944;
/* 顶部菜单栏底线颜色 */
color: #303133;
}
.el-tree-node__expand-icon {
/* 树形菜单三角标颜色 */
color: #999;
}
.el-tree-node__content:hover {
/* 树形菜单鼠标悬浮的字体颜色及背景颜色 */
color: #ffffff;
background-color: #009944;
}
.el-tree-node:focus > .el-tree-node__content {
/* 树形菜单选中时的字体颜色及背景颜色 */
color: #ffffff;
background-color: #009944;
padding: 0px 12px;;
}
.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
/* 树形菜单选中时的字体颜色及背景颜色 */
color: #ffffff;
background-color: #009944;
}
</style>