In my current setup, I've implemented a TreeView
component that holds a tree
. Each tree
entry includes Children
with their own unique label
, perm
, and further children
.
Take a look at an example of the tree:
App.vue
let tree = ref({
label: 'org',
perm: {
POST: "auth",
GET: "null"
},
children: [
{
label: '*',
perm: {
POST: "auth",
GET: "null"
},
children: [{
label: 'repo',
perm: {
POST: "null",
GET: "null"
},
children: []
}]
},
{
label: 'test',
perm: {
POST: "auth",
GET: "auth"
},
children: []
}
]
})
To display this in a simple Tree View, I'm using the following setup:
TreeView.vue
<div @click="toggleHideShow()">
{{ label }}
</div>
<div class="child" v-if="hideShow">
<TreeView v-for="child in children" :label="child.label" :perm="child.perm" :children="child.children" />
</div>
My goal is to be able to dynamically add children through this TreeView. Is there a way for me to access the exact Children
object upon clicking?
Currently, I attempted to pass attributes through the toggleHideShow
method:
<div @click="toggleHideShow(label, perm, children)">
After that, I create a new Children
object, iterate through the tree
, and compare each one to find the matching object.
const toggleHideShow = (label: string, perm: Perm, children?: Array<Children>)=> {
hideShow.value = !hideShow.value
const newChild: Children = {
label: label,
perm: perm,
children: children
}
//do for loop here
}
The issue arises from my tree
variable being declared in App.vue, which I then pass into my TreeView component.
<TreeView :label="tree.label" :perm="tree.perm" :children="tree.children" />
Here are my three questions:
1: How can I access the tree
variable within my TreeView component? Will modifying it also alter the tree
variable inside App.vue, therefore updating the TreeView dynamically?
2: Would it be more efficient to create a method within App.vue that takes in the newly created newChild
variable as a parameter and adds it to the tree from there? If so, how would I implement this method in my TreeView component?
3: Are there other methods for me to identify the specific Children
object within the tree
that was clicked on? And how can I retrieve it from the tree
variable considering it's not an array?