-
Notifications
You must be signed in to change notification settings - Fork 0
/
vueViewComponent.html
108 lines (108 loc) · 2.39 KB
/
vueViewComponent.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>简单的树形视图实现</title>
<style type="text/css">
body{font-family: Menlo,Consolas,monospace; color: #444;}
.hidden{display: none;}
.bold{ font-weight: bold;}
.item{cursor: pointer;}
ul{padding-left:1em; margin-left: 0.75em; line-height: 1.5rem; list-style-type: dot;}
</style>
<script src="src/vue.min.js"></script>
<script type="text/x-template" id="view-template">
<li>
<div :class="{'bold':isFolder}" @click="toggle" @dblclick="changeType">
{{alllist.name}}
<span v-if="isFolder">[ {{show?'-':'+'}} ]</span>
</div>
<ul v-show="show" v-if="isFolder">
<viewtemplate class="item" :alllist="childs" v-for="childs in alllist.children"></viewtemplate>
<!--注意绑定数据是childs,不在是alllist-->
<li class="add" @click="addChildren">+</li>
</ul>
</li>
</script>
</head>
<body>
<p class="hidden">展现了组件的递归使用</p>
<p>(You can double click on an item to turn it into a folder!)</p>
<div>
<ul id="main">
<viewtemplate class="item" :alllist="treeData"></viewtemplate>
</ul>
</div>
<script type="text/javascript">
var data={
name:'My Tree',
children:[
{name:'child1'},
{name:'child2'},
{
name:'childFolder1',
children:[
{
name:'childFolder2',
children:[
{name:'childFolderchilde21'},
{name:'childFolderchilde22'},
]
},
{name:'childFolderchilde11'},
{name:'childFolderchilde12'},
{
name:'childFolder3',
children:[
{name:'childFolderchilde31'},
{name:'childFolderchilde32'},
]
}
]
}
]
};
Vue.component('viewtemplate', {
template:'#view-template',
props:{
alllist:Object
},
data:function (){
return {
show:false
}
},
computed:{
isFolder:function (){
return this.alllist.children && this.alllist.children.length;
}
},
methods:{
toggle:function(){
if(this.isFolder){
this.show=!this.show
}
},
changeType:function(){
if(!this.isFolder){
Vue.set(this.alllist,'children',[]);
this.addChildren();
this.show=true;
}
},
addChildren:function(){
this.alllist.children.push({
name:'new stuff'
})
}
}
})
var vm=new Vue({
el:'#main',
data:{
treeData:data
}
})
</script>
</body>
</html>