tree.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <div class="el-tree" :class="{ 'el-tree--highlight-current': highlightCurrent }">
  3. <el-tree-node
  4. v-for="child in root.childNodes"
  5. :node="child"
  6. :props="props"
  7. :key="getNodeKey(child)"
  8. :render-content="renderContent">
  9. </el-tree-node>
  10. <div class="el-tree__empty-block" v-if="!root.childNodes || root.childNodes.length === 0">
  11. <span class="el-tree__empty-text">{{ emptyText }}</span>
  12. </div>
  13. </div>
  14. </template>
  15. <script type="text/ecmascript-6">
  16. import TreeStore from './model/tree-store';
  17. import {t} from 'element-ui/src/locale';
  18. export default {
  19. name: 'ElTree',
  20. props: {
  21. data: {
  22. type: Array
  23. },
  24. emptyText: {
  25. type: String,
  26. default() {
  27. return t('el.tree.emptyText');
  28. }
  29. },
  30. nodeKey: String,
  31. checkStrictly: Boolean,
  32. defaultExpandAll: Boolean,
  33. expandOnClickNode: {
  34. type: Boolean,
  35. default: true
  36. },
  37. autoExpandParent: {
  38. type: Boolean,
  39. default: true
  40. },
  41. defaultCheckedKeys: Array,
  42. defaultExpandedKeys: Array,
  43. renderContent: Function,
  44. showCheckbox: {
  45. type: Boolean,
  46. default: false
  47. },
  48. props: {
  49. default() {
  50. return {
  51. children: 'children',
  52. label: 'label',
  53. icon: 'icon'
  54. };
  55. }
  56. },
  57. lazy: {
  58. type: Boolean,
  59. default: false
  60. },
  61. highlightCurrent: Boolean,
  62. currentNodeKey: [String, Number],
  63. load: Function,
  64. filterNodeMethod: Function
  65. },
  66. created() {
  67. this.isTree = true;
  68. this.store = new TreeStore({
  69. key: this.nodeKey,
  70. data: this.data,
  71. lazy: this.lazy,
  72. props: this.props,
  73. load: this.load,
  74. currentNodeKey: this.currentNodeKey,
  75. checkStrictly: this.checkStrictly,
  76. defaultCheckedKeys: this.defaultCheckedKeys,
  77. defaultExpandedKeys: this.defaultExpandedKeys,
  78. autoExpandParent: this.autoExpandParent,
  79. defaultExpandAll: this.defaultExpandAll,
  80. filterNodeMethod: this.filterNodeMethod
  81. });
  82. this.root = this.store.root;
  83. },
  84. data() {
  85. return {
  86. store: null,
  87. root: null,
  88. currentNode: null
  89. };
  90. },
  91. components: {
  92. ElTreeNode: require('./tree-node.vue')
  93. },
  94. computed: {
  95. children: {
  96. set(value) {
  97. this.data = value;
  98. },
  99. get() {
  100. return this.data;
  101. }
  102. }
  103. },
  104. watch: {
  105. defaultCheckedKeys(newVal) {
  106. this.store.defaultCheckedKeys = newVal;
  107. this.store.setDefaultCheckedKey(newVal);
  108. },
  109. defaultExpandedKeys(newVal) {
  110. this.store.defaultExpandedKeys = newVal;
  111. this.store.setDefaultExpandedKeys(newVal);
  112. },
  113. currentNodeKey(newVal) {
  114. this.store.setCurrentNodeKey(newVal);
  115. },
  116. data(newVal) {
  117. this.store.setData(newVal);
  118. }
  119. },
  120. methods: {
  121. filter(value) {
  122. if (!this.filterNodeMethod) throw new Error('[Tree] filterNodeMethod is required when filter');
  123. this.store.filter(value);
  124. },
  125. getNodeKey(node, index) {
  126. const nodeKey = this.nodeKey;
  127. if (nodeKey && node) {
  128. return node.data[nodeKey];
  129. }
  130. return index;
  131. },
  132. getCheckedNodes(leafOnly) {
  133. return this.store.getCheckedNodes(leafOnly);
  134. },
  135. getCheckedKeys(leafOnly) {
  136. return this.store.getCheckedKeys(leafOnly);
  137. },
  138. setCheckedNodes(nodes, leafOnly) {
  139. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCheckedNodes');
  140. this.store.setCheckedNodes(nodes, leafOnly);
  141. },
  142. setCheckedKeys(keys, leafOnly) {
  143. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCheckedNodes');
  144. this.store.setCheckedKeys(keys, leafOnly);
  145. },
  146. setChecked(data, checked, deep) {
  147. this.store.setChecked(data, checked, deep);
  148. }
  149. }
  150. };
  151. </script>