刘磊
2025-06-09 dabbcafc629ef87d11ba55ef8cc1cdc776c047d8
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
<template>
  <div class="com-el-tree">
 
      <div class="m-title"><i class="el-icon-warning-outline"></i>角色列表</div>
 
    <el-scrollbar style="height: 100%; width: 200px" class="el-role-tree">
      <el-tree
        :data="tree"
        @node-click="nodeClick"
        node-key="id"
        :default-expanded-keys="openKeys"
        :expand-on-click-node="false"
        style="padding: 5px 0; margin-right: 2px"
      >
        <template #default="{ node, data }">
          <div class="action-group">
            <div
              class="action-text"
              :class="{
                actived: data.id == selectId,
                'node-text': data.parentId !== 0,
              }"
              :style="{ width: (4 - data.lv) * 18 + 150 + 'px' }"
            >
              <Icon
                v-if="data.parentId !== 0"
                :type="data.id == selectId ? 'ios-paper' : 'ios-paper-outline'"
              />
              {{ data.roleName }}
            </div>
          </div>
        </template>
      </el-tree>
    </el-scrollbar>
  </div>
</template>
 
<script>
export default {
  props: {
    // roles: {
    //   type: Object,
    //   default: () => {
    //     return [];
    //   }
    // },
    onChange: {
      type: Function,
      default: (treeId) => {},
    },
  },
  data() {
    return {
      selectId: -1,
      checked: false,
      openKeys: [],
      data: [],
      tree: [],
    };
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.http.post("/api/role/getUserChildRoles", {}, true).then((result) => {
        if (!result.status) return this.$message.error(result.message);
        this.data.splice(0);
        this.data = result.data;
        this.data.forEach((x) => {
          if (x.parentId == 0) {
            x.lv = 1;
            x.children = [];
            this.tree.push(x);
            this.getTree(x.id, x);
          }
        });
        this.openKeys.push(this.tree[0].id);
        this.selectId = this.openKeys[0];
        //默认加载第一个树形菜单下面的数据
 
        //this.onChange(this.selectId);
      });
    },
    getTree(id, data) {
      this.data.forEach((x) => {
        if (x.parentId == id) {
          x.lv = data.lv + 1;
          if (!data.children) data.children = [];
          data.children.push(x);
          this.getTree(x.id, x);
        }
      });
    },
    nodeClick(node, selected) {
      //  console.log(node);
      this.selectId = node.id;
      //缓存当前选中的节点
      //  this.$store.getters.data().treeDemo1.treeId = node.id;
      this.onChange(node);
    },
  },
};
</script>
<style lang="less" scoped>
.com-el-tree {
  //2020.06.03增加左侧tree固定宽度
  width: 200px;
  display: flex;
  flex-direction: column;
  height: 100%;
  border-radius: 3px;
  background: white;
  .el-role-tree {
    flex: 1;
    // border-right: 1px solid #eee;
  }
  .actived {
  }
  .action-text {
    font-size: 14px;
  }
}
.role-list {
  color: white;
  line-height: 40px;
  padding: 0 13px;
  font-size: 16px;
  top: 2px;
  width: 200px;
}
.m-title {
  line-height: 30px;
  font-size: 15px;
  background: #66b1ff0f;
  font-weight: bold;
  padding: 6px 16px;
  border-bottom: 1px solid #eee;
  i {
    padding-right: 5px;
  }
}
.com-el-tree ::v-dee(.el-tree-node) {
  padding: 3px 0;
}
.com-el-tree ::v-dee(.el-scrollbar .el-scrollbar__thumb) {
  width: 0 !important;
}
</style>
<style scoped>
 
</style>