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 相关文章推荐
Javascript &amp; DHTML 实例编程(教程)(三)初级实例篇1—上传文件控件实例
Jun 02 Javascript
jquery常用方法及使用示例汇总
Nov 08 Javascript
JS+CSS实现鼠标滑过时动态翻滚的导航条效果
Sep 24 Javascript
JS创建对象的写法示例
Nov 04 Javascript
jQuery根据ID、CLASS、等获取对象的实例
Dec 04 Javascript
js实现适配不同的屏幕大小
Apr 10 Javascript
p5.js入门教程和基本形状绘制
Mar 15 Javascript
D3.js(v3)+react 实现带坐标与比例尺的柱形图 (V3版本)
May 09 Javascript
JavaScript实现单英文金山打字通
Jul 24 Javascript
在Layui中实现开关按钮的效果实例
Sep 29 Javascript
jQuery轮播图功能制作方法详解
Dec 03 jQuery
详解vue-template-admin三级路由无法缓存的解决方案
Mar 10 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
php删除文件夹及其文件夹下所有文件的函数代码
2013/01/23 PHP
解析php函数method_exists()与is_callable()的区别
2013/06/21 PHP
PHP之autoload运行机制实例分析
2014/08/28 PHP
PHP实现定时执行任务的方法
2014/10/05 PHP
Yii的CDbCriteria查询条件用法实例
2014/12/04 PHP
基于jquery的实现简单的表格中增加或删除下一行
2010/08/01 Javascript
基于jquery的多功能软键盘插件
2012/07/25 Javascript
深入理解javaScript中的事件驱动
2013/05/21 Javascript
详解jQuery插件开发中的extend方法
2013/11/19 Javascript
JS之Date对象和获取系统当前时间详解
2014/01/13 Javascript
jquery动态分页效果堪比时光网
2014/09/25 Javascript
JavaScript 表单处理实现代码
2015/04/13 Javascript
深入理解JavaScript内置函数
2016/06/03 Javascript
Vue.js 2.0 和 React、Augular等其他前端框架大比拼
2016/10/08 Javascript
JS实现重新加载当前页面或者父页面的几种方法
2016/11/30 Javascript
JS 中LocalStorage和SessionStorage的使用
2017/08/17 Javascript
Vue服务器渲染Nuxt学习笔记
2018/01/31 Javascript
解决Vue-cli npm run build生产环境打包,本地不能打开的问题
2018/09/20 Javascript
对angularJs中自定义指令replace的属性详解
2018/10/09 Javascript
微信小程序获取用户信息并保存登录状态详解
2019/05/10 Javascript
vue中typescript装饰器的使用方法超实用教程
2019/06/17 Javascript
使用layui前端框架弹出form表单以及提交的示例
2019/10/25 Javascript
JavaScript随机数的组合问题案例分析
2020/05/16 Javascript
Element DateTimePicker日期时间选择器的使用示例
2020/07/27 Javascript
[45:59]EG vs OG 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/18 DOTA
Python爬取读者并制作成PDF
2015/03/10 Python
Python使用pickle模块储存对象操作示例
2018/08/15 Python
python TF-IDF算法实现文本关键词提取
2019/05/29 Python
pandas计算最大连续间隔的方法
2019/07/04 Python
python使用openpyxl操作excel的方法步骤
2020/05/28 Python
Python实现Excel自动分组合并单元格
2021/02/22 Python
python Protobuf定义消息类型知识点讲解
2021/03/02 Python
2014年党支部工作总结
2014/11/13 职场文书
2014年环保工作总结
2014/11/26 职场文书
党风廉政承诺书2016
2016/03/25 职场文书
毕业生自我鉴定范文
2019/05/13 职场文书