vue-递归树形菜单

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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<!DOCTYPE html>
<html lang="zh-cn">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style>
ul {
padding: 0;
margin: 0;
list-style: none;
}

.tree-menu {
width: 360px;
height: 100%;
padding: 0px 12px;
border-right: 1px solid #e6e9f0;
}

.tree-menu-comm span {
display: block;
font-size: 12px;
position: relative;
}

.tree-contro .ico {
background-position: 3px -92px;
}

.tree-title .ico {
position: absolute;
left: -13px;
top: 0;
width: 15px;
height: 26px;
background: url(./folder-tree.png) no-repeat 4px -43px;
opacity: 0.8;
}

.tree-menu-comm span strong {
display: block;
width: 82%;
position: relative;
line-height: 22px;
padding: 2px 0;
padding-left: 5px;
color: #161719;
font-weight: normal;
}

.tree-nav {
background: #e7f2fe;
border: 1px solid #bfdaf4;
padding-left: 14px;
margin-left: 0px;
}

.tree-title {
border: 1px solid #fff;
margin-top: 1px;
}
/*无箭头*/

.tree-contro-none .ico {
background-position: -999px -99px;
}
/*箭头朝下*/

.tree-contro .ico {
background-position: 3px -92px;
}
</style>
<script src="../vue.js"></script>
<script>
</script>
</head>

<body>
<div id="app">
<!-- <div class="tree-menu-comm tree-menu">
<ul>
<li>
<div class="tree-title" style="padding-left: 16px;"><span><strong>目录</strong> <i class="ico"></i></span></div>
<ul>
<li>
<div class="tree-title" style="padding-left: 32px;"><span><strong>我的音乐</strong> <i class="ico"></i></span></div>
<ul>
<li>
<div class="tree-title" style="padding-left: 48px;"><span><strong>周杰伦</strong> <i class="ico"></i></span></div>
<ul>
<li>
<div class="tree-title tree-contro-none" style="padding-left: 64px;"><span><strong>发如雪</strong> <i class="ico"></i></span></div>

</li>
</ul>
</li>
<li>
<div class="tree-title" style="padding-left: 48px;"><span><strong>王杰</strong> <i class="ico"></i></span></div>
<ul>
<li>
<div class="tree-title tree-contro-none" style="padding-left: 64px;"><span><strong>一场游戏一场梦</strong> <i class="ico"></i></span></div>

</li>
</ul>
</li>
</ul>
</li>
<li>
<div class="tree-title tree-contro-none" style="padding-left: 32px;"><span><strong>我的照片</strong> <i class="ico"></i></span></div>

</li>
</ul>
</li>
</ul>
</div> -->

<m-tree :data="treeList"></m-tree>

</div>
<script>
/*
传入的数据结构:
[
{
title:XXX,
children:[
{
title:XXXX,
chidren:[]
}
]
}]

设置的props:
data 数据结构 默认为 []

定制模板:
不可定制

监控状态变化:
事件名on-select-change 点击树节点触发
*/


Vue.component('m-tree-list',{
computed:{
count(){
var c = this.increment;
return ++c;
},
stylePadding(){
return {
'padding-left':this.count * 16 + 'px'
}
}
},
props:{
data:{
type:Array,
default:[]
},
increment:{
type:Number,
default:0
}
},
template:`
<ul>
<li v-for="item of data">
<div class="tree-title" :style="[stylePadding]">
<span><strong>{{item.title}}</strong> <i class="ico"></i></span>
</div>
<!--如果循环的item有children属性,那么生成下一级-->
<m-tree-list
:increment="count"
v-if='item.chidren'
:data="item.chidren"
></m-tree-list>
</li>
</ul>
`
})

Vue.component('m-tree',{
props:{
data:{
type:Array,
default:[]
}
},
template:`
<div class="tree-menu-comm tree-menu">
<m-tree-list :data="data"></m-tree-list>
</div>
`
})


var data = [{
title: "目录",
chidren: [{
title: "我的音乐",
chidren: [{
title: "周杰伦",
chidren: [{
title: "发如雪"
}]
}, {
title: "王杰",
chidren: [{
title: "一场游戏一场梦"
}]
}]
}, {
title: "我的照片"
}]
}];

new Vue({
el:"#app",
data:{
treeList:data
}
})

</script>
</body>

</html>