xiangpei
2025-04-18 7feee0463330ee014b0ac1a6f8e31118bb699f12
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
<template>
  <el-dialog
      :title="title"
      :visible.sync="open"
      width="30%"
      :close-on-click-modal="false"
      :destroy-on-close="true"
      :append-to-body="true">
    <el-tree
        style="padding-bottom: 30px"
        ref="tree"
        :data="treeData"
        node-key="id"
        :expand-on-click-node="false"
        :default-expand-all="true"
        :highlight-current="true"
        :check-strictly="true"
        @check-change="setCurrentCheck"
        :default-expanded-keys="[1, 1]"
        show-checkbox
        :props="defaultProps">
    </el-tree>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close" size="small">取 消</el-button>
      <el-button type="primary" @click="save" size="small">确 定</el-button>
    </div>
  </el-dialog>
</template>
 
<script>
export default {
  name: "TreeDialog",
  data() {
    return {
      open: false,
      selectOrg: null,
      defaultProps: {
        children: 'children',
        label: 'orgName'
      }
    }
  },
  props: {
    treeData: {
      type: Array
    },
    title: {
      type: String
    }
  },
  methods: {
    // 回显
    setChecked(id) {
      this.open = true
      this.$nextTick(() => {
        if (id) {
          this.$refs.tree.setCheckedKeys([id])
        }
      })
    },
    // 单选控制
    setCurrentCheck(data, checked) {
      if (checked) {
        this.$refs.tree.setCheckedKeys([data.id])
        this.selectOrg = data
      } else {
        this.selectOrg = null
      }
    },
    save(){
      this.$emit("save", this.selectOrg)
    },
    close() {
      this.open = false
    }
  }
}
</script>
 
<style scoped>
 
</style>