vue实现树形菜单效果


Posted in Javascript onMarch 19, 2018

本文实例为大家分享了vue实现树形菜单效果展示的具体代码,供大家参考,具体内容如下

<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>vue</title>
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <link rel="stylesheet" href="animate.css">
 <script src="vue.js"></script>

 <style>
  *{
  color:#585858;
  
  }
  #app{
  min-height: 650px;
  }
  #app li{
  list-style-type:none;
  }
  #app a{
  text-decoration:none;
  }
  #app button{
  width:100%;
  }
  #app ul{
  padding:10px;
  }
  #app span{
  cursor:pointer;
  }
  #tree{
  border: 1px solid #ccc;  
  min-height: 650px;
  width: 50%;
  margin:0;
  padding-top: 10px;
  background-color:#f2f2f2;
  position: absolute;
  top:0;
  left:0;
  }
  
  #tree li {
  display: block;
  padding: 0;
  margin: 0;
  border: 0;
  border-bottom: 1px solid #e5e5e5;
  min-height: 32px;
  line-height:32px;
  }
 </style>
 </head>

 <body>

 <div id='app' @click='hideTree($event)'>
  <button @click.stop="show = !show">点我</button>
  
  <transition enter-active-class="animated fadeInLeft" leave-active-class="animated fadeOutLeft">
  <item v-bind:tree='treeData' id='tree' v-if="show"></item> 
  </transition> 
 </div>


 <template id='tree-template'>
  <ul>
  <li v-for='(v,i) in tree'> 
   <span v-if="isFolder(v)" @click="toggle(i)">{{ tree[i].open ? '-' : '+' }}</span>
   <a data-id="v.id">{{v.city}}</a> 
   <item v-bind:tree='v.child' v-show="tree[i].open"></item>
  </li>
  </ul> 
 </template>

 <script>
  var data = [{"id":26,"pid":1,"city":"四川省"},{"id":30,"pid":1,"city":"云南省"},{"id":322,"pid":26,"city":"成都"},{"id":323,"pid":26,"city":"绵阳"},{"id":324,"pid":26,"city":"阿坝"},{"id":325,"pid":26,"city":"巴中"},{"id":326,"pid":26,"city":"达州"},{"id":327,"pid":26,"city":"德阳"},{"id":328,"pid":26,"city":"甘孜"},{"id":329,"pid":26,"city":"广安"},{"id":330,"pid":26,"city":"广元"},{"id":331,"pid":26,"city":"乐山"},{"id":332,"pid":26,"city":"凉山"},{"id":333,"pid":26,"city":"眉山"},{"id":334,"pid":26,"city":"南充"},{"id":335,"pid":26,"city":"内江"},{"id":336,"pid":26,"city":"攀枝花"},{"id":337,"pid":26,"city":"遂宁"},{"id":338,"pid":26,"city":"雅安"},{"id":339,"pid":26,"city":"宜宾"},{"id":340,"pid":26,"city":"资阳"},{"id":341,"pid":26,"city":"自贡"},{"id":342,"pid":26,"city":"泸州"},{"id":367,"pid":30,"city":"昆明"},{"id":378,"pid":30,"city":"曲靖"},{"id":3100,"pid":367,"city":"盘龙区"},{"id":3101,"pid":367,"city":"五华区"},{"id":3102,"pid":367,"city":"官渡区"},{"id":3103,"pid":367,"city":"西山区"},{"id":3104,"pid":367,"city":"东川区"},{"id":3105,"pid":367,"city":"安宁市"},{"id":3106,"pid":367,"city":"呈贡县"},{"id":3107,"pid":367,"city":"晋宁县"},{"id":3108,"pid":367,"city":"富民县"},{"id":3109,"pid":367,"city":"宜良县"},{"id":3110,"pid":367,"city":"嵩明县"},{"id":3111,"pid":367,"city":"石林县"},{"id":3112,"pid":367,"city":"禄劝"},{"id":3113,"pid":367,"city":"寻甸"},{"id":3189,"pid":378,"city":"麒麟区"},{"id":3190,"pid":378,"city":"宣威市"},{"id":3191,"pid":378,"city":"马龙县"},{"id":3192,"pid":378,"city":"陆良县"},{"id":3193,"pid":378,"city":"师宗县"},{"id":3194,"pid":378,"city":"罗平县"},{"id":3195,"pid":378,"city":"富源县"},{"id":3196,"pid":378,"city":"会泽县"},{"id":3197,"pid":378,"city":"沾益县"}];

  var treeData = createTree({
  idname:'id',
  pidname:'pid',
  rootid:1,
  data:data
  });

  function createTree(arg){
  var idname = arg.idname,
   pidname = arg.pidname,
   rootid = arg.rootid,
   data = arg.data,
   treeData = [];
  var _createTree = function(id){
   var ret = []; 
   var index = 0;
   for(var i = 0; i < data.length; i++){  
   if(data[i][pidname] == id){
    ret[index] = data[i];
    ret[index].child = _createTree(data[i][idname]);
    index++;
   } 

   }
   return ret;
  }

  var index = 0;
  for(var i = 0; i < data.length; i++){  
   if(data[i][pidname] == rootid){
   treeData[index] = data[i];
   treeData[index].child = _createTree(data[i][idname]);
   index++;
   } 
  }
  return treeData;
  }

  Vue.component('item', {
  template: '#tree-template',
  props: ['tree'],
  data: function () {
   return {}
  },
  methods: {
   toggle: function (i) {
   this.tree[i].open = !this.tree[i].open;
   this.$set(this.tree, i, this.tree[i]);
   },
   isFolder: function (data) {
   return data.child && data.child.length
   },

  },
  })

  var vm = new Vue({
  el: '#app',
  data: {
   treeData: treeData,
   show:false,
  },
  methods: {
   hideTree:function(e){
   if(e.target.id == 'app'){
    console.log(137);
    this.show = false;
   }
   }
  },
  created: function () {
   function _addOpen(data) {
   for (var i = 0; i < data.length; i++) {
    data[i]['open'] = false;
    if (data[i].child.length > 0) {
    _addOpen(data[i].child);
    }
   }
   }
   _addOpen(this.treeData);
  }
  });
 </script>

 </body>

</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
Firefox下提示illegal character并出现乱码的原因
Mar 25 Javascript
8款非常棒的响应式jQuery 幻灯片插件推荐
Feb 02 Javascript
原生js 秒表实现代码
Jul 24 Javascript
js判断页面中是否有指定控件的简单实例
Mar 04 Javascript
addEventListener 的用法示例介绍
May 07 Javascript
JavaScript中具名函数的多种调用方式总结
Nov 08 Javascript
学习vue.js表单控件绑定操作
Dec 05 Javascript
JS获取短信验证码倒计时的实现代码
May 22 Javascript
使用vue + less 实现简单换肤功能的示例
Feb 21 Javascript
webpack 模块热替换原理
Apr 09 Javascript
JS的函数调用栈stack size的计算方法
Jun 24 Javascript
学习 Vue.js 遇到的那些坑
Feb 02 Vue.js
VUE重点问题总结
Mar 19 #Javascript
p5.js入门教程之键盘交互
Mar 19 #Javascript
vue 实现的树形菜的实例代码
Mar 19 #Javascript
Servlet3.0与纯javascript通过Ajax交互的实例详解
Mar 18 #Javascript
jQuery替换节点元素的操作方法
Mar 18 #jQuery
使用Angular CLI生成 Angular 5项目教程详解
Mar 18 #Javascript
分析javascript原型及原型链
Mar 18 #Javascript
You might like
php 方便水印和缩略图的图形类
2009/05/21 PHP
php array_search() 函数使用
2010/04/13 PHP
PHP实现的大文件切割与合并功能示例
2018/04/10 PHP
PHP面向对象程序设计(OOP)之方法重写(override)操作示例
2018/12/21 PHP
Jquery从头学起第四讲 jquery入门教程
2010/08/01 Javascript
javaScript同意等待代码实现心得
2011/01/01 Javascript
浅谈JavaScript函数节流
2014/12/09 Javascript
JS扩展方法实例分析
2015/04/15 Javascript
JavaScript中数据结构与算法(四):串(BF)
2015/06/19 Javascript
JavaScript运动减速效果实例分析
2015/08/04 Javascript
完美的js div拖拽实例代码
2016/09/24 Javascript
JS实现自动阅读单词(有道单词本添加功能)
2016/11/14 Javascript
详解基于vue的移动web app页面缓存解决方案
2017/08/03 Javascript
浅谈vue路径优化之resolve
2017/10/13 Javascript
javascript实现电脑和手机版样式切换
2017/11/10 Javascript
详解如何使用webpack打包JS
2018/06/21 Javascript
vuejs router history 配置到iis的方法
2018/09/20 Javascript
Nodejs把接收图片base64格式保存为文件存储到服务器上
2018/09/26 NodeJs
vue使用Font Awesome的方法步骤
2019/02/26 Javascript
跟混乱的页面弹窗说再见
2019/04/11 Javascript
JS字符串与二进制的相互转化实例代码详解
2019/06/28 Javascript
[03:16]DOTA2完美大师赛小组赛精彩集锦
2017/11/22 DOTA
[01:16:16]DOTA2-DPC中国联赛定级赛 RNG vs Phoenix BO3第二场 1月8日
2021/03/11 DOTA
不管你的Python报什么错,用这个模块就能正常运行
2018/09/14 Python
Python 基于wxpy库实现微信添加好友功能(简洁)
2019/11/29 Python
Python .py生成.pyd文件并打包.exe 的注意事项说明
2021/03/04 Python
linux比较文件内容的命令是什么
2015/09/23 面试题
北大青鸟学生求职信
2013/09/24 职场文书
出国留学自荐信
2013/10/25 职场文书
甲方资料员岗位职责
2013/12/13 职场文书
小学生竞选班干部演讲稿(5篇)
2014/09/12 职场文书
2014年学校团委工作总结
2014/12/20 职场文书
公司承诺函范文
2015/01/21 职场文书
初中英语教学随笔
2015/08/15 职场文书
2016年党员读书月活动总结
2016/04/06 职场文书
Python可视化神器pyecharts之绘制箱形图
2022/07/07 Python