【问题标题】:How to create a tree-view in vue-js with clickable components opening up a vue-js modal?如何在 vue-js 中创建一个带有可点击组件的树视图,打开一个 vue-js 模态?
【发布时间】:2018-03-19 20:10:18
【问题描述】:

我想在 Vue-JS 中创建一个树视图组件来显示我的 JSON 数据。但是我有一个额外的要求,我需要在单击 JSON 视图的任何数据字段时启动 VueJS 模式。我查看了几个在树视图中显示数据的npm modules,但我不确定如何修改它们以使其可点击并启动vue-js modal。 这是我目前用来显示树视图的代码,但没有一个字段是可点击的。

assets.vue

    <template>

  <div>  

        <h1>The Assets Page</h1>
        <!--<p>{{response}}</p>-->
        <tree-view v-on:click="openGroupModal()" :data="response._embedded.assets" :options="{maxDepth: 0, rootObjectKey: 'Assets'}"></tree-view>
      </div>

</template>
<script>
import axios from 'axios'
import GroupModal from '../assets/assetdetails.vue'
import Vue from 'vue'
import VModal from 'vue-js-modal'
Vue.use(VModal, { dynamic: true })

export default {
  name: 'service',
  data () {
   return {
   response: [],
   errors: []
   }
   },
   
 created () {
      this.callRestService()
    },
 methods: {
   callRestService () {
    axios.get('http://localhost:8080/rest/assets')
   .then(response => {
   // JSON responses are automatically parsed.
   this.response = response.data
   })
   .catch(e => {
    this.errors.push(e)
  })
},
openGroupModal(){

  this.$modal.show(GroupModal,
  {
      draggable: true,
      resizable: true,
      scrollable: true,
      height: "auto",
      width:"50%"
    })
}
 }
}
</script>
<style>
</style> 

main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import VueRouter from 'vue-router'
import VModal from 'vue-js-modal'
import App from './App.vue'
import TreeView from 'vue-json-tree-view'

import { routes } from './routes';


Vue.use(VueRouter);
Vue.use(VModal, { dynamic: true })
Vue.use(TreeView)
//Vue.config.productionTip = false

const router = new VueRouter({

  routes  
});


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

有什么想法吗?

更新:我尝试了 Jacob 的回答,但这并没有帮助我启动模式。我试过这个:

<template>

  <div>  

        <h1>The Assets Page</h1>
        <!--<p>{{response}}</p>-->
        <div @click.capture="openGroupModal($event)">
        <tree-view :data="response._embedded.assets" :options="{maxDepth: 0, rootObjectKey: 'Assets'}"></tree-view>
        </div>
      </div>

</template>
<script>
import axios from 'axios'
import GroupModal from '../assets/assetdetails.vue'
import Vue from 'vue'
import VModal from 'vue-js-modal'
Vue.use(VModal, { dynamic: true })

export default {
  name: 'service',
  data () {
   return {
   response: [],
   errors: []
   }
   },
   
 created () {
      this.callRestService()
    },
 methods: {
   callRestService () {
    axios.get('http://localhost:8080/rest/assets')
   .then(response => {
   // JSON responses are automatically parsed.
   this.response = response.data
   })
   .catch(e => {
    this.errors.push(e)
  })
},
openGroupModal($event){
  console.log("The console message",$event.target);
  this.$modal.show(GroupModal,
  {
      draggable: true,
      resizable: true,
      scrollable: true,
      height: "auto",
      width:"50%"
    })
}
 }
}
</script>
<style>
</style> 

【问题讨论】:

  • 您的意思是您希望在路线上打开模式? IE:深度链接让你进入一个开放的模式?
  • 不是真的在路上。只需在树的特定节点上的 onClick 事件中弹出它。

标签: javascript npm vue.js


【解决方案1】:

嗯.. 一个 div 容器可以为你工作

使用@click.capture。 (一旦添加了.capture,针对内部元素的事件会先在此处处理,然后再由该元素处理,来自Vue doc)

您可以使用$event.target准确获取点击了哪个元素

例如

<template>
  <div @click.capture="openGroupModal($event)">
    <tree-view :data="{1:1,2:{a:'a'}}" :options="{maxDepth: 3}"></tree-view>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  methods:{
    openGroupModal($event){
      console.log($event.target);
      // other codes for showing modal and etc...
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

在 https://codesandbox.io/s/nkwk2k095l 中的 HelloWorld.vue 组件上测试

【讨论】:

  • 让我试试看。这应该使树的所有节点都可以点击?
  • 那行不通。我确实知道单击了哪个元素,但我无法在单击任何节点时启动模式。检查我尝试过的代码的更新。
  • @CodeHunter import VModal from 'vue-js-modal' ;Vue.use(VModal, { dynamic: true }) 应该在main.js 中完成,而不是在组件中。
  • @CodeHunter 我可以让模式在codesandbox.io/s/4x4vxx9984 中工作
  • 感谢您的回答!实际上,我也可以使用 npm vue-tree-harlow 包让它工作,它可以让您处理节点单击元素,并且您可以触发每个节点上的各个事件。
猜你喜欢
  • 2018-08-21
  • 2020-03-31
  • 2018-09-27
  • 2019-08-26
  • 2017-12-07
  • 2020-06-26
  • 2019-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多