File tree Expand file tree Collapse file tree 2 files changed +132
-0
lines changed Expand file tree Collapse file tree 2 files changed +132
-0
lines changed Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="utf-8 ">
5
+ < title > Vue.js tree-view demo</ title >
6
+ < style >
7
+ body {
8
+ font-family : monospace;
9
+ }
10
+ .item {
11
+ cursor : pointer;
12
+ }
13
+ ul {
14
+ padding-left : 1em ;
15
+ list-style-type : dot;
16
+ }
17
+ </ style >
18
+ < script src ="../../dist/vue.js "> </ script >
19
+ </ head >
20
+ < body >
21
+
22
+ <!-- item template -->
23
+ < script type ="text/x-template " id ="item-template ">
24
+ < div v-style = "color: isFolder ? '#333' : '#999'"
25
+ v-on = "click: toggle, dblclick: changeType" >
26
+ { { model . name} }
27
+ < span v-if = "isFolder" > [{ { open ? '-' : '+' } } ]</ span >
28
+ </ div >
29
+ < ul v - show = "open" v - if = "isFolder" >
30
+ < li class = "item"
31
+ v-repeat = "model: model.children | sortByChildren"
32
+ v-component = "item" >
33
+ </ li >
34
+ < li v - on = "click: addChild" > + < / li >
35
+ </ ul >
36
+ </ script >
37
+
38
+ < p > (You can double click on an item to turn it into a folder.)</ p >
39
+
40
+ <!-- the demo root element -->
41
+ < ul id ="demo ">
42
+ < li class ="item "
43
+ v-component ="item "
44
+ v-with ="model: treeData ">
45
+ </ li >
46
+ </ ul >
47
+
48
+ <!-- demo code -->
49
+ < script src ="tree.js "> </ script >
50
+ </ body >
51
+ </ html >
Original file line number Diff line number Diff line change
1
+ // demo data
2
+ var data = {
3
+ name : 'My Tree' ,
4
+ children : [
5
+ { name : 'hello' } ,
6
+ { name : 'wat' } ,
7
+ {
8
+ name : 'child folder' ,
9
+ children : [
10
+ {
11
+ name : 'child folder' ,
12
+ children : [
13
+ { name : 'hello' } ,
14
+ { name : 'wat' }
15
+ ]
16
+ } ,
17
+ { name : 'hello' } ,
18
+ { name : 'wat' } ,
19
+ {
20
+ name : 'child folder' ,
21
+ children : [
22
+ { name : 'hello' } ,
23
+ { name : 'wat' }
24
+ ]
25
+ }
26
+ ]
27
+ }
28
+ ]
29
+ }
30
+
31
+ // define the item component
32
+ Vue . component ( 'item' , {
33
+ template : '#item-template' ,
34
+ data : function ( ) {
35
+ return {
36
+ open : false
37
+ }
38
+ } ,
39
+ computed : {
40
+ isFolder : function ( ) {
41
+ return this . model . children &&
42
+ this . model . children . length
43
+ }
44
+ } ,
45
+ filters : {
46
+ sortByChildren : function ( list ) {
47
+ return list . slice ( ) . sort ( function ( a , b ) {
48
+ var alen = a . children ? 1 : 0
49
+ var blen = b . children ? 1 : 0
50
+ return alen > blen ? 1 : - 1
51
+ } )
52
+ }
53
+ } ,
54
+ methods : {
55
+ toggle : function ( ) {
56
+ if ( this . isFolder ) {
57
+ this . open = ! this . open
58
+ }
59
+ } ,
60
+ changeType : function ( ) {
61
+ if ( ! this . isFolder ) {
62
+ this . model . $add ( 'children' , [ ] )
63
+ this . addChild ( )
64
+ this . open = true
65
+ }
66
+ } ,
67
+ addChild : function ( ) {
68
+ this . model . children . push ( {
69
+ name : 'new stuff'
70
+ } )
71
+ }
72
+ }
73
+ } )
74
+
75
+ // boot up the demo
76
+ var demo = new Vue ( {
77
+ el : '#demo' ,
78
+ data : {
79
+ treeData : data
80
+ }
81
+ } )
You can’t perform that action at this time.
0 commit comments