tree.js 1.3 KB

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