Add lazy loading demo for Tree

pull/41/head
cagataycivici 2019-08-02 11:02:44 +03:00
parent 072ef2672f
commit 8275c46b03
2 changed files with 56 additions and 71 deletions

View File

@ -1,5 +1,11 @@
<template> <template>
<div :class="containerClass"> <div :class="containerClass">
<template v-if="loading">
<div class="p-tree-loading-mask p-component-overlay"></div>
<div class="p-tree-loading-content">
<i :class="loadingIconClass" />
</div>
</template>
<ul class="p-tree-container" role="tree"> <ul class="p-tree-container" role="tree">
<TreeNode v-for="node of value" :key="node.key" :node="node" :templates="$scopedSlots" <TreeNode v-for="node of value" :key="node.key" :node="node" :templates="$scopedSlots"
:expandedKeys="d_expandedKeys" @node-toggle="onNodeToggle" @node-click="onNodeClick" :expandedKeys="d_expandedKeys" @node-toggle="onNodeToggle" @node-click="onNodeClick"
@ -149,49 +155,6 @@ export default {
return _selectionKeys; return _selectionKeys;
}, },
/*handleCheckboxSelection(event) {
const node = event.node;
const checked = this.isChecked(node);
let _selectionKeys = this.selectionKeys ? {...this.selectionKeys} : {};
if (checked) {
if (this.propagateSelectionDown)
this.propagateDown(node, false, _selectionKeys);
else
delete _selectionKeys[node.key];
if (this.propagateSelectionUp && this.props.onPropagateUp) {
this.props.onPropagateUp({
originalEvent: event,
check: false,
selectionKeys: selectionKeys
});
}
this.$emit('node-unselect', node);
}
else {
if (this.props.propagateSelectionDown)
this.propagateDown(this.props.node, true, selectionKeys);
else
selectionKeys[this.props.node.key] = {checked: true};
if (this.props.propagateSelectionUp && this.props.onPropagateUp) {
this.props.onPropagateUp({
originalEvent: event,
check: true,
selectionKeys: selectionKeys
});
}
this.$emit('node-select', node);
}
return _selectionKeys;
},*/
isCheckboxSelectionMode() {
return this.selectionMode === 'checkbox';
},
isSingleSelectionMode() { isSingleSelectionMode() {
return this.selectionMode === 'single'; return this.selectionMode === 'single';
}, },
@ -211,6 +174,9 @@ export default {
'p-tree-selectable': this.selectionMode != null, 'p-tree-selectable': this.selectionMode != null,
'p-tree-loading': this.loading 'p-tree-loading': this.loading
}]; }];
},
loadingIconClass() {
return ['p-tree-loading-icon pi-spin', this.loadingIcon];
} }
}, },
components: { components: {

View File

@ -5,20 +5,12 @@
<div class="content-section introduction"> <div class="content-section introduction">
<div class="feature-intro"> <div class="feature-intro">
<h1>Tree - Lazy</h1> <h1>Tree - Lazy</h1>
<p>Tree is used to display hierarchical data.</p> <p>Lazy loading is handy when dealing with huge datasets. This example imitates a lazy loading case with timeouts.</p>
</div> </div>
</div> </div>
<div class="content-section implementation"> <div class="content-section implementation">
<h3>Basic</h3> <Tree :value="nodes" @node-expand="onNodeExpand" :loading="loading"></Tree>
<Tree :value="nodes"></Tree>
<h3>Programmatic Control</h3>
<div style="margin-bottom: 1em">
<Button type="button" icon="pi pi-plus" label="Expand All" @click="expandAll" />
<Button type="button" icon="pi pi-minus" label="Collapse All" @click="collapseAll" />
</div>
<Tree :value="nodes" :expandedKeys="expandedKeys"></Tree>
</div> </div>
<TreeDoc /> <TreeDoc />
@ -32,8 +24,8 @@ import TreeSubMenu from './TreeSubMenu';
export default { export default {
data() { data() {
return { return {
nodes: null, loading: false,
expandedKeys: {} nodes: null
} }
}, },
nodeService: null, nodeService: null,
@ -41,26 +33,53 @@ export default {
this.nodeService = new NodeService(); this.nodeService = new NodeService();
}, },
mounted() { mounted() {
this.nodeService.getTreeNodes().then(data => this.nodes = data); this.loading = true;
setTimeout(() => {
this.nodes = this.initateNodes();
this.loading = false;
}, 2000);
}, },
methods: { methods: {
expandAll() { onNodeExpand(node) {
for (let node of this.nodes) { if (!node.children) {
this.expandNode(node); this.loading = true;
setTimeout(() => {
let _node = {...node};
_node.children = [];
for (let i = 0; i < 3; i++) {
_node.children.push({
key: node.key + '-' + i,
label: 'Lazy ' + node.label + '-' + i
});
} }
this.expandedKeys = {...this.expandedKeys}; let _nodes = {...this.nodes}
}, _nodes[parseInt(node.key, 10)] = _node;
collapseAll() {
this.expandedKeys = {}; this.nodes = _nodes;
}, this.loading = false;
expandNode(node) { }, 500);
this.expandedKeys[node.key] = true;
if (node.children && node.children.length) {
for (let child of node.children) {
this.expandNode(child);
}
} }
},
initateNodes() {
return [{
key: '0',
label: 'Node 0',
leaf: false
},
{
key: '1',
label: 'Node 1',
leaf: false
},
{
key: '2',
label: 'Node 2',
leaf: false
}];
} }
}, },
components: { components: {