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" rel="external nofollow" >
    <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>

总结

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

Javascript 相关文章推荐
浅析Prototype的模板类 Template
Dec 07 Javascript
用JQUERY增删元素的代码
Feb 14 Javascript
Javascript 中的 call 和 apply使用介绍
Feb 22 Javascript
Javascript操作URL函数修改版
Nov 07 Javascript
手机平板等移动端适配跳转URL的js代码
Jan 25 Javascript
深入理解JavaScript系列(31):设计模式之代理模式详解
Mar 03 Javascript
利用jquery去掉时光轴头尾部线条的方法实例
Jun 16 jQuery
微信小程序实现多宫格抽奖活动
Apr 15 Javascript
vue项目中应用ueditor自定义上传按钮功能
Apr 27 Javascript
JS与jQuery实现ListBox上移,下移,左移,右移操作功能示例
May 31 jQuery
JavaScript enum枚举类型定义及使用方法
May 15 Javascript
VUE异步更新DOM - 用$nextTick解决DOM视图的问题
Nov 06 Javascript
Servlet3.0与纯javascript通过Ajax交互的实例详解
Mar 18 #Javascript
jQuery替换节点元素的操作方法
Mar 18 #jQuery
使用Angular CLI生成 Angular 5项目教程详解
Mar 18 #Javascript
分析javascript原型及原型链
Mar 18 #Javascript
jquery 实现拖动文件上传加载进度条功能
Mar 18 #jQuery
jQuery中each方法的使用详解
Mar 18 #jQuery
JavaScript中字符串的常用操作方法及特殊字符
Mar 18 #Javascript
You might like
SESSION信息保存在哪个文件目录下以及能够用来保存什么类型的数据
2012/06/17 PHP
php中stdClass的用法分析
2015/02/27 PHP
PHP实现简单搜歌的方法
2015/07/28 PHP
php+Memcached实现简单留言板功能示例
2017/02/15 PHP
PHP手机短信验证码实现流程详解
2018/05/17 PHP
javascript FormatNumber函数实现方法
2008/12/30 Javascript
基于jquery+thickbox仿校内登录注册框
2010/06/07 Javascript
基于jQuery的遍历同id元素 并响应事件的代码
2012/06/14 Javascript
jquery增加时编辑jqGrid(实例代码)
2013/11/08 Javascript
js关于精确计算和数值格式化以及直接引js文件
2014/01/28 Javascript
javascript相关事件的几个概念
2015/05/21 Javascript
javascript中Array()数组函数详解
2015/08/23 Javascript
JS仿Base.js实现的继承示例
2017/04/07 Javascript
NodeJS 实现手机短信验证模块阿里大于功能
2017/06/19 NodeJs
Vue-Router进阶之滚动行为详解
2017/09/13 Javascript
原生JS实现列表内容自动向上滚动效果
2019/05/22 Javascript
使用异步controller与jQuery实现卷帘式分页
2019/06/18 jQuery
ES6 Promise对象的应用实例分析
2019/06/27 Javascript
改变layer confirm弹窗按钮的颜色方法
2019/09/12 Javascript
echarts实现折线图的拖拽效果
2019/12/19 Javascript
微信小程序实现锚点跳转
2020/11/23 Javascript
[52:09]2014 DOTA2华西杯精英邀请赛 5 25 NewBee VS DK第二场
2014/05/26 DOTA
python模块导入的细节详解
2018/12/10 Python
Django 对象关系映射(ORM)源码详解
2019/08/06 Python
如何利用pygame实现简单的五子棋游戏
2019/12/29 Python
Python urlopen()参数代码示例解析
2020/12/10 Python
python 如何把docker-compose.yaml导入到数据库相关条目里
2021/01/15 Python
万得城电器土耳其网站:欧洲第一大电子产品零售商
2016/10/07 全球购物
资产经营总监岗位职责范文
2013/12/01 职场文书
质检员岗位职责
2013/12/17 职场文书
工作决心书范文
2014/03/11 职场文书
社区健康教育实施方案
2014/03/18 职场文书
《梅花魂》教学反思
2014/04/30 职场文书
群众路线批评与自我批评发言稿
2014/10/16 职场文书
职代会闭幕词
2015/01/28 职场文书
小学班主任工作经验交流材料
2015/11/02 职场文书