tree.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. @node-expand="handleNodeExpand">
  10. </el-tree-node>
  11. <div class="el-tree__empty-block" v-if="!root.childNodes || root.childNodes.length === 0">
  12. <span class="el-tree__empty-text">{{ emptyText }}</span>
  13. </div>
  14. </div>
  15. </template>
  16. <script>
  17. import TreeStore from './model/tree-store';
  18. import ElTreeNode from './tree-node.vue';
  19. import {t} from 'element-ui/src/locale';
  20. import emitter from 'element-ui/src/mixins/emitter';
  21. export default {
  22. name: 'ElTree',
  23. mixins: [emitter],
  24. components: {
  25. ElTreeNode
  26. },
  27. data() {
  28. return {
  29. store: null,
  30. root: null,
  31. currentNode: null
  32. };
  33. },
  34. props: {
  35. data: {
  36. type: Array
  37. },
  38. emptyText: {
  39. type: String,
  40. default() {
  41. return t('el.tree.emptyText');
  42. }
  43. },
  44. nodeKey: String,
  45. checkStrictly: Boolean,
  46. defaultExpandAll: Boolean,
  47. expandOnClickNode: {
  48. type: Boolean,
  49. default: true
  50. },
  51. checkDescendants: {
  52. type: Boolean,
  53. default: false
  54. },
  55. autoExpandParent: {
  56. type: Boolean,
  57. default: true
  58. },
  59. defaultCheckedKeys: Array,
  60. defaultExpandedKeys: Array,
  61. renderContent: Function,
  62. showCheckbox: {
  63. type: Boolean,
  64. default: false
  65. },
  66. props: {
  67. default() {
  68. return {
  69. children: 'children',
  70. label: 'label',
  71. icon: 'icon',
  72. disabled: 'disabled'
  73. };
  74. }
  75. },
  76. lazy: {
  77. type: Boolean,
  78. default: false
  79. },
  80. highlightCurrent: Boolean,
  81. load: Function,
  82. filterNodeMethod: Function,
  83. accordion: Boolean,
  84. indent: {
  85. type: Number,
  86. default: 18
  87. }
  88. },
  89. computed: {
  90. children: {
  91. set(value) {
  92. this.data = value;
  93. },
  94. get() {
  95. return this.data;
  96. }
  97. }
  98. },
  99. watch: {
  100. defaultCheckedKeys(newVal) {
  101. this.store.defaultCheckedKeys = newVal;
  102. this.store.setDefaultCheckedKey(newVal);
  103. },
  104. defaultExpandedKeys(newVal) {
  105. this.store.defaultExpandedKeys = newVal;
  106. this.store.setDefaultExpandedKeys(newVal);
  107. },
  108. data(newVal) {
  109. this.store.setData(newVal);
  110. }
  111. },
  112. methods: {
  113. filter(value) {
  114. if (!this.filterNodeMethod) throw new Error('[Tree] filterNodeMethod is required when filter');
  115. this.store.filter(value);
  116. },
  117. getNodeKey(node, index) {
  118. const nodeKey = this.nodeKey;
  119. if (nodeKey && node) {
  120. return node.data[nodeKey];
  121. }
  122. return index;
  123. },
  124. getCheckedNodes(leafOnly) {
  125. return this.store.getCheckedNodes(leafOnly);
  126. },
  127. getCheckedKeys(leafOnly) {
  128. return this.store.getCheckedKeys(leafOnly);
  129. },
  130. getCurrentNode() {
  131. const currentNode = this.store.getCurrentNode();
  132. return currentNode ? currentNode.data : null;
  133. },
  134. getCurrentKey() {
  135. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in getCurrentKey');
  136. const currentNode = this.getCurrentNode();
  137. return currentNode ? currentNode[this.nodeKey] : null;
  138. },
  139. setCheckedNodes(nodes, leafOnly) {
  140. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCheckedNodes');
  141. this.store.setCheckedNodes(nodes, leafOnly);
  142. },
  143. setCheckedKeys(keys, leafOnly) {
  144. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCheckedKeys');
  145. this.store.setCheckedKeys(keys, leafOnly);
  146. },
  147. setChecked(data, checked, deep) {
  148. this.store.setChecked(data, checked, deep);
  149. },
  150. setCurrentNode(node) {
  151. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCurrentNode');
  152. this.store.setUserCurrentNode(node);
  153. },
  154. setCurrentKey(key) {
  155. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCurrentKey');
  156. this.store.setCurrentNodeKey(key);
  157. },
  158. handleNodeExpand(nodeData, node, instance) {
  159. this.broadcast('ElTreeNode', 'tree-node-expand', node);
  160. this.$emit('node-expand', nodeData, node, instance);
  161. },
  162. updateKeyChildren(key, data) {
  163. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in updateKeyChild');
  164. this.store.updateChildren(key, data);
  165. }
  166. },
  167. created() {
  168. this.isTree = true;
  169. this.store = new TreeStore({
  170. key: this.nodeKey,
  171. data: this.data,
  172. lazy: this.lazy,
  173. props: this.props,
  174. load: this.load,
  175. currentNodeKey: this.currentNodeKey,
  176. checkStrictly: this.checkStrictly,
  177. checkDescendants: this.checkDescendants,
  178. defaultCheckedKeys: this.defaultCheckedKeys,
  179. defaultExpandedKeys: this.defaultExpandedKeys,
  180. autoExpandParent: this.autoExpandParent,
  181. defaultExpandAll: this.defaultExpandAll,
  182. filterNodeMethod: this.filterNodeMethod
  183. });
  184. this.root = this.store.root;
  185. }
  186. };
  187. </script>