tree.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. // define the item component
  31. Vue.component('item', {
  32. template: '#item-template',
  33. data: function () {
  34. return {
  35. open: false
  36. }
  37. },
  38. computed: {
  39. isFolder: function () {
  40. return this.model.children &&
  41. this.model.children.length
  42. }
  43. },
  44. methods: {
  45. toggle: function () {
  46. if (this.isFolder) {
  47. this.open = !this.open
  48. }
  49. },
  50. changeType: function () {
  51. if (!this.isFolder) {
  52. this.model.$add('children', [])
  53. this.addChild()
  54. this.open = true
  55. }
  56. },
  57. addChild: function () {
  58. this.model.children.push({
  59. name: 'new stuff'
  60. })
  61. }
  62. }
  63. })
  64. // boot up the demo
  65. var demo = new Vue({
  66. el: '#demo',
  67. data: {
  68. treeData: data
  69. }
  70. })