element/packages/tree/src/tree.vue

159 lines
3.9 KiB
Vue
Raw Normal View History

2016-07-27 06:15:02 +00:00
<template>
<div class="el-tree" :class="{ 'el-tree--highlight-current': highlightCurrent }">
<el-tree-node
v-for="child in root.childNodes"
:node="child"
:props="props"
:key="getNodeKey(child)"
:render-content="renderContent">
</el-tree-node>
<div class="el-tree__empty-block" v-if="!root.childNodes || root.childNodes.length === 0">
<span class="el-tree__empty-text">{{ emptyText }}</span>
</div>
2016-07-27 06:15:02 +00:00
</div>
</template>
<script type="text/ecmascript-6">
import TreeStore from './model/tree-store';
import {t} from 'element-ui/src/locale';
2016-07-27 06:15:02 +00:00
export default {
name: 'el-tree',
props: {
data: {
type: Array
},
emptyText: {
type: String,
2016-11-17 07:37:04 +00:00
default() {
return t('el.tree.emptyText');
}
},
nodeKey: String,
checkStrictly: Boolean,
defaultExpandAll: Boolean,
autoExpandParent: {
type: Boolean,
default: true
},
defaultCheckedKeys: Array,
defaultExpandedKeys: Array,
renderContent: Function,
2016-07-27 06:15:02 +00:00
showCheckbox: {
type: Boolean,
default: false
},
props: {
default() {
return {
children: 'children',
label: 'label',
icon: 'icon'
};
}
},
lazy: {
type: Boolean,
default: false
},
highlightCurrent: Boolean,
currentNodeKey: [String, Number],
load: Function,
filterNodeMethod: Function
2016-07-27 06:15:02 +00:00
},
created() {
this.isTree = true;
2016-07-27 06:15:02 +00:00
this.store = new TreeStore({
key: this.nodeKey,
2016-07-27 06:15:02 +00:00
data: this.data,
lazy: this.lazy,
props: this.props,
load: this.load,
currentNodeKey: this.currentNodeKey,
checkStrictly: this.checkStrictly,
defaultCheckedKeys: this.defaultCheckedKeys,
defaultExpandedKeys: this.defaultExpandedKeys,
autoExpandParent: this.autoExpandParent,
defaultExpandAll: this.defaultExpandAll,
filterNodeMethod: this.filterNodeMethod
2016-07-27 06:15:02 +00:00
});
this.root = this.store.root;
2016-07-27 06:15:02 +00:00
},
data() {
return {
store: null,
root: null,
currentNode: null
2016-07-27 06:15:02 +00:00
};
},
components: {
ElTreeNode: require('./tree-node.vue')
},
computed: {
children: {
set(value) {
this.data = value;
},
get() {
return this.data;
}
}
},
2016-10-08 05:58:31 +00:00
watch: {
defaultCheckedKeys(newVal) {
this.store.defaultCheckedKeys = newVal;
this.store.setDefaultCheckedKey(newVal);
},
defaultExpandedKeys(newVal) {
this.store.defaultExpandedKeys = newVal;
this.store.setDefaultExpandedKeys(newVal);
},
currentNodeKey(newVal) {
this.store.setCurrentNodeKey(newVal);
},
data(newVal) {
this.store.setData(newVal);
2016-10-08 05:58:31 +00:00
}
},
2016-07-27 06:15:02 +00:00
methods: {
filter(value) {
if (!this.filterNodeMethod) throw new Error('[Tree] filterNodeMethod is required when filter');
this.store.filter(value);
},
getNodeKey(node, index) {
const nodeKey = this.nodeKey;
if (nodeKey && node) {
return node.data[nodeKey];
}
return index;
},
2016-07-27 06:15:02 +00:00
getCheckedNodes(leafOnly) {
return this.store.getCheckedNodes(leafOnly);
},
getCheckedKeys(leafOnly) {
return this.store.getCheckedKeys(leafOnly);
},
setCheckedNodes(nodes, leafOnly) {
if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCheckedNodes');
this.store.setCheckedNodes(nodes, leafOnly);
},
setCheckedKeys(keys, leafOnly) {
if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCheckedNodes');
this.store.setCheckedKeys(keys, leafOnly);
2016-11-28 10:57:09 +00:00
},
setChecked(data, checked, deep) {
this.store.setChecked(data, checked, deep);
2016-07-27 06:15:02 +00:00
}
}
};
</script>