tree.js 1.3 KB

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