zhuguifei
2025-04-28 442928123f63ee497d766f9a7a14f0a6ee067e25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<template>
  <a-drawer
    :width='400'
    placement='left'
    :closable='false'
    :visible='visible'
    :get-container='false'
    :wrap-style="{ position: 'absolute' }"
    @close='onClose'
  >
    <template slot='title'>
      <div style='display: flex;justify-content: space-between;align-items: center'>
        <span>选择根节点</span>
        <a-button @click='onSelectSave'  size='small' type="primary">
          保存
        </a-button>
      </div>
    </template>
 
 
 
    <div style='background: #fff;height: 100%; padding: 10px 20px' >
      <a-input-search @search='onSearch' style='width:100%;'  placeholder='请输入部门名称' />
      <!--组织机构-->
      <a-tree
        showLine
        :defaultExpandAll='true'
        :selectedKeys='selectedKeys'
        :checkStrictly='true'
        @select='onSelect'
        :dropdownStyle="{maxHeight:'200px',overflow:'auto'}"
        :treeData='departTree'
        :autoExpandParent='autoExpandParent'
        :expandedKeys='iExpandedKeys'
        @expand='onExpand'
      />
 
    </div>
 
 
  </a-drawer>
</template>
 
<script>
import { queryMyDepartTreeList, searchByKeywords } from '@api/api'
import Vue from 'vue'
 
export default {
  name: 'CompanyLeftDrawer',
  data() {
    return {
      visible: false,
      treeData: [],
      departTree: [],
      loading: false,
      userIdentity: '',
      iExpandedKeys: [],
      selectedKeys: [],
      checkedKeys: [],
      autoExpandParent: true
    }
  }, methods: {
    onOpen() {
      this.visible = true
 
      this.loadTree()
 
    },
 
    onClose() {
      this.visible = false
    },
    onSearch(value) {
      let that = this
      if (value) {
        searchByKeywords({ keyWord: value, myDeptSearch: '1' }).then((res) => {
          if (res.success) {
            that.departTree = []
            for (let i = 0; i < res.result.length; i++) {
              let temp = res.result[i]
              that.departTree.push(temp)
            }
          } else {
            that.$message.warning(res.message)
          }
        })
      } else {
        that.loadTree()
      }
 
    },
    loadTree() {
      var that = this
      that.treeData = []
      that.departTree = []
      queryMyDepartTreeList().then((res) => {
        if (res.success && res.result) {
          for (let i = 0; i < res.result.length; i++) {
            let temp = res.result[i]
            that.treeData.push(temp)
            that.departTree.push(temp)
            that.setThisExpandedKeys(temp)
            // console.log(temp.id)
          }
          this.loading = false
        }
        that.userIdentity = res.message
      })
    },
    setThisExpandedKeys(node) {
      //只展开一级目录
      if (node.children && node.children.length > 0) {
        this.iExpandedKeys.push(node.key)
        //下方代码放开注释则默认展开所有节点
 
         for (let a = 0; a < node.children.length; a++) {
            this.setThisExpandedKeys(node.children[a])
          }
 
      }
    },
    onExpand(expandedKeys) {
      // console.log('onExpand', expandedKeys)
      // if not set autoExpandParent to false, if children expanded, parent can not collapse.
      // or, you can remove all expanded children keys.
      this.iExpandedKeys = expandedKeys
      this.autoExpandParent = false
    },
    onSelect(selectedKeys, e) {
      if (this.selectedKeys[0] !== selectedKeys[0]) {
        this.selectedKeys = [selectedKeys[0]]
        if(this.selectedKeys && this.selectedKeys.length > 0){
          this.$emit("selectDepart",this.selectedKeys[0])
        }
 
      }
      let record = e.node.dataRef
      this.checkedKeys.push(record.id)
 
    },
    onSelectSave(){
      this.$emit("ok",this.selectedKeys[0])
      this.onClose()
    }
  }
}
</script>
 
<style lang='less' scoped>
@import '~@assets/less/common.less';
/deep/ .ant-drawer-body{
  padding: 0;
}
 
 
 
</style>