2019-05-19 18:54:09 +08:00
|
|
|
<template>
|
2020-07-11 12:54:57 +08:00
|
|
|
<j-modal
|
2019-05-19 18:54:09 +08:00
|
|
|
title="选择部门"
|
|
|
|
:width="modalWidth"
|
|
|
|
:visible="visible"
|
|
|
|
:confirmLoading="confirmLoading"
|
|
|
|
@ok="handleSubmit"
|
|
|
|
@cancel="handleCancel"
|
2021-08-13 15:26:20 +08:00
|
|
|
@update:fullscreen="isFullscreen"
|
2021-01-23 23:59:31 +08:00
|
|
|
wrapClassName="j-depart-select-modal"
|
2020-07-11 12:54:57 +08:00
|
|
|
switchFullscreen
|
2019-05-19 18:54:09 +08:00
|
|
|
cancelText="关闭">
|
|
|
|
<a-spin tip="Loading..." :spinning="false">
|
2022-02-24 15:13:05 +08:00
|
|
|
<a-input-search v-model="searchValue" style="margin-bottom: 1px" placeholder="请输入部门名称按回车进行搜索" />
|
|
|
|
<a-empty v-if="filterTreeData.length===0"></a-empty>
|
2019-05-19 18:54:09 +08:00
|
|
|
<a-tree
|
2022-02-24 15:13:05 +08:00
|
|
|
v-else
|
2019-05-19 18:54:09 +08:00
|
|
|
checkable
|
2021-08-13 15:26:20 +08:00
|
|
|
:class="treeScreenClass"
|
2022-02-24 15:13:05 +08:00
|
|
|
:treeData="filterTreeData"
|
2021-08-13 15:26:20 +08:00
|
|
|
:checkStrictly="checkStrictly"
|
2019-05-19 18:54:09 +08:00
|
|
|
@check="onCheck"
|
|
|
|
@select="onSelect"
|
|
|
|
@expand="onExpand"
|
|
|
|
:autoExpandParent="autoExpandParent"
|
|
|
|
:expandedKeys="expandedKeys"
|
|
|
|
:checkedKeys="checkedKeys">
|
|
|
|
|
|
|
|
</a-tree>
|
|
|
|
</a-spin>
|
2021-08-13 15:26:20 +08:00
|
|
|
<!--底部父子关联操作和确认取消按钮-->
|
|
|
|
<template slot="footer" v-if="treeOpera && multi">
|
|
|
|
<div class="drawer-bootom-button">
|
|
|
|
<a-dropdown style="float: left" :trigger="['click']" placement="topCenter">
|
|
|
|
<a-menu slot="overlay">
|
|
|
|
<a-menu-item key="1" @click="switchCheckStrictly(1)">父子关联</a-menu-item>
|
|
|
|
<a-menu-item key="2" @click="switchCheckStrictly(2)">取消关联</a-menu-item>
|
|
|
|
</a-menu>
|
|
|
|
<a-button>
|
|
|
|
树操作 <a-icon type="up" />
|
|
|
|
</a-button>
|
|
|
|
</a-dropdown>
|
|
|
|
<a-button @click="handleCancel" type="primary" style="margin-right: 0.8rem">关闭</a-button>
|
|
|
|
<a-button @click="handleSubmit" type="primary" >确认</a-button>
|
|
|
|
</div>
|
|
|
|
</template>
|
2020-07-11 12:54:57 +08:00
|
|
|
</j-modal>
|
2019-05-19 18:54:09 +08:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import { queryDepartTreeList } from '@/api/api'
|
|
|
|
export default {
|
|
|
|
name: 'JSelectDepartModal',
|
2021-08-13 15:26:20 +08:00
|
|
|
props:['modalWidth','multi','rootOpened','departId', 'store', 'text','treeOpera'],
|
2019-05-19 18:54:09 +08:00
|
|
|
data(){
|
|
|
|
return {
|
|
|
|
visible:false,
|
|
|
|
confirmLoading:false,
|
|
|
|
treeData:[],
|
|
|
|
autoExpandParent:true,
|
|
|
|
expandedKeys:[],
|
|
|
|
dataList:[],
|
|
|
|
checkedKeys:[],
|
|
|
|
checkedRows:[],
|
2021-08-13 15:26:20 +08:00
|
|
|
searchValue:"",
|
|
|
|
checkStrictly: true,
|
|
|
|
fullscreen:false
|
2019-05-19 18:54:09 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
created(){
|
|
|
|
this.loadDepart();
|
|
|
|
},
|
|
|
|
watch:{
|
|
|
|
departId(){
|
|
|
|
this.initDepartComponent()
|
2019-08-24 00:15:45 +08:00
|
|
|
},
|
|
|
|
visible: {
|
|
|
|
handler() {
|
2021-08-13 15:26:20 +08:00
|
|
|
this.initDepartComponent(true)
|
2019-08-24 00:15:45 +08:00
|
|
|
}
|
2019-05-19 18:54:09 +08:00
|
|
|
}
|
|
|
|
},
|
2021-08-13 15:26:20 +08:00
|
|
|
computed:{
|
|
|
|
treeScreenClass() {
|
|
|
|
return {
|
|
|
|
'my-dept-select-tree': true,
|
|
|
|
'fullscreen': this.fullscreen,
|
|
|
|
}
|
|
|
|
},
|
2022-02-24 15:13:05 +08:00
|
|
|
filterTreeData(){
|
|
|
|
if(!this.searchValue){
|
|
|
|
return this.treeData
|
|
|
|
}
|
|
|
|
let filter = []
|
|
|
|
this.dataList.forEach((item) => {
|
|
|
|
if (item.title.includes(this.searchValue)) {
|
|
|
|
filter.push(Object.assign({}, item, {children: null, isLeaf: true}))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return filter
|
|
|
|
},
|
2021-08-13 15:26:20 +08:00
|
|
|
},
|
2019-05-19 18:54:09 +08:00
|
|
|
methods:{
|
|
|
|
show(){
|
|
|
|
this.visible=true
|
|
|
|
this.checkedRows=[]
|
|
|
|
this.checkedKeys=[]
|
|
|
|
},
|
|
|
|
loadDepart(){
|
2021-08-13 15:26:20 +08:00
|
|
|
// 这个方法是找到所有的部门信息
|
2019-05-19 18:54:09 +08:00
|
|
|
queryDepartTreeList().then(res=>{
|
|
|
|
if(res.success){
|
|
|
|
let arr = [...res.result]
|
|
|
|
this.reWriterWithSlot(arr)
|
|
|
|
this.treeData = arr
|
|
|
|
this.initDepartComponent()
|
|
|
|
if(this.rootOpened){
|
|
|
|
this.initExpandedKeys(res.result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2021-08-13 15:26:20 +08:00
|
|
|
initDepartComponent(flag){
|
|
|
|
let arr = []
|
|
|
|
//该方法两个地方用 1.visible改变事件重新设置选中项 2.组件编辑页面回显
|
|
|
|
let fieldName = flag==true?'key':this.text
|
2019-05-19 18:54:09 +08:00
|
|
|
if(this.departId){
|
2021-08-13 15:26:20 +08:00
|
|
|
let arr2 = this.departId.split(',')
|
2019-05-19 18:54:09 +08:00
|
|
|
for(let item of this.dataList){
|
2021-08-13 15:26:20 +08:00
|
|
|
if(arr2.indexOf(item[this.store])>=0){
|
|
|
|
arr.push(item[fieldName])
|
2019-05-19 18:54:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-13 15:26:20 +08:00
|
|
|
if(flag==true){
|
|
|
|
this.checkedKeys = [...arr]
|
|
|
|
}else{
|
|
|
|
this.$emit("initComp", arr.join(','))
|
|
|
|
}
|
2019-05-19 18:54:09 +08:00
|
|
|
},
|
|
|
|
reWriterWithSlot(arr){
|
|
|
|
for(let item of arr){
|
|
|
|
if(item.children && item.children.length>0){
|
|
|
|
this.reWriterWithSlot(item.children)
|
|
|
|
let temp = Object.assign({},item)
|
|
|
|
temp.children = {}
|
|
|
|
this.dataList.push(temp)
|
|
|
|
}else{
|
|
|
|
this.dataList.push(item)
|
|
|
|
item.scopedSlots={ title: 'title' }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
initExpandedKeys(arr){
|
|
|
|
if(arr && arr.length>0){
|
|
|
|
let keys = []
|
|
|
|
for(let item of arr){
|
|
|
|
if(item.children && item.children.length>0){
|
|
|
|
keys.push(item.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.expandedKeys=[...keys]
|
2021-08-13 15:26:20 +08:00
|
|
|
//全部keys
|
|
|
|
//this.allTreeKeys = [...keys]
|
2019-05-19 18:54:09 +08:00
|
|
|
}else{
|
|
|
|
this.expandedKeys=[]
|
2021-08-13 15:26:20 +08:00
|
|
|
//this.allTreeKeys = []
|
2019-05-19 18:54:09 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
onCheck (checkedKeys,info) {
|
|
|
|
if(!this.multi){
|
2019-11-21 18:17:25 +08:00
|
|
|
let arr = checkedKeys.checked.filter(item => this.checkedKeys.indexOf(item) < 0)
|
2019-05-19 18:54:09 +08:00
|
|
|
this.checkedKeys = [...arr]
|
2019-11-21 18:17:25 +08:00
|
|
|
this.checkedRows = (this.checkedKeys.length === 0) ? [] : [info.node.dataRef]
|
2019-05-19 18:54:09 +08:00
|
|
|
}else{
|
2021-08-13 15:26:20 +08:00
|
|
|
if(this.checkStrictly){
|
|
|
|
this.checkedKeys = checkedKeys.checked
|
|
|
|
}else{
|
|
|
|
this.checkedKeys = checkedKeys
|
|
|
|
}
|
2019-11-21 18:17:25 +08:00
|
|
|
this.checkedRows = this.getCheckedRows(this.checkedKeys)
|
2019-05-19 18:54:09 +08:00
|
|
|
}
|
|
|
|
},
|
2019-11-21 18:17:25 +08:00
|
|
|
onSelect(selectedKeys,info) {
|
2021-08-13 15:26:20 +08:00
|
|
|
//取消关联的情况下才走onSelect的逻辑
|
|
|
|
if(this.checkStrictly){
|
|
|
|
let keys = []
|
|
|
|
keys.push(selectedKeys[0])
|
|
|
|
if(!this.checkedKeys || this.checkedKeys.length===0 || !this.multi){
|
|
|
|
this.checkedKeys = [...keys]
|
|
|
|
this.checkedRows=[info.node.dataRef]
|
2019-05-19 18:54:09 +08:00
|
|
|
}else{
|
2021-08-13 15:26:20 +08:00
|
|
|
let currKey = info.node.dataRef.key
|
|
|
|
if(this.checkedKeys.indexOf(currKey)>=0){
|
|
|
|
this.checkedKeys = this.checkedKeys.filter(item=> item !==currKey)
|
|
|
|
}else{
|
|
|
|
this.checkedKeys.push(...keys)
|
|
|
|
}
|
2019-05-19 18:54:09 +08:00
|
|
|
}
|
2021-08-13 15:26:20 +08:00
|
|
|
this.checkedRows = this.getCheckedRows(this.checkedKeys)
|
2019-05-19 18:54:09 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
onExpand (expandedKeys) {
|
|
|
|
this.expandedKeys = expandedKeys
|
|
|
|
this.autoExpandParent = false
|
|
|
|
},
|
|
|
|
handleSubmit(){
|
|
|
|
if(!this.checkedKeys || this.checkedKeys.length==0){
|
|
|
|
this.$emit("ok",'')
|
|
|
|
}else{
|
2021-03-17 18:40:08 +08:00
|
|
|
let checkRow = this.getCheckedRows(this.checkedKeys)
|
|
|
|
let keyStr = this.checkedKeys.join(",")
|
|
|
|
this.$emit("ok", checkRow, keyStr)
|
2019-05-19 18:54:09 +08:00
|
|
|
}
|
|
|
|
this.handleClear()
|
|
|
|
},
|
|
|
|
handleCancel(){
|
|
|
|
this.handleClear()
|
|
|
|
},
|
|
|
|
handleClear(){
|
|
|
|
this.visible=false
|
|
|
|
this.checkedKeys=[]
|
|
|
|
},
|
|
|
|
getParentKey(currKey,treeData){
|
|
|
|
let parentKey
|
|
|
|
for (let i = 0; i < treeData.length; i++) {
|
|
|
|
const node = treeData[i]
|
|
|
|
if (node.children) {
|
|
|
|
if (node.children.some(item => item.key === currKey)) {
|
|
|
|
parentKey = node.key
|
|
|
|
} else if (this.getParentKey(currKey, node.children)) {
|
|
|
|
parentKey = this.getParentKey(currKey, node.children)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return parentKey
|
2019-11-21 18:17:25 +08:00
|
|
|
},
|
|
|
|
// 根据 checkedKeys 获取 rows
|
|
|
|
getCheckedRows(checkedKeys) {
|
|
|
|
const forChildren = (list, key) => {
|
|
|
|
for (let item of list) {
|
|
|
|
if (item.id === key) {
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
if (item.children instanceof Array) {
|
|
|
|
let value = forChildren(item.children, key)
|
|
|
|
if (value != null) {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
let rows = []
|
|
|
|
for (let key of checkedKeys) {
|
|
|
|
let row = forChildren(this.treeData, key)
|
|
|
|
if (row != null) {
|
|
|
|
rows.push(row)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rows
|
2021-08-13 15:26:20 +08:00
|
|
|
},
|
|
|
|
switchCheckStrictly (v) {
|
|
|
|
if(v==1){
|
|
|
|
this.checkStrictly = false
|
|
|
|
}else if(v==2){
|
|
|
|
this.checkStrictly = true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
isFullscreen(val){
|
|
|
|
this.fullscreen=val
|
2019-05-19 18:54:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
2020-09-13 18:23:23 +08:00
|
|
|
<style lang="less" scoped>
|
|
|
|
// 限制部门选择树高度,避免部门太多时点击确定不便
|
|
|
|
.my-dept-select-tree{
|
2021-08-13 15:26:20 +08:00
|
|
|
height:350px;
|
|
|
|
|
|
|
|
&.fullscreen{
|
|
|
|
height: calc(100vh - 250px);
|
|
|
|
}
|
2020-09-13 18:23:23 +08:00
|
|
|
overflow-y: scroll;
|
|
|
|
}
|
2021-08-13 15:26:20 +08:00
|
|
|
.drawer-bootom-button {
|
|
|
|
position: absolute;
|
|
|
|
bottom: 0;
|
|
|
|
width: 100%;
|
|
|
|
border-top: 1px solid #e8e8e8;
|
|
|
|
padding: 10px 16px;
|
|
|
|
text-align: right;
|
|
|
|
left: 0;
|
|
|
|
background: #fff;
|
|
|
|
border-radius: 0 0 2px 2px;
|
|
|
|
}
|
2019-05-19 18:54:09 +08:00
|
|
|
</style>
|