2016-07-27 06:15:02 +00:00
|
|
|
<template>
|
2016-10-17 16:31:37 +00:00
|
|
|
<div class="el-tree" :class="{ 'el-tree--highlight-current': highlightCurrent }">
|
|
|
|
<el-tree-node
|
2016-11-23 02:46:05 +00:00
|
|
|
v-for="child in root.childNodes"
|
2016-10-17 16:31:37 +00:00
|
|
|
:node="child"
|
|
|
|
:props="props"
|
2016-11-23 02:46:05 +00:00
|
|
|
:key="getNodeKey(child)"
|
2017-01-14 08:02:52 +00:00
|
|
|
:render-content="renderContent"
|
2017-01-16 08:34:32 +00:00
|
|
|
@node-expand="handleNodeExpand">
|
2016-10-17 16:31:37 +00:00
|
|
|
</el-tree-node>
|
2016-11-23 02:46:05 +00:00
|
|
|
<div class="el-tree__empty-block" v-if="!root.childNodes || root.childNodes.length === 0">
|
2016-11-16 07:35:46 +00:00
|
|
|
<span class="el-tree__empty-text">{{ emptyText }}</span>
|
|
|
|
</div>
|
2016-07-27 06:15:02 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2017-03-17 03:35:27 +00:00
|
|
|
<script>
|
2016-11-23 02:46:05 +00:00
|
|
|
import TreeStore from './model/tree-store';
|
2017-10-11 10:00:58 +00:00
|
|
|
import ElTreeNode from './tree-node.vue';
|
2016-11-23 02:46:05 +00:00
|
|
|
import {t} from 'element-ui/src/locale';
|
2017-01-14 08:02:52 +00:00
|
|
|
import emitter from 'element-ui/src/mixins/emitter';
|
2016-07-27 06:15:02 +00:00
|
|
|
|
|
|
|
export default {
|
2016-12-31 15:33:51 +00:00
|
|
|
name: 'ElTree',
|
2016-07-27 06:15:02 +00:00
|
|
|
|
2017-01-14 08:02:52 +00:00
|
|
|
mixins: [emitter],
|
|
|
|
|
|
|
|
components: {
|
2017-10-11 10:00:58 +00:00
|
|
|
ElTreeNode
|
2017-01-14 08:02:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
store: null,
|
|
|
|
root: null,
|
|
|
|
currentNode: null
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
props: {
|
|
|
|
data: {
|
|
|
|
type: Array
|
|
|
|
},
|
2016-11-16 07:35:46 +00:00
|
|
|
emptyText: {
|
|
|
|
type: String,
|
2016-11-17 07:37:04 +00:00
|
|
|
default() {
|
|
|
|
return t('el.tree.emptyText');
|
|
|
|
}
|
2016-11-16 07:35:46 +00:00
|
|
|
},
|
|
|
|
nodeKey: String,
|
|
|
|
checkStrictly: Boolean,
|
|
|
|
defaultExpandAll: Boolean,
|
2016-12-19 07:38:51 +00:00
|
|
|
expandOnClickNode: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true
|
|
|
|
},
|
2017-07-28 08:54:07 +00:00
|
|
|
checkDescendants: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
2016-11-16 07:35:46 +00:00
|
|
|
autoExpandParent: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true
|
|
|
|
},
|
|
|
|
defaultCheckedKeys: Array,
|
|
|
|
defaultExpandedKeys: Array,
|
2016-10-17 16:31:37 +00:00
|
|
|
renderContent: Function,
|
2016-07-27 06:15:02 +00:00
|
|
|
showCheckbox: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
default() {
|
|
|
|
return {
|
|
|
|
children: 'children',
|
|
|
|
label: 'label',
|
2017-07-19 10:36:26 +00:00
|
|
|
icon: 'icon',
|
|
|
|
disabled: 'disabled'
|
2016-07-27 06:15:02 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
lazy: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
2016-10-17 16:31:37 +00:00
|
|
|
highlightCurrent: Boolean,
|
2016-11-21 16:08:22 +00:00
|
|
|
load: Function,
|
2017-01-14 08:02:52 +00:00
|
|
|
filterNodeMethod: Function,
|
2017-02-08 03:49:54 +00:00
|
|
|
accordion: Boolean,
|
|
|
|
indent: {
|
|
|
|
type: Number,
|
2017-09-14 03:34:26 +00:00
|
|
|
default: 18
|
2017-02-08 03:49:54 +00:00
|
|
|
}
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
computed: {
|
|
|
|
children: {
|
|
|
|
set(value) {
|
|
|
|
this.data = value;
|
|
|
|
},
|
|
|
|
get() {
|
|
|
|
return this.data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-10-08 05:58:31 +00:00
|
|
|
watch: {
|
2016-11-16 07:35:46 +00:00
|
|
|
defaultCheckedKeys(newVal) {
|
2016-11-25 08:30:10 +00:00
|
|
|
this.store.defaultCheckedKeys = newVal;
|
2016-11-23 02:46:05 +00:00
|
|
|
this.store.setDefaultCheckedKey(newVal);
|
2016-11-25 08:30:10 +00:00
|
|
|
},
|
|
|
|
defaultExpandedKeys(newVal) {
|
|
|
|
this.store.defaultExpandedKeys = newVal;
|
|
|
|
this.store.setDefaultExpandedKeys(newVal);
|
|
|
|
},
|
|
|
|
data(newVal) {
|
|
|
|
this.store.setData(newVal);
|
2016-10-08 05:58:31 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
methods: {
|
2016-11-21 16:08:22 +00:00
|
|
|
filter(value) {
|
|
|
|
if (!this.filterNodeMethod) throw new Error('[Tree] filterNodeMethod is required when filter');
|
2016-11-23 02:46:05 +00:00
|
|
|
this.store.filter(value);
|
|
|
|
},
|
|
|
|
getNodeKey(node, index) {
|
|
|
|
const nodeKey = this.nodeKey;
|
|
|
|
if (nodeKey && node) {
|
|
|
|
return node.data[nodeKey];
|
|
|
|
}
|
|
|
|
return index;
|
2016-11-21 16:08:22 +00:00
|
|
|
},
|
2016-07-27 06:15:02 +00:00
|
|
|
getCheckedNodes(leafOnly) {
|
2016-11-23 02:46:05 +00:00
|
|
|
return this.store.getCheckedNodes(leafOnly);
|
2016-11-16 07:35:46 +00:00
|
|
|
},
|
2016-11-21 13:53:55 +00:00
|
|
|
getCheckedKeys(leafOnly) {
|
2016-11-23 02:46:05 +00:00
|
|
|
return this.store.getCheckedKeys(leafOnly);
|
2016-11-21 13:53:55 +00:00
|
|
|
},
|
2017-07-23 09:28:26 +00:00
|
|
|
getCurrentNode() {
|
|
|
|
const currentNode = this.store.getCurrentNode();
|
|
|
|
return currentNode ? currentNode.data : null;
|
|
|
|
},
|
|
|
|
getCurrentKey() {
|
|
|
|
if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in getCurrentKey');
|
|
|
|
const currentNode = this.getCurrentNode();
|
|
|
|
return currentNode ? currentNode[this.nodeKey] : null;
|
|
|
|
},
|
2016-11-21 13:53:55 +00:00
|
|
|
setCheckedNodes(nodes, leafOnly) {
|
|
|
|
if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCheckedNodes');
|
2016-11-23 02:46:05 +00:00
|
|
|
this.store.setCheckedNodes(nodes, leafOnly);
|
2016-11-21 13:53:55 +00:00
|
|
|
},
|
|
|
|
setCheckedKeys(keys, leafOnly) {
|
2017-07-23 09:28:26 +00:00
|
|
|
if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCheckedKeys');
|
2016-11-23 02:46:05 +00:00
|
|
|
this.store.setCheckedKeys(keys, leafOnly);
|
2016-11-28 10:57:09 +00:00
|
|
|
},
|
|
|
|
setChecked(data, checked, deep) {
|
|
|
|
this.store.setChecked(data, checked, deep);
|
2017-01-14 08:02:52 +00:00
|
|
|
},
|
2017-07-23 09:28:26 +00:00
|
|
|
setCurrentNode(node) {
|
|
|
|
if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCurrentNode');
|
|
|
|
this.store.setUserCurrentNode(node);
|
|
|
|
},
|
|
|
|
setCurrentKey(key) {
|
|
|
|
if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCurrentKey');
|
|
|
|
this.store.setCurrentNodeKey(key);
|
|
|
|
},
|
2017-01-16 08:34:32 +00:00
|
|
|
handleNodeExpand(nodeData, node, instance) {
|
|
|
|
this.broadcast('ElTreeNode', 'tree-node-expand', node);
|
2017-01-24 02:37:44 +00:00
|
|
|
this.$emit('node-expand', nodeData, node, instance);
|
2017-07-21 08:25:28 +00:00
|
|
|
},
|
|
|
|
updateKeyChildren(key, data) {
|
|
|
|
if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in updateKeyChild');
|
|
|
|
this.store.updateChildren(key, data);
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
2017-01-14 08:02:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
created() {
|
|
|
|
this.isTree = true;
|
|
|
|
|
|
|
|
this.store = new TreeStore({
|
|
|
|
key: this.nodeKey,
|
|
|
|
data: this.data,
|
|
|
|
lazy: this.lazy,
|
|
|
|
props: this.props,
|
|
|
|
load: this.load,
|
|
|
|
currentNodeKey: this.currentNodeKey,
|
|
|
|
checkStrictly: this.checkStrictly,
|
2017-07-28 08:54:07 +00:00
|
|
|
checkDescendants: this.checkDescendants,
|
2017-01-14 08:02:52 +00:00
|
|
|
defaultCheckedKeys: this.defaultCheckedKeys,
|
|
|
|
defaultExpandedKeys: this.defaultExpandedKeys,
|
|
|
|
autoExpandParent: this.autoExpandParent,
|
|
|
|
defaultExpandAll: this.defaultExpandAll,
|
|
|
|
filterNodeMethod: this.filterNodeMethod
|
|
|
|
});
|
|
|
|
|
|
|
|
this.root = this.store.root;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|