tree.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. 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. methods: {
  46. toggle: function () {
  47. if (this.isFolder) {
  48. this.open = !this.open
  49. }
  50. },
  51. changeType: function () {
  52. if (!this.isFolder) {
  53. this.model.$add('children', [])
  54. this.addChild()
  55. this.open = true
  56. }
  57. },
  58. addChild: function () {
  59. this.model.children.push({
  60. name: 'new stuff'
  61. })
  62. }
  63. }
  64. })
  65. // boot up the demo
  66. var demo = new Vue({
  67. el: '#demo',
  68. data: {
  69. treeData: data
  70. }
  71. })