tree.js 1.3 KB

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