diff --git a/src/views/mdp/sys/dept/DeptTree.vue b/src/views/mdp/sys/dept/DeptTree.vue index 802551c8..73a1e2fe 100644 --- a/src/views/mdp/sys/dept/DeptTree.vue +++ b/src/views/mdp/sys/dept/DeptTree.vue @@ -1,17 +1,21 @@ @@ -73,22 +79,22 @@ return [this.value]; } return this.checkedKeys; - }, - nodeKey_(){ - return this.nodeKey?this.nodeKey:'deptid' - }, + }, ...mapGetters([ - 'userInfo' - ]) + 'workShop' + ]), + treeData(){ + return this.translateDataToTree(this.depts) + } }, - props: ['value','branchId','visible','nodeKey','showCount','showRoot','countTips','showFilter','rootKey','multiple','checkedKeys','refresh','defaultExpandAll','expandOnClickNode','showCheckbox','indent'], + props: ['value','branchId','visible', 'showCount','showRoot','countTips','showFilter', 'multiple','checkedKeys','refresh','defaultExpandAll','expandOnClickNode','showCheckbox','indent'], data() { return { deptFilterText: '', - deptTreeData:[], + depts:[], defaultDeptTreeProps:{ - id:this.nodeKey_, + id:'deptid', label:'deptName', children: 'children' }, @@ -105,13 +111,13 @@ if( this.multiple===undefined || this.multiple===false||this.multiple==='false'){ if(checked==true){ if(checkedKeys.length>1){ - this.$refs.deptTree.setCheckedKeys([data[this.nodeKey_]]); + this.$refs.deptTree.setCheckedKeys([data['deptid']]); this.$emit('check-change',data,checked,indeterminate); - this.deptid=data[this.nodeKey_]; + this.deptid=data['deptid']; }else{ this.$emit('check-change',data,checked,indeterminate); - this.deptid=data[this.nodeKey_]; - } + this.deptid=data['deptid']; + } }else{ if(checkedKeys.length==0){ this.deptid=''; @@ -136,45 +142,87 @@ } this.$emit('branch-row-click',row, event, column);// @branch-row-click="rowClick" + }, + translateDataToTree(data) { + if(!data){ + return []; + } + let parents = data.filter(value =>{ + //如果我的上级为空,则我是最上级 + if(value.pdeptid == 'undefined' || value.pdeptid == null || value.pdeptid == '' || value.pdeptid == 'A0'){ + return true; + + //如果我的上级不在列表中,我作为最上级 + }else if(data.some(i=>value.pdeptid==i.deptid)){ + return false; + }else { + return true + } + + }) + let children = data.filter(value =>{ + if(data.some(i=>value.pdeptid==i.deptid)){ + return true; + }else{ + return false; + } + }) + let translator = (parents, children) => { + parents.forEach((parent) => { + children.forEach((current, index) => { + if (current.pdeptid === parent.deptid) { + let temp = JSON.parse(JSON.stringify(children)) + temp.splice(index, 1) + translator([current], temp) + typeof parent.children !== 'undefined' ? parent.children.push(current) : parent.children = [current] + } + } + ) + } + ) + } + + translator(parents, children) + + return parents }, //获取分类树列表 getDeptTreeData(refresh) { - - let id=''; - if(this.rootKey!='' && this.rootKey!=null){ - id=this.rootKey; - } + let params = { - id: id - }; - if(refresh){ - params.refresh=true; - } + }; params.branchId=this.branchId - if(this.userInfo.isSuperAdmin==true || this.userInfo.isPlatformAdmin==true){ + if(this.workShop.isSuperAdmin==true || this.workShop.isPlatformAdmin==true){ if(this.branch!=null){ params.branchId=this.branch.id + }else if(params.branchId==null || params.branchId==''){ + params.branchId=this.workShop.branchId } + }else{ + if(params.branchId==null || params.branchId==''){ + params.branchId=this.workShop.branchId + } + params.autoDetectParentDeptid="1" } this.listLoading = true; listTreeDept(params).then((res) => { var tips=res.data.tips; + var data=res.data.data; if(tips.isOk==true){ - if( this.showRoot===undefined || this.showRoot===false||this.showRoot==='false'){ - let tree=res.data.data; - if(tree==null){ - this.deptTreeData=[]; + if( this.showRoot===undefined || this.showRoot===false||this.showRoot==='false'){ + if(data==null){ + this.depts=[]; }else{ if(this.branch!=null){ res.data.data.branchId=this.branch.id res.data.data.deptName=this.branch.branchName+'(机构)' res.data.data.isBranch=true }else { - res.data.data.branchId=this.userInfo.branchId - res.data.data.deptName=this.userInfo.branchName+'(机构)' + res.data.data.branchId=this.workShop.branchId + res.data.data.deptName=this.workShop.branchName+'(机构)' res.data.data.isBranch=true } - this.deptTreeData=tree.children; + this.depts=data } }else{ @@ -183,11 +231,11 @@ res.data.data.deptName=this.branch.branchName+'(机构)' res.data.data.isBranch=true }else { - res.data.data.branchId=this.userInfo.branchId - res.data.data.deptName=this.userInfo.branchName+'(机构)' + res.data.data.branchId=this.workShop.branchId + res.data.data.deptName=this.workShop.branchName+'(机构)' res.data.data.isBranch=true } - this.deptTreeData=[res.data.data]; + this.depts=data; } }else{ @@ -199,8 +247,15 @@ }); }, filterDeptNode(value, data) { - if (!value) return true; - return data.name.indexOf(value) !== -1; + if (!value) return true; + if( data.deptid.indexOf(value)>=0 || data.deptName.indexOf(value)>=0 ){ + return true; + }else{ + if( data.pdeptid && data.pdeptid.indexOf(value)>=0){ + return true; + } + } + return false; }, renderContent(h, { node, data, store }) { var countMsg=''; @@ -229,5 +284,14 @@ \ No newline at end of file