Skip to content

Commit daf27b2

Browse files
committed
add :key, publish version 0.5.0, update demo
1 parent af13a59 commit daf27b2

13 files changed

+91
-35
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ Thanks to [hukaibaihu](https://github.com/hukaibaihu/vue-org-tree) and his sourc
1010
<template>
1111
<h1>Basic</h1>
1212
<div>
13-
<blocks-tree :data="treeData" :horizontal="treeOrientation=='1'" :collapsable="true"></blocks-tree>
13+
<blocks-tree :data="treeData" :horizontal="treeOrientation=='1'" :collapsable="true"></blocks-tree>
1414
</div>
1515
1616
<h1>With slots</h1>
1717
<div>
18-
<blocks-tree :data="treeData" :horizontal="treeOrientation=='1'" :collapsable="true">
18+
<blocks-tree :data="treeData" :horizontal="treeOrientation=='1'" :collapsable="true" :props="{label: 'label', expand: 'expand', children: 'children', key:'some_id'}">
1919
<template #node="{data,context}">
2020
<span>
2121
<input type="checkbox" :checked="selected.indexOf(data.some_id)> -1" @change="(e)=>toggleSelect(data,e.target.checked)"/> {{data.label}}
@@ -141,7 +141,7 @@ createApp(App)
141141
prop | descripton | type | default
142142
------------------|-----------------------------------------|:----------------------:|:---------------------------------------------------------:
143143
data | | `Object` |
144-
props | configure props | `Object` | `{label: 'label', children: 'children', expand: 'expand'}`
144+
props | configure props | `Object` | `{label: 'label', children: 'children', expand: 'expand',key: 'id'}`
145145
labelWidth | node label width | `String` \| `Number` | `auto`
146146
collapsable | children node is collapsable | `Boolean` | `true`
147147
renderContent | how to render node label | `Function` | -

demo/demo-app.vue

+49-5
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,32 @@
77

88
<h1>Basic</h1>
99
<div>
10-
<blocks-tree :data="treeData" :horizontal="treeOrientation=='1'" :collapsable="true"></blocks-tree>
10+
<blocks-tree :props="{label: 'label', expand: 'expand', children: 'children', key:'some_id'}" :data="treeData" :horizontal="treeOrientation=='1'" :collapsable="true"></blocks-tree>
1111
</div>
1212
<h1>With slots</h1>
1313
<div>
14-
<blocks-tree :data="treeData" :horizontal="treeOrientation=='1'" :collapsable="true">
14+
<blocks-tree :data="treeData" :props="{label: 'label', expand: 'expand', children: 'children', key:'some_id'}" :horizontal="treeOrientation=='1'" :collapsable="true">
1515
<template #node="{data,context}">
1616
<span>
17-
<input type="checkbox" :checked="selected.indexOf(data.some_id)> -1" @change="(e)=>toggleSelect(data,e.target.checked)"/> {{data.label}}
17+
<input type="checkbox" :checked="selected.indexOf(data.some_id)> -1" @change="(e)=>toggleSelect(data,e.target.checked)"/> <strong>#{{data.some_id}}</strong> {{data.label}} &nbsp; <a v-if="data.some_id != treeData.some_id" title="Delete item" style="color:red;cursor:pointer;" @click="()=>deleteNode(data,treeData)">x</a>
1818
</span>
1919
<br/>
2020
<span v-if="data.children && data.children.length">
2121
<a href="#" @click="context.toggleExpand">toggle expand</a>
2222
</span>
23+
24+
25+
2326
</template>
2427
</blocks-tree>
2528
<div>
2629
Selected: {{selected}}
2730
</div>
31+
<div>
32+
<label for="add_leaf">Add leaf to item with id:</label>
33+
<input id="add_leaf" v-model="inputLeafId" type="number"/> <button @click="()=>tryAddLeaf(inputLeafId,treeData)">+</button>
34+
35+
</div>
2836
</div>
2937
</template>
3038
<script>
@@ -33,7 +41,7 @@ import { defineComponent,ref,reactive } from 'vue';
3341
export default defineComponent({
3442
3543
setup() {
36-
44+
let inputLeafId = ref();
3745
let selected = ref([]);
3846
let treeOrientation = ref("0");
3947
let treeData = reactive({
@@ -72,11 +80,47 @@ export default defineComponent({
7280
}
7381
}
7482
83+
const tryAddLeaf = (parentId,tree) => {
84+
85+
let isParent = tree.some_id == parentId;
86+
if(isParent){
87+
let some_id = parseInt(Math.random()*100)
88+
let leaf = {
89+
label:`child of ${tree.label}`,
90+
some_id:some_id,
91+
}
92+
if(!tree.children) {
93+
tree.expand = true;
94+
tree.children = [];
95+
}
96+
97+
tree.children.push(leaf);
98+
99+
}else if(tree.children){
100+
tree.children.forEach(ch=> tryAddLeaf(parentId,ch))
101+
}
102+
}
103+
104+
const deleteNode = (node,tree) => {
105+
106+
let parent = tree.children ? tree.children.find(p=>p.some_id == node.some_id) : null;
107+
if(parent){
108+
tree.children.splice(tree.children.indexOf(node),1)
109+
}else if(tree.children) {
110+
tree.children.forEach(ch=> deleteNode(node,ch))
111+
}
112+
113+
114+
}
115+
75116
return {
76117
treeData,
77118
selected,
78119
toggleSelect,
79-
treeOrientation
120+
treeOrientation,
121+
inputLeafId,
122+
tryAddLeaf,
123+
deleteNode
80124
}
81125
}
82126
})

dist/vue3-blocks-tree.common.js

+13-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vue3-blocks-tree.common.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vue3-blocks-tree.umd.js

+13-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vue3-blocks-tree.umd.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vue3-blocks-tree.umd.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vue3-blocks-tree.umd.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/app.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue3-blocks-tree",
3-
"version": "0.4.2",
3+
"version": "0.5.0",
44
"description": "A vue3 block organization tree view component. Hierarchical horizontal or vertical tree",
55
"main": "dist/vue3-blocks-tree.common.js",
66
"unpkg": "dist/vue3-blocks-tree.umd.js",

src/components/blocks-tree/blocks-node.vue

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</div>
1515
</div>
1616
<div v-if="(collapsable && expanded && !isLeaf) || (!collapsable && !isLeaf)" class="org-tree-node-children">
17-
<blocks-node v-for="ch in data[props.children]"
17+
<blocks-node v-for="ch in data[props.children]" :key="ch[props.key] || ch[props.label]"
1818
:data="ch"
1919
:props="props"
2020
:collapsable="collapsable"
@@ -51,7 +51,8 @@ export default defineComponent({
5151
default:()=>({
5252
label: 'label',
5353
expand: 'expand',
54-
children: 'children'
54+
children: 'children',
55+
key:'id'
5556
})
5657
},
5758
collapsable:{

src/components/blocks-tree/blocks-tree.vue

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<div class="org-tree-container">
33
<div class="org-tree" :class="{horizontal, collapsable}">
44
<blocks-node
5+
:key="data[props.key] || data[props.label]"
56
:data="data"
67
:props="props"
78
:horizontal="horizontal"
@@ -42,7 +43,8 @@ export default defineComponent({
4243
default: () => (<PropsType>{
4344
label: 'label',
4445
expand: 'expand',
45-
children: 'children'
46+
children: 'children',
47+
key:'id'
4648
})
4749
},
4850
horizontal: Boolean,

0 commit comments

Comments
 (0)