tree.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. filters: {
  45. sortByChildren: function (list) {
  46. return list.slice().sort(function (a, b) {
  47. var alen = a.children ? 1 : 0
  48. var blen = b.children ? 1 : 0
  49. return alen > blen ? 1 : -1
  50. })
  51. }
  52. },
  53. methods: {
  54. toggle: function () {
  55. if (this.isFolder) {
  56. this.open = !this.open
  57. }
  58. },
  59. changeType: function () {
  60. if (!this.isFolder) {
  61. this.model.$add('children', [])
  62. this.addChild()
  63. this.open = true
  64. }
  65. },
  66. addChild: function () {
  67. this.model.children.push({
  68. name: 'new stuff'
  69. })
  70. }
  71. }
  72. })
  73. // boot up the demo
  74. var demo = new Vue({
  75. el: '#demo',
  76. data: {
  77. treeData: data
  78. }
  79. })