-
Notifications
You must be signed in to change notification settings - Fork 476
/
Copy path5-8 Recursive Component1.html
158 lines (150 loc) · 3.71 KB
/
5-8 Recursive Component1.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="vue.js"></script>
<meta charset="UTF-8">
<title>5-8</title>
<style>
body {
font-family: Menlo, Consolas, monospace;
color: #444;
}
.item {
cursor: pointer;
}
.bold {
font-weight: bold;
}
ul {
padding-left: 1em;
line-height: 1.5em;
list-style-type: dot;
}
</style>
</head>
<body>
<!-- item template -->
<!--
<item>
<li>
<div>-,+</div>
<ul>
<item></item>
</ul>
</li>
<item>
-->
<script type="text/x-template" id="item-template">
<li>
<div
:class="{bold: isFolder}"
@click="toggle"
@dblclick="changeType">
{{model.name}}
<span v-if="isFolder">
[{{open ? '-' : '+'}}]
</span>
</div>
<ul v-show="open" v-if="isFolder">
<item
class="item"
v-for="model in model.children"
:model="model">
</item>
<li @click="addChild">
+
</li>
</ul>
</li>
</script>
<p>(你可以在项目上双点击,将它成为目录)</p>
<!-- the demo root element -->
<ul id="demo">
<item class="item" :model="treeData">
</item>
</ul>
<script>
// demo data
var data = {
name: '树',
children: [
{
name: '1-1.台湾 小凡'
},
{
name: '1-2.喜欢 Vue.js'
},
{
name: '1-3.目录',
children: [
{
name: '1-3-1.目录',
children: [
{
name: '1-3-1-1.台湾 小凡'
}, {
name: '1-3-1-2.喜欢 Vue.js'
}
]
}, {
name: '1-3-2.台湾 小凡'
}, {
name: '1-3-3.喜欢 Vue.js'
}, {
name: '1-3-4目录',
children: [
{
name: '1-3-4-1.台湾 小凡'
}, {
name: '1-3-4-2€.喜欢 Vue.js'
}]
}]
}]
}
// define the item component
Vue.component('item', {
template: '#item-template',
props: {
model: Object
},
data: function() {
return {
open: false
}
},
computed: {
isFolder: function() {
return this.model.children &&
this.model.children.length
}
},
methods: {
toggle: function() {
if (this.isFolder) {
this.open = !this.open
}
},
changeType: function() {
if (!this.isFolder) {
Vue.set(this.model, 'children', [])
this.addChild()
this.open = true
}
},
addChild: function() {
this.model.children.push({
name: '新项目'
})
}
}
})
// boot up the demo
var demo = new Vue({
el: '#demo',
data: {
treeData: data
}
})
</script>
</body>
</html>