vue组件横向树实现代码


Posted in Javascript onAugust 02, 2018

将之前的用css3+jq实现的横向树样式简单封装成组件使用到vue项目中,文件名为transverseTree.vue

代码:

<template>
 <div class="tree">
  <ul v-if="treeData && treeData.length">
   <li v-for="(column,index) in treeData">
    <span class="root">{{column.name}}</span>
    <ul v-if="column.children && column.children.length">
     <li v-for="(childrenColumn,index) in column.children">
      <span>{{childrenColumn.name}}</span>
      <ul v-if="childrenColumn.children && childrenColumn.children.length">
       <li v-for="(grandChildrenColumn,index) in childrenColumn.children">
        <span>{{grandChildrenColumn.name}}</span>
       </li>
      </ul>
     </li>
    </ul>
   </li>
  </ul>
 </div>
</template>
<script>
 export default {
 name: 'transverseTree',
 props: {
  treeData:{
   type:Array,
   default:[]
  }
 },
 methods: {
  editDom(){
   if($('.root').siblings('ul').children('li').length==1){
    let num = 26*($('.root').siblings('ul').children('li').find('li').length-1);
    $('.root').css({ 'top': num });
    $('.root').siblings('ul').children('li').css({ 'top': num });
    $('.root').siblings('ul').find('ul').css({ 'top': -num });
    if($('.root').siblings('ul').find('li').length > 1){
     $('.root').siblings('ul').children('li').children('span').addClass('hasChild');
    }
   }else{
    $('.root').css({ 'top': 26 * ($('.root').siblings('ul').children('li').length - 1) });
   }
  }
 },
 mounted() {
  this.$nextTick(()=>{
   this.editDom();
  });
 }
 };
</script>
<style scope>
.tree{
 position: relative;
 margin: -16px -16px 0;
 min-height: 400px;
 padding-left: 11px;
 overflow: auto;
}
.tree ul{
 width: 210px;
 height: 100%;
 position: absolute;
}
.tree ul ul{
 left: 226px;
 top: 0;
}
.tree li{
 float: left;
 list-style-type: none;
 position: relative;
 padding: 16px 5px 0 5px;
}
.tree li span{
 position: relative;
 display: inline-block;
 width: 200px;
 height: 36px;
 background: #F0F0F5;
 border-radius: 4px;
 text-decoration: none;
 color: #2D2D2D;
 font-size: 14px;
 line-height: 36px;
 text-align: center;
}
.tree li::before{
 box-sizing:inherit;
 content: '';
 position: absolute;
 top: 33px;
 left: -7px;
 border-top: 2px solid #D2D2D7;
 width: 12px;
}
.tree li::after{
 box-sizing:inherit;
 content: '';
 position: absolute;
 top: 8px;
 left: -9px;
 height: 100%;
 border-left: 2px solid #D2D2D7;
}
.tree li:first-child::after{
 height: 51%;
 border-left: 2px solid #D2D2D7;
 border-top: 2px solid #D2D2D7;
 top: 33px;
 width: 1px;
 border-top-left-radius: 4px;
}
.tree li:last-child::after{
 height: 25px;
 border-left: 2px solid #D2D2D7;
 border-bottom: 2px solid #D2D2D7;
 top: 8px;
 width: 1px;
 border-bottom-left-radius: 4px;
}
.tree li:only-child::after,
.tree li:only-child::before{
 display: none;
}
.tree ul ul li:only-child::before{
 display: inline-block;
}
.tree ul ul li:only-child span::before{
 display: inline-block;
}
.tree li:only-child span.root::before,.tree li:only-child span.hasChild::before{
 content: '';
 position: absolute;
 top: 17px;
 right: -14px;
 border-top: 2px solid #D2D2D7;
 width: 14px;
}
.tree ul ul ul li:only-child span::before{
 content: '';
 position: absolute;
 top: 17px;
 left: -26px;
 border-top: 2px solid #D2D2D7;
 width: 26px;
}
</style>

在父组件中使用import引入该组件:

import transverseTree from './transverseTree'

注册组件:

components: { ifbpInfolistCard,transverseTree },

在template中使用:

<transverse-tree :treeData='treeData'></transverse-tree>

其中,treeData为一个数组,在data中给treeData一个初始值:

treeData: [
{name:'报表名称1',
children:[
{name:'功能名称1',
children:[
{name:'磁贴名称1'}
]},
{name:'功能名称2',
children:[
{name:'磁贴名称1'}
]},
{name:'功能名称3',
children:[
{name:'磁贴名称1'}
]},
]}
]

实现效果:

vue组件横向树实现代码

vue组件横向树实现代码

ps:需要特别说明的是,我目前的代码暂时只支持这两种样式,即:

1父节点-1子节点-1/多孙节点,或是1父节点-多子节点-1孙节点,样式是通过jq去判断修改的,以后有时间的话再去研究优化争取可复用性强一些。希望对大家能有所帮助。

总结

以上所述是小编给大家介绍的vue组件横向树实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
用prototype实现的简单小巧的多级联动菜单
Mar 24 Javascript
让AJAX不依赖后端接口实现方案
Dec 03 Javascript
js自动下载文件到本地的实现代码
Apr 28 Javascript
jQuery实现获取table表格第一列值的方法
Mar 01 Javascript
微信小程序 获取相册照片实例详解
Nov 16 Javascript
实例解析Array和String方法
Dec 14 Javascript
React Native使用fetch实现图片上传的示例代码
Mar 07 Javascript
详解Vue取消eslint语法限制
Aug 04 Javascript
angular6 填坑之sdk的方法
Dec 27 Javascript
vue前端框架—Mint UI详解(更适用于移动端)
Apr 30 Javascript
VUE 实现动态给对象增加属性,并触发视图更新操作示例
Nov 29 Javascript
node.js基础知识汇总
Aug 25 Javascript
利用Node.js批量抓取高清妹子图片实例教程
Aug 02 #Javascript
在微信小程序里使用watch和computed的方法
Aug 02 #Javascript
在小程序中使用Echart图表的示例代码
Aug 02 #Javascript
node.js读取Excel数据(下载图片)的方法示例
Aug 02 #Javascript
Vue-cli配置打包文件本地使用的教程图解
Aug 02 #Javascript
详解使用VueJS开发项目中的兼容问题
Aug 02 #Javascript
重新认识vue之事件阻止冒泡的实现
Aug 02 #Javascript
You might like
谨慎使用PHP的引用原因分析
2012/09/06 PHP
对于PHP 5.4 你必须要知道的
2013/08/07 PHP
php打造智能化的柱状图程序,用于报表等
2015/06/19 PHP
PHP将二维数组某一个字段相同的数组合并起来的方法
2016/02/26 PHP
PHP常见错误提示含义解释(实用!值得收藏)
2016/04/25 PHP
PHP进阶学习之Geo的地图定位算法详解
2019/06/19 PHP
IE php关于强制下载文件的代码
2008/08/23 Javascript
漂亮的jquery提示效果(仿腾讯弹出层)
2013/02/05 Javascript
jQuery JSON实现无刷新三级联动实例探讨
2013/05/28 Javascript
js中浮点型运算BUG的解决方法说明
2014/01/06 Javascript
js检验密码强度(低中高)附图
2014/06/05 Javascript
JQuery CheckBox(复选框)操作方法汇总
2015/04/15 Javascript
浅谈JavaScript 覆盖原型以及更改原型
2016/08/31 Javascript
微信小程序 框架详解及实例应用
2016/09/26 Javascript
jQuery实现大图轮播
2017/02/13 Javascript
JS实现按钮添加背景音乐示例代码
2017/10/17 Javascript
Vue利用路由钩子token过期后跳转到登录页的实例
2017/10/26 Javascript
vue+swiper实现组件化开发的实例代码
2017/10/26 Javascript
详解关于Angular4 ng-zorro使用过程中遇到的问题
2018/12/05 Javascript
JS实现判断数组是否包含某个元素示例
2019/05/24 Javascript
jQuery实现简单飞机大战
2020/07/05 jQuery
Vue常用API、高级API的相关总结
2021/02/02 Vue.js
Python原始字符串(raw strings)用法实例
2014/10/13 Python
零基础写python爬虫之神器正则表达式
2014/11/06 Python
用 Django 开发一个 Python Web API的方法步骤
2020/12/03 Python
西班牙鞋子和箱包在线销售网站:zapatos.es
2020/02/17 全球购物
会计毕业生自荐信
2013/11/21 职场文书
企业演讲稿范文
2013/12/28 职场文书
人力资源作业细则
2014/03/03 职场文书
毕业生如何写自我鉴定
2014/03/15 职场文书
施工质量承诺书范文
2014/05/30 职场文书
私人委托书格式
2014/09/10 职场文书
领导班子群众路线与四风问题对照检查材料思想汇报
2014/10/11 职场文书
离职信范本
2015/06/23 职场文书
两行代码解决Jupyter Notebook中文不能显示的问题
2021/04/24 Python
Python 恐龙跑跑小游戏实现流程
2022/02/15 Python