iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >vue-tree-chart树形组件的实现(含鼠标右击事件)
  • 490
分享到

vue-tree-chart树形组件的实现(含鼠标右击事件)

2024-04-02 19:04:59 490人浏览 八月长安
摘要

基于 Vue-tree-chart,生成项目效果预览,包含鼠标右击事件; vue-tree-chart:https://GitHub.com/tower1229/Vue-Tree-

基于 Vue-tree-chart,生成项目效果预览,包含鼠标右击事件;

在这里插入图片描述

vue-tree-chart:https://GitHub.com/tower1229/Vue-Tree-Chart

大家可以直接安装使用(具体事例可以查看官网)

但是个人建议最好是下载整个项目,封装成组件调用

基于官网初始代码,封装组件:

<template>
    <table v-if="treeData.name">
      <tr>
        <td :colspan="Array.isArray(treeData.children) ? treeData.children.length * 2 : 1" 
          :class="{parentLevel: Array.isArray(treeData.children) && treeData.children.length, extend: Array.isArray(treeData.children) && treeData.children.length && treeData.extend}"
        >
          <div :class="{node: true, hasMate: treeData.mate}">
            <div class="person" 
              :class="Array.isArray(treeData.class) ? treeData.class : []"
             
            >
              <div class="avat">
                <img :src="treeData.image_url" @contextmenu="$emit('click-node', treeData)"/>
              </div>
              <!-- <div class="name">{{treeData.name}}</div> -->
            </div>
             <div class="paeson_name">{{treeData.name}}</div>
            <template v-if="Array.isArray(treeData.mate) && treeData.mate.length">
              <div class="person" v-for="(mate, mateIndex) in treeData.mate" :key="treeData.name+mateIndex"
                :class="Array.isArray(mate.class) ? mate.class : []"
                @click="$emit('click-node', mate)"
              >
                <div class="avat">
                  <img :src="mate.image_url" />
                </div>
                <!-- <div class="name">{{mate.name}}</div> -->
              </div>
              <div class="paeson_name">{{treeData.name}}</div>
            </template>
          </div>
          <div class="extend_handle" v-if="Array.isArray(treeData.children) && treeData.children.length" @click="toggleExtend(treeData)"></div>
        </td>
      </tr>
      <tr v-if="Array.isArray(treeData.children) && treeData.children.length && treeData.extend">
        <td v-for="(children, index) in treeData.children" :key="index" colspan="2" class="childLevel">
          <TreeChart :JSON="children" @click-node="$emit('click-node', $event)"/>
        </td>
      </tr>
    </table>
</template>

<script>
export default {
  name: "TreeChart",
  props: ["json"],
  data() {
    return {
      treeData: {}
    }
  },
  watch: {
    json: {
      handler: function(Props){
        let extendKey = function(jsonData){
          jsonData.extend = (jsonData.extend===void 0 ? true: !!jsonData.extend);
          if(Array.isArray(jsonData.children)){
            jsonData.children.forEach(c => {
              extendKey(c)
            })
          }
          return jsonData;
        }
        if(Props){
          this.treeData = extendKey(Props);
        }
      },
      immediate: true
    }
  },
  methods: {
    toggleExtend: function(treeData){
      treeData.extend = !treeData.extend;
      this.$forceUpdate();
    }
  }
}
</script>

<style scoped>
table{border-collapse: separate!important;border-spacing: 0!important;}
td{position: relative; vertical-align: top;padding:0 0 50px 0;text-align: center; }
.extend_handle{position: absolute;left:50%;bottom:30px; width:10px;height: 10px;padding:10px;transfORM: translate3D(-15px,0,0);cursor: pointer;}
.extend_handle:before{content:""; display: block; width:100%;height: 100%;box-sizing: border-box; border:2px solid;border-color:#ccc #ccc transparent transparent;
transform: rotateZ(135deg);transform-origin: 50% 50% 0;transition: transform ease 300ms;}
.extend_handle:hover:before{border-color:#333 #333 transparent transparent;}

.extend::after{content: "";position: absolute;left:50%;bottom:15px;height:15px;border-left:2px solid #ccc;transform: translate3d(-1px,0,0)}
.childLevel::before{content: "";position: absolute;left:50%;bottom:100%;height:15px;border-left:2px solid #ccc;transform: translate3d(-1px,0,0)}
.childLevel::after{content: "";position: absolute;left:0;right:0;top:-15px;border-top:2px solid #ccc;}
.childLevel:first-child:before, .childLevel:last-child:before{display: none;}
.childLevel:first-child:after{left:50%;height:15px; border:2px solid;border-color:#ccc transparent transparent #ccc;border-radius: 6px 0 0 0;transform: translate3d(1px,0,0)}
.childLevel:last-child:after{right:50%;height:15px; border:2px solid;border-color:#ccc #ccc transparent transparent;border-radius: 0 6px 0 0;transform: translate3d(-1px,0,0)}
.childLevel:first-child.childLevel:last-child::after{left:auto;border-radius: 0;border-color:transparent #ccc transparent transparent;transform: translate3d(1px,0,0)}
.node{position: relative; display: inline-block;margin: 0 1em;box-sizing: border-box; text-align: center;}
.node:hover{color: #2d8cf0;cursor: pointer;}
.node .person{position: relative; display: inline-block;z-index: 2;width:6em; overflow: hidden;}
.node .person .avat{display: block;width:4em;height: 4em;margin:auto;overflow:hidden; background:#fff;border:1px solid #ccc;box-sizing: border-box;}
.node .person .avat:hover{ border: 1px solid #2d8cf0;}
.node .person .avat img{width:100%;height: 100%;}
.node .person .name{height:2em;line-height: 2em;overflow: hidden;width:100%;}
.node.hasMate::after{content: "";position: absolute;left:2em;right:2em;top:2em;border-top:2px solid #ccc;z-index: 1;}
.node .paeson_name{transform: rotate(90deg);position: absolute; top: 68px;right: 39px;width: 88px;text-align: center;text-overflow: ellipsis; overflow: hidden; white-space: nowrap;}


.landscape{transform:translate(-100%,0) rotate(-90deg);transform-origin: 100% 0;}
.landscape .node{text-align: left;height: 8em;width:8em;right: 18px;}
.landscape .person{position: absolute; transform: rotate(90deg);height: 4em;top:4em;left: 2.5em;}
.landscape .person .avat{position: absolute;left: 0;border-radius: 2em;border-width:2px;}
.landscape .person .name{height: 4em; line-height: 4em;}
.landscape .hasMate{position: relative;}
.landscape .hasMate .person{position: absolute; }
.landscape .hasMate .person:first-child{left:auto; right:-4em;}
.landscape .hasMate .person:last-child{left: -4em;margin-left:0;}
</style>

新建一个组件,调用组件并增加鼠标右击事件:

<template>
  <div id="app">
    <TreeChart :json="data" :class="{landscape: 1}" @click-node="clickNode" />

    <div class="gl_prs_ctn" :style='[contextstyle]'>
          <ul class='gl_prs_li'>
              <li >添加</li>
              <li >详情</li>
              <li >编辑</li>
              <li >删除</li>
          </ul>
    </div>  

  </div>
</template>

<script>
import TreeChart from "./treechar";
export default {
  name: 'app',
  components: {
    TreeChart
  },
  data() {
    return {
      data: {
        name: 'root',
        image_url: "Https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3689173839,956040439&fm=26&gp=0.jpg",
        class: ["rootNode"],
        children: [
          {
            name: 'children1',
            image_url: "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3689173839,956040439&fm=26&gp=0.jpg"
          },
          {
            name: 'children2',
            image_url: "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3689173839,956040439&fm=26&gp=0.jpg",
            children: [
              {
                name: 'grandchild',
                image_url: "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3689173839,956040439&fm=26&gp=0.jpg"
              },
              {
                name: 'grandchild2',
                image_url: "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3689173839,956040439&fm=26&gp=0.jpg"
              },
              {
                name: 'grandchild3',
                image_url: "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3689173839,956040439&fm=26&gp=0.jpg"
              }
            ]
          }
        ]
      },
      contextstyle: {
          display: 'none',
          right: '0px',
          top: '0px',
          left: '0px',
          bottom: '0px',
      }, 
    }
  },
  created(){
      document.oncontextmenu = ()=>{return false}
      document.addEventListener("click", (event) => {
            if(this.contextstyle.display == 'block'){
                this.contextstyle.display = 'none'
            }
      })
  },
  methods: {
      clickNode(node){
        if(window.event.x + 188 > document.documentElement.clientWidth){
            this.contextstyle.left = 'unset';
            this.contextstyle.right = document.documentElement.clientWidth - window.event.x + 'px';
        }else{
            this.contextstyle.left = window.event.x + 'px';
        }
        if(window.event.y + 166 > document.documentElement.clientHeight){
            this.contextstyle.top = 'unset';
            this.contextstyle.bottom = document.documentElement.clientHeight - window.event.y + 'px';
        }else{
            this.contextstyle.top = window.event.y + 'px';
        }                       
        this.contextstyle.display = 'block';
      },
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -WEBkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
.gl_prs_ctn{
            width: 188px;
            background: rgb(255, 255, 255);
            box-shadow: rgba(0, 0, 0, 0.075) 0px 1px 1px inset, rgba(102, 175, 233, 0.6) 0px 0px 8px;
            z-index: 99999;
            position: fixed;
            padding: 10px;
            box-sizing: content-box;
            height: 142px;
}
.gl_prs_li{padding: unset;margin: unset;}
.gl_prs_li>li{
    cursor: pointer;   
    list-style: none;
    border-bottom: 1px solid #efefef;
    padding: 7px 10px;
}
li:last-child { border: unset }
li:hover{
      background: #ccc;
      color: #fff;
}
</style>

到此这篇关于vue-tree-chart树形组件的实现(含鼠标右击事件)的文章就介绍到这了,更多相关vue-tree-chart 树形组件内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: vue-tree-chart树形组件的实现(含鼠标右击事件)

本文链接: https://www.lsjlt.com/news/140729.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • vue-tree-chart树形组件的实现(含鼠标右击事件)
    基于 vue-tree-chart,生成项目效果预览,包含鼠标右击事件; vue-tree-chart:https://github.com/tower1229/Vue-Tree-...
    99+
    2024-04-02
  • Vue组件tree如何实现树形菜单
    这篇文章主要为大家展示了“Vue组件tree如何实现树形菜单”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Vue组件tree如何实现树形菜单”这篇文章吧。vue...
    99+
    2024-04-02
  • 怎么使用Vue组件tree实现树形菜单
    本篇内容主要讲解“怎么使用Vue组件tree实现树形菜单”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用Vue组件tree实现树形菜单”吧!vue 编写的树形菜单,小巧实用,支持vue1....
    99+
    2023-07-04
  • vue递归实现树形组件
    本文实例为大家分享了vue递归实现树形组件的具体代码,供大家参考,具体内容如下 1. 先来看一下效果: 2. 代码部分 (myTree.vue) 图片可以自己引一下自己的图片,或者...
    99+
    2024-04-02
  • Vue+Elementui实现树形控件右键菜单
    本文实例为大家分享了Vue+Element ui实现树形控件右键菜单的具体代码,供大家参考,具体内容如下 需求 实现树形控件右键菜单功能,有添加文件、删除文件、重命名功能 一、按需引...
    99+
    2024-04-02
  • jQuery鼠标点击事件怎么实现
    可以使用 jQuery 的 click() 方法来实现鼠标点击事件。例如,假设有一个按钮的 HTML 代码如下:``````那么可以...
    99+
    2023-05-29
    jQuery鼠标点击事件 jQuery
  • Vue2组件tree如何实现无限级树形菜单
    小编给大家分享一下Vue2组件tree如何实现无限级树形菜单,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!How to run ...
    99+
    2024-04-02
  • vue递归组件实现树形结构
    本文实例为大家分享了vue递归组件实现树形结构,供大家参考,具体内容如下 一、递归组件 什么是递归组件?简单来说就是在组件中内使用组件本身。函数自己调用自己。很多情况下我们呢刷数据的...
    99+
    2024-04-02
  • Vue下如何实现一个树形组件
    这篇文章主要介绍“Vue下如何实现一个树形组件”,在日常操作中,相信很多人在Vue下如何实现一个树形组件问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Vue下如何实现一个树形组件”的疑惑有所帮助!接下来,请跟...
    99+
    2023-07-04
  • Vue中怎么实现一个树形组件
    Vue中怎么实现一个树形组件,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。具体如下:使用SemanticUI和vue做一个me...
    99+
    2024-04-02
  • 使用pyqt5 实现ComboBox的鼠标点击触发事件
    一、自定义MyComboBox # MyComboBox.py from PyQt5.QtWidgets import QComboBox from PyQt5.QtCore i...
    99+
    2024-04-02
  • 怎么使用vue递归实现树形组件
    这篇文章主要介绍“怎么使用vue递归实现树形组件”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“怎么使用vue递归实现树形组件”文章能帮助大家解决问题。1. 先来看一下效果:2. 代码部分 (myTr...
    99+
    2023-07-02
  • vue如何利用树形组件实现删除双击增加分支
    这篇“vue如何利用树形组件实现删除双击增加分支”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“vue如何利用树形组件实现删除...
    99+
    2023-07-04
  • vue鼠标悬停事件监听实现方法
    目录前言情景描述解决方法总结前言 开发框架为 vue2.x 情景描述 需求是这样的:页面在鼠标悬停(不动)n秒之后,页面进行相应的事件。 比如在我的需求下,是鼠标悬停15秒之后,页面...
    99+
    2024-04-02
  • vue递归组件之如何实现简单树形控件
    这篇文章主要介绍vue递归组件之如何实现简单树形控件,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!1、递归组件-简单树形控件预览及问题 在编写树形组件时遇到的问题:组件如何...
    99+
    2024-04-02
  • vue实现鼠标悬停事件的代码怎么写
    本篇内容介绍了“vue实现鼠标悬停事件的代码怎么写”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!v-bind:title="mes...
    99+
    2023-07-04
  • vue如何使用递归组件实现一个树形控件
    这篇文章主要介绍“vue如何使用递归组件实现一个树形控件”,在日常操作中,相信很多人在vue如何使用递归组件实现一个树形控件问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”vue如何使用递归组件实现一个树形控件...
    99+
    2023-07-04
  • 怎么使用Vue递归组件实现树形菜单
    本文小编为大家详细介绍“怎么使用Vue递归组件实现树形菜单”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么使用Vue递归组件实现树形菜单”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。效果如下图,点击后打开二级...
    99+
    2023-07-04
  • Vue Element-ui如何实现树形控件节点添加图标
    本篇内容主要讲解“Vue Element-ui如何实现树形控件节点添加图标”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Vue Element-ui如何实现树形控件节点添加图...
    99+
    2023-06-21
  • Vue组件库ElementUI实现表格加载树形数据教程
    ElementUI实现表格树形列表加载教程,供大家参考,具体内容如下 Element UI 是一套采用 Vue 2.0 作为基础框架实现的组件库,一套为开发者、设计师和产品经理准备的...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作