liuxiaolong
2019-05-06 770c069996e1d2979c1170c1639c18b02d07e72f
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<template>
  <div id="tree-container" class="clearfix" style="padding: 8px">
    <div class="flex-row-left mb-2">
      <b-input
        autocomplete="off"
        v-model="searchValue"
        placeholder="输入关键字"
        @change.native="handleSearch"
        class="mr10"
      />
      <b-button variant="primary" class="pl30 pr30" size="md" @click="handleSearch">搜索</b-button>
    </div>
 
    <!-- style="background: rgb(243, 243, 243)" -->
    <div class="overflow-auto">
      <!-- <Tree
        :data="data"
        :props="option"
        :default-expand-all="isOpenAll"
        @node-click="handleNodeClick"
        highlight-current
        id="tree"
        ref="tree2"
        node-key="id"
        :filter-node-method="filterNode"
        class="float-left"
        style="width: 100%"
      />-->
      <Tree
        id="tree"
        ref="tree2"
        node-key="id"
        class="float-left"
        style="width: 100%"
        :data="data"
        :props="option"
        :expand-on-click-node="false"
        :show-checkbox="showCheckbox"
        :highlight-current="highlightCurrent"
        :default-expand-all="isOpenAll"
        @node-click="handleNodeClick"
        @check-change="handleCheckChange"
      />
    </div>
  </div>
</template>
 
<script>
import { Tree } from 'element-ui'
export default {
  data: () => ({
    searchValue: '',
    checkedList: []
  }),
  props: {
    data: {
      type: Array,
      default: () => [],
      required: true
    },
    option: {
      type: Object,
      default: () => ({
        children: 'child',
        label: 'name'
      })
    },
    isOpenAll: {
      default: true,
      type: Boolean
    },
    isDataBase: {
      default: false,
      type: Boolean
    },
    highlightCurrent: { default: true, type: Boolean },
    showCheckbox: { default: false, type: Boolean }
  },
  computed: {
    checkedIds() {
      return this.checkedList.map(item => item.id)
    }
  },
  methods: {
    handleNodeClick(node) {
      this.$emit('currentNode', node)
    },
    // * 设置选中状态
    setCurrentKey(id) {
      this.$nextTick(() => {
        this.$refs.tree2.setCurrentKey(id)
      })
    },
    // 清空选中状态
    removeCurrentNode() {
      this.$refs.tree2.setCurrentKey(null)
    },
    // * 过滤
    handleSearch() {
      this.$refs.tree2.filter(this.searchValue)
    },
    // * 过滤
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    // * 获取被选中节点
    getCurrentNode() {
      return this.$refs.tree2.getCurrentNode()
    },
    /* 勾选选框 */
    handleCheckChange(data, checked, indeterminate) {
      // 获得在当前数组中的index
      let dataIndex = null
      for (let [index, item] of this.checkedList.entries()) {
        if (data && item.id === data.id) {
          dataIndex = index
          break
        }
      }
      if (checked) {
        // 添加
        this.checkedList.push(data)
      } else if (!indeterminate && dataIndex !== null) {
        // 删除
        this.checkedList.splice(dataIndex, 1)
      }
 
      this.$emit('check-change', {
        list: this.checkedList,
        ids: this.checkedIds
      })
    }
  },
  components: {
    Tree
  }
}
</script>
 
<style lang="scss" socped>
#tree-container .el-tree {
  overflow: auto;
}
 
#tree-container .el-tree-node > .el-tree-node__children {
  overflow: visible;
}
.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
  // background: rgb(38, 180, 255);
  // color: #fff;
  color: #35bde7;
  position: relative;
  border-radius: 5px;
  overflow: hidden;
  .el-tree-node__expand-icon {
    color: #35bde7;
  }
  .el-tree-node__expand-icon.is-leaf {
    color: transparent;
    cursor: default;
  }
}
.el-tree-node__content:hover {
  border-radius: 5px;
  overflow: hidden;
}
.tree-btn {
  padding: 6px 8px;
}
</style>