[英]How to get id of a specific tree node when I click on them and then pass that data into my variable
I am new to Quasar and Vue. 我是Quasar和Vue的新手。 Could someone explain to me how to solve my task?
有人可以向我解释如何解决我的任务吗?
Briefly about the task: 简要介绍一下任务:
(1) I have a q-tree element which represents the folder structure at the left side of a screen [ref.1] (1)我有一个q树元素,它表示屏幕左侧的文件夹结构[ref.1]
(2) Here is a folder structure [ref.2] (2)这是文件夹结构[ref.2]
(3) When the user clicks on any element in this folder structure, then he will see a new component on the right side with all children elements of clicked one in a grid layout. (3)当用户单击此文件夹结构中的任何元素时,他将在右侧看到一个新组件,在网格布局中,该组件具有被单击的所有子元素。
This is what do I have now. 这就是我现在所拥有的。
[ref.1] treeComponent.vue [ref.1] treeComponent.vue
<template> <q-tree :nodes="documents" @click="getId" node-key="id" > </q-tree> </template> <script> var documents = require('./documents') module.exports = { data: function () { return { selectedDoc: x, documents: documents } }, methods: { getId: function () { const x = this.getNodeByKey('id') consol.log(x) } } } </script>
[ref.2] documents.js [ref.2] documents.js
module.exports = [ { id: '1', label: 'My Documents', icon: 'folder', children: [ { id: '01', label: 'Dir 1', children: [ { id: '0001', label: 'Doc 1'}, { id: '0002', label: 'Doc 2'} ] }, { id: '02', label: 'Dir 2', children: [ { id: '0003', label: 'Doc 3'}, { id: '0004', label: 'Doc 4'} ] }, { id: '103', label: 'Dir 3', children: [ { id: '0005', label: 'Doc 5'}, { id: '0006', label: 'Doc 6'}, { id: '0007', label: 'Doc 7'} ] } ] } ]
you need to replace id by key.after this add this handler for each node 您需要用key替换id。此操作之后为每个节点添加此处理程序
handler: (node) => this.onclick(node)
then add this method in methods 然后在方法中添加此方法
onclick(node) {
alert(node.key)
},
this will display id of perticular node 这将显示垂直节点的ID
So, the main problem was related to not good enough acquainted with Quasar framework. 因此,主要问题与对Quasar框架的了解不够充分有关。 Here is my answer to this question:
这是我对这个问题的回答:
<template>
<button v-on:click = "showNodeSelected">showClickedNode</button>
<q-tree
:nodes = "documents"
:selected.sync = "selected"
node-key="id"
/>
</template>
<script>
var documents = require('./documents')
module.exports = {
data: function () {
return {
selected: null,
documents: documents
}
},
methods: {
showNodeSelected: function () {
console.log(this.selected)
}
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.