Blame view

ant-design-vue-jeecg/src/components/jeecgbiz/modal/JSelectDepartModal.vue 8.26 KB
肖超群 authored
1
2
3
4
5
6
7
8
9
10
11
<template>
  <j-modal
    title="选择部门"
    :width="modalWidth"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
    @update:fullscreen="isFullscreen"
    wrapClassName="j-depart-select-modal"
    switchFullscreen
谭毅彬 authored
12
    :cancelText="$t('button.close')">
肖超群 authored
13
    <a-spin tip="Loading..." :spinning="false">
肖超群 authored
14
      <a-input-search v-model="searchValue" style="margin-bottom: 1px" placeholder="请输入部门名称按回车进行搜索"/>
肖超群 authored
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
      <a-empty v-if="filterTreeData.length===0"></a-empty>
      <a-tree
        v-else
        checkable
        :class="treeScreenClass"
        :treeData="filterTreeData"
        :checkStrictly="checkStrictly"
        @check="onCheck"
        @select="onSelect"
        @expand="onExpand"
        :autoExpandParent="autoExpandParent"
        :expandedKeys="expandedKeys"
        :checkedKeys="checkedKeys">

      </a-tree>
    </a-spin>
    <!--底部父子关联操作和确认取消按钮-->
    <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>
肖超群 authored
40
41
            树操作
            <a-icon type="up"/>
肖超群 authored
42
43
          </a-button>
        </a-dropdown>
谭毅彬 authored
44
45
        <a-button @click="handleCancel" type="primary" style="margin-right: 0.8rem">{{$t('button.close')}}</a-button>
        <a-button @click="handleSubmit" type="primary">{{$t('button.ok')}}</a-button>
肖超群 authored
46
47
48
49
50
51
      </div>
    </template>
  </j-modal>
</template>

<script>
肖超群 authored
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
import {queryDepartTreeList} from '@/api/api'

export default {
  name: 'JSelectDepartModal',
  props: ['modalWidth', 'multi', 'rootOpened', 'departId', 'store', 'text', 'treeOpera'],
  data() {
    return {
      visible: false,
      confirmLoading: false,
      treeData: [],
      autoExpandParent: true,
      expandedKeys: [],
      dataList: [],
      checkedKeys: [],
      checkedRows: [],
      searchValue: "",
      checkStrictly: true,
      fullscreen: false
    }
  },
  created() {
    this.loadDepart();
  },
  watch: {
    departId() {
      this.initDepartComponent()
    },
    visible: {
      handler() {
        this.initDepartComponent(true)
      }
    }
  },
  computed: {
    treeScreenClass() {
肖超群 authored
87
      return {
肖超群 authored
88
89
        'my-dept-select-tree': true,
        'fullscreen': this.fullscreen,
肖超群 authored
90
91
      }
    },
肖超群 authored
92
93
94
    filterTreeData() {
      if (!this.searchValue) {
        return this.treeData
肖超群 authored
95
      }
肖超群 authored
96
97
98
99
      let filter = []
      this.dataList.forEach((item) => {
        if (item.title.includes(this.searchValue)) {
          filter.push(Object.assign({}, item, {children: null, isLeaf: true}))
肖超群 authored
100
        }
肖超群 authored
101
102
      })
      return filter
肖超群 authored
103
    },
肖超群 authored
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  },
  methods: {
    show() {
      this.visible = true
      this.checkedRows = []
      this.checkedKeys = []
    },
    loadDepart() {
      // 这个方法是找到所有的部门信息
      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)
肖超群 authored
121
          }
肖超群 authored
122
123
124
125
126
127
128
129
130
131
132
133
        }
      })
    },
    initDepartComponent(flag) {
      let arr = []
      //该方法两个地方用 1.visible改变事件重新设置选中项 2.组件编辑页面回显
      let fieldName = flag == true ? 'key' : this.text
      if (this.departId) {
        let arr2 = this.departId.split(',')
        for (let item of this.dataList) {
          if (arr2.indexOf(item[this.store]) >= 0) {
            arr.push(item[fieldName])
肖超群 authored
134
135
          }
        }
肖超群 authored
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
      }
      if (flag == true) {
        this.checkedKeys = [...arr]
      } else {
        this.$emit("initComp", arr.join(','))
      }
    },
    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'}
肖超群 authored
153
        }
肖超群 authored
154
155
156
157
158
159
160
161
      }
    },
    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)
肖超群 authored
162
163
          }
        }
肖超群 authored
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
        this.expandedKeys = [...keys]
        //全部keys
        //this.allTreeKeys = [...keys]
      } else {
        this.expandedKeys = []
        //this.allTreeKeys = []
      }
    },
    onCheck(checkedKeys, info) {
      if (!this.multi) {
        let arr = checkedKeys.checked.filter(item => this.checkedKeys.indexOf(item) < 0)
        this.checkedKeys = [...arr]
        this.checkedRows = (this.checkedKeys.length === 0) ? [] : [info.node.dataRef]
      } else {
        if (this.checkStrictly) {
          this.checkedKeys = checkedKeys.checked
        } else {
          this.checkedKeys = checkedKeys
肖超群 authored
182
        }
肖超群 authored
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
        this.checkedRows = this.getCheckedRows(this.checkedKeys)
      }
    },
    onSelect(selectedKeys, info) {
      //取消关联的情况下才走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]
        } else {
          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)
肖超群 authored
200
201
          }
        }
肖超群 authored
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
        this.checkedRows = this.getCheckedRows(this.checkedKeys)
      }
    },
    onExpand(expandedKeys) {
      this.expandedKeys = expandedKeys
      this.autoExpandParent = false
    },
    handleSubmit() {
      if (!this.checkedKeys || this.checkedKeys.length == 0) {
        this.$emit("ok", '')
      } else {
        let checkRow = this.getCheckedRows(this.checkedKeys)
        let keyStr = this.checkedKeys.join(",")
        this.$emit("ok", checkRow, keyStr)
      }
      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)
肖超群 authored
235
236
          }
        }
肖超群 authored
237
238
239
240
241
242
243
244
245
      }
      return parentKey
    },
    // 根据 checkedKeys 获取 rows
    getCheckedRows(checkedKeys) {
      const forChildren = (list, key) => {
        for (let item of list) {
          if (item.id === key) {
            return item
肖超群 authored
246
          }
肖超群 authored
247
248
249
250
          if (item.children instanceof Array) {
            let value = forChildren(item.children, key)
            if (value != null) {
              return value
肖超群 authored
251
252
253
            }
          }
        }
肖超群 authored
254
255
        return null
      }
肖超群 authored
256
肖超群 authored
257
258
259
260
261
      let rows = []
      for (let key of checkedKeys) {
        let row = forChildren(this.treeData, key)
        if (row != null) {
          rows.push(row)
肖超群 authored
262
263
        }
      }
肖超群 authored
264
265
266
267
268
269
270
271
272
273
274
      return rows
    },
    switchCheckStrictly(v) {
      if (v == 1) {
        this.checkStrictly = false
      } else if (v == 2) {
        this.checkStrictly = true
      }
    },
    isFullscreen(val) {
      this.fullscreen = val
肖超群 authored
275
276
    }
  }
肖超群 authored
277
}
肖超群 authored
278
279
280
281

</script>

<style lang="less" scoped>
肖超群 authored
282
283
284
285
286
287
// 限制部门选择树高度,避免部门太多时点击确定不便
.my-dept-select-tree {
  height: 350px;

  &.fullscreen {
    height: calc(100vh - 250px);
肖超群 authored
288
  }
肖超群 authored
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303

  overflow-y: scroll;
}

.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;
}
肖超群 authored
304
</style>