簡體   English   中英

當我單擊它們時如何獲取特定樹節點的ID,然后將其傳遞到變量中

[英]How to get id of a specific tree node when I click on them and then pass that data into my variable

我是Quasar和Vue的新手。 有人可以向我解釋如何解決我的任務嗎?

簡要介紹一下任務:

(1)我有一個q樹元素,它表示屏幕左側的文件夾結構[ref.1]

(2)這是文件夾結構[ref.2]

(3)當用戶單擊此文件夾結構中的任何元素時,他將在右側看到一個新組件,在網格布局中,該組件具有被單擊的所有子元素。

這就是我現在所擁有的。

[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

 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'} ] } ] } ] 

您需要用key替換id。此操作之后為每個節點添加此處理程序

handler: (node) => this.onclick(node)

然后在方法中添加此方法

onclick(node) {
  alert(node.key)
},

這將顯示垂直節點的ID

因此,主要問題與對Quasar框架的了解不夠充分有關。 這是我對這個問題的回答:

<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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM