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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
| <template>
| <content-box title="站点列表"
| toggle
| @toggleChange="toggleChange"
| style="width:320px">
| <my-el-tree
| :data="data"
| :default-expanded-keys="expandedKeys"
| :current-node-key="getCurrentKey"
| @node-click="nodeClick"
| @leaf-click="leafClick"></my-el-tree>
| </content-box>
| </template>
|
| <script>
| import ContentBox from '../../components/ContentBox'
| import MyElTree from '../../components/MyElTree'
|
| import {
| searchStation,
| searchBattInfo,
| } from '../../assets/js/api'
|
| import {
| getTreeDataByKey,
| } from '../../assets/js/tools'
|
| export default {
| components: {
| ContentBox,
| MyElTree,
| },
| props: {
| defaultExpandedKeys: {
| type: Array,
| default() {
| return []
| },
| },
| currentNodeKey: {
| type: [String, Number],
| default: ""
| }
| },
| data() {
| return {
| data: [],
| expanded: [],
| currentKey: '',
| }
| },
| methods: {
| toggleChange() {
| this.$emit('toggleChange');
| },
| // 查询机房的信息
| searchStation() {
| searchStation({
| StationName1:"",
| StationName2:"",
| StationName5:"",
| }).then((res)=>{
| let rs = JSON.parse(res.data.result);
| let data = [];
| if(rs.code == 1) {
| data = rs.data;
| }
| // 格式化数据
| this.formatData(data);
| });
| },
| formatData(data) {
| let result = [];
| // 遍历数据构造树状
| data.forEach(item=>{
| // 省
| let provice = this.addData(result, item.StationName1);
| // 市
| let city = this.addData(provice.children, item.StationName2, provice);
| // 区县
| let county = this.addData(city.children, item.StationName5, city);
| // 机房
| let home = this.addData(county.children, item.StationName3, county, item);
| // 添加空白位置占位
| home.children.push({
| id: home.id+'temp',
| label: '数据加载中...'
| });
| });
| // 设置树状列表
| this.data = result;
| // 设置默认展开的节点
| if(data.length>0) {
| let item = data[0];
| this.expanded = [
| item.StationName1,
| item.StationName2,
| item.StationName5,
| item.StationName3,
| ];
| }
| let expandedNode = getTreeDataByKey(this.expandedKeys[0], result);
| if(expandedNode != -1) {
| this.nodeClick(expandedNode);
| }
| },
| addData(list, val, parent, data) {
| let item;
| let index = this.checkValIsIn(val, list);
| let parentId = parent?parent.id+'-':"";
| if(index == -1) {
| item = {
| id: parentId+val,
| label: val,
| children: [],
| data: data,
| };
| list.push(item);
| }else {
| item = list[index];
| }
| return item;
| },
| checkValIsIn(val, arr) {
| for(let i=0; i<arr.length; i++) {
| if(arr[i].label == val) {
| return i;
| }
| }
| return -1;
| },
| nodeClick(data, node) {
| if(data.children && data.children[0] && data.children[0].label == "数据加载中...") {
| this.searchBattInfo(data, node)
| }
| },
| searchBattInfo(data, node) {
| // 加载等待
| if(node) {
| node.loading=true;
| }
|
| searchBattInfo(data.data).then((res)=>{
| // 关闭等待
| if(node) {
| node.loading=false;
| }
|
| let rs = JSON.parse(res.data.result);
| let result=[{
| id: Math.random(),
| label: '暂无电池组',
| }];
| // 查询到结果
| if(rs.code == 1) {
| result = rs.data.map(item=>{
| item.id = item.BattGroupId;
| item.label = item.BattGroupName;
| item.leaf = true;
| return item;
| });
| }
| data.children = result;
| // 根据node的值判断不是通过点击触发的
| if(!node) {
| let acIndex = 0;
| for(let i=1; i<result.length; i++) {
| if(result[i].BattGroupId == this.currentNodeKey) {
| acIndex = i;
| break;
| }
| }
| this.currentKey = result[acIndex].id;
| this.$emit('leaf-click', result[acIndex]);
| }
| });
| },
| leafClick(data) {
| if(data.leaf) {
| this.$emit('leaf-click', data);
| }
|
| },
| },
| computed: {
| expandedKeys() {
| console.log(this.defaultExpandedKeys);
| let parentKey = this.defaultExpandedKeys.join("-");
| console.log(parentKey);
| if(parentKey.length>0 && parentKey != '---') {
| return [parentKey];
| }else {
| let expanded = this.expanded.join("-");
| return [expanded];
| }
| },
| getCurrentKey() {
| return this.currentNodeKey?this.currentNodeKey:this.currentKey;
| }
| },
| mounted() {
| // 查询电池组
| this.searchStation();
| }
| }
| </script>
|
| <style scoped>
|
| </style>
|
|