Vue树形架构插件vue-org-tree

Vue树形架构插件vue-org-tree

注:2019-8-21在从心_205a书友的帮助下解决了bug:节点无法展开问题

底下这个函数一定要写两个参数,如果只写onExpand(data),data.expand结果是undefined
这个应该是插件的bug,一般只写一个参数也是可以
onExpand(e,data) {
      if ("expand" in data) {
        data.expand = !data.expand;
        if (!data.expand && data.children) {
          this.collapse(data.children);
        }
      } else {
        this.$set(data, "expand", true);
      }
    }

注:2019-8-21解决了bug:点击选项要跳转链接,点击展开收缩按钮也会跳转

  增加判断语句即可,默认展开收缩按钮对象里的url肯定是空
  onNodeClick(e, data) {
       //console.log(data.label);
       if(data.url==null){
        return false
      }else{
        window.open(data.url)
      }
    },

文档传送门https://github.com/hukaibaihu/vue-org-tree

使用步骤(这里以在vue-cli3的项目为例)

Ⅰ.安装:这边安装一定要加-D
cnpm install -D less-loader less
cnpm install -D vue2-org-tree

Ⅱ.在main.js引入注册
import Vue2OrgTree from 'vue2-org-tree';
Vue.use(Vue2OrgTree)

Ⅲ.在router.js中注册路由tree.vue
import tree from '../components/tree.vue';

routes增加配置

Ⅳ.在tree.vue里写页面

//示例代码
<template>
  <div id="tree">
    <div class="container">
      <div class="col-md-10 col-md-offset-1">
        <div class="page-header">
          <h3>招行度量指标体系</h3>
        </div>
        <div class="row">
          <div class="col-md-8 col-md-offset-2">
            <form class="form-horizontal row">
              <div class="col-md-4">
                <div class="checkbox">
                  <label>
                    <input type="checkbox" v-model="horizontal" /> 横排
                  </label>
                </div>
              </div>
              <div class="col-md-4">
                <div class="checkbox">
                  <label>
                    <input type="checkbox" v-model="collapsable" /> 竖排
                  </label>
                </div>
              </div>
              <div class="col-md-4">
                <div class="checkbox">
                  <label>
                    <input type="checkbox" v-model="expandAll" @change="expandChange" /> 显示所有项
                  </label>
                </div>
              </div>
              <p>
                <br />
              </p>
              <p>
                <br />
              </p>
              <div class="col-md-6">
                <div class="form-group">
                  <label class="control-label col-md-5">背景色选择:</label>
                  <div class="col-md-7">
                    <select class="form-control" v-model="labelClassName">
                      <option value="bg-white">bg-white</option>
                      <option value="bg-orange">bg-orange</option>
                      <option value="bg-gold">bg-gold</option>
                      <option value="bg-gray">bg-gray</option>
                      <option value="bg-lightpink">bg-lightpink</option>
                      <option value="bg-chocolate">bg-chocolate</option>
                      <option value="bg-tomato">bg-tomato</option>
                    </select>
                  </div>
                </div>
              </div>
            </form>
          </div>
        </div>
        <p>
          <br />
        </p>
        <div class="text-center">
          <vue2-org-tree
            name="test"
            :data="data"
            :horizontal="horizontal"
            :collapsable="collapsable"
            :label-class-name="labelClassName"
            :render-content="renderContent"
            @on-expand="onExpand"
            @on-node-click="onNodeClick"
          />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      data: {
        id: 0,
        label: "度量指标体系",
        children: [
          {
            id: 1,
            label: "交付质量",
            children: [
              {
                id: 11,
                label: "生产质量",
                children: [
                  {
                    id: 111,
                    label: "生产事件"
                  }
                ]
              },
              {
                id: 12,
                label: "开发质量",
                children: [
                  {
                    id: 121,
                    label: "项目开发缺陷密度"
                  }
                ]
              },
              {
                id: 13,
                label: "发布质量",
                children: [
                  {
                    id: 131,
                    label: "上线失败率"
                  }
                ]
              },
              {
                id: 14,
                label: "过程质量",
                children: [
                  {
                    id: 141,
                    label: "项目过程符合度"
                  }
                ]
              }
            ]
          },
          {
            id: 2,
            label: "交付效能",
            children: [
              {
                id: 21,
                label: "交付能力",
                children: [
                  {
                    id: 211,
                    label: "功能点产出"
                  },
                  {
                    id: 212,
                    label: "代码行产出"
                  },
                  {
                    id: 213,
                    label: "上线投产次数"
                  },
                  {
                    id: 214,
                    label: "接收需求数"
                  },
                  {
                    id: 215,
                    label: "立项数"
                  },
                  {
                    id: 216,
                    label: "结项数"
                  },
                  {
                    id: 217,
                    label: "迭代速率"
                  },
                  {
                    id: 218,
                    label: "迭代完成率"
                  }
                ]
              },
              {
                id: 22,
                label: "交付效率",
                children: [
                  {
                    id: 221,
                    label: "人均产出功能点"
                  },
                  {
                    id: 222,
                    label: "项目生产率"
                  }
                ]
              }
            ]
          },
          {
            id: 3,
            label: "业务满意度",
            children: [
              {
                id: 31,
                label: "业务满意度"
              }
            ]
          },
          {
            id: 4,
            label: "交付价值",
            children: [
              {
                id: 41,
                label: "业务价值关键指标"
              }
            ]
          },
          {
            id: 5,
            label: "交付速度",
            children: [
              {
                id: 51,
                label: "需求评估速度",
                children: [
                  {
                    id: 511,
                    label: "需求响应度"
                  }
                ]
              },
              {
                id: 52,
                label: "开发速度",
                children: [
                  {
                    id: 521,
                    label: "完成度天数"
                  },
                  {
                    id: 522,
                    label: "完成度达标率"
                  },
                  {
                    id: 523,
                    label: "故事平均测试通过周期"
                  }
                ]
              },
              {
                id: 53,
                label: "发布速度",
                children: [
                  {
                    id: 531,
                    label: "首次交付天数"
                  },
                  {
                    id: 532,
                    label: "首次交付达标率"
                  },
                  {
                    id: 533,
                    label: "故事平均发布周期"
                  }
                ]
              },
              {
                id: 54,
                label: "燃尽图"
              },
              {
                id: 55,
                label: "一次测试通过率"
              }
            ]
          },
          {
            id: 6,
            label: "持续交付",
            children: [
              {
                id: 61,
                label: "技术债务率",
                url: "https://world.taobao.com/"
              },
              {
                id: 62,
                label: "DEVOPS成熟度",
                url: " http://www.baidu.com"
              },
              {
                id: 63,
                label: "DEVOPS健康度",
                url: " https://www.google.com/"
              }
            ]
          }
        ]
      },
      horizontal: true,
      collapsable: true,
      expandAll: false,
      labelClassName: "bg-white"
    };
  },
  methods: {
    renderContent(h, data) {
      return data.label;
    },
    onExpand(e, data) {
      if ("expand" in data) {
        data.expand = !data.expand;
        if (!data.expand && data.children) {
          this.collapse(data.children);
        }
      } else {
        this.$set(data, "expand", true);
      }
    },
    //点击选项执行的方法,可以用于跳转到其他链接,注意一定要写协议头
    onNodeClick(e, data) {
       //console.log(data.label);
       if(data.url==null){
        return false
      }else{
        window.open(data.url)
      }
    },
    collapse(list) {
      var _this = this;
      list.forEach(function(child) {
        if (child.expand) {
          child.expand = false;
        }
        child.children && _this.collapse(child.children);
      });
    },
    expandChange() {
      this.toggleExpand(this.data, this.expandAll);
    },
    toggleExpand(data, val) {
      var _this = this;
      if (Array.isArray(data)) {
        data.forEach(function(item) {
          _this.$set(item, "expand", val);
          if (item.children) {
            _this.toggleExpand(item.children, val);
          }
        });
      } else {
        this.$set(data, "expand", val);
        if (data.children) {
          _this.toggleExpand(data.children, val);
        }
      }
    }
  }
};
</script>
<style type="text/css">
.org-tree-node-label {
  white-space: nowrap;
}
.bg-white {
  background-color: white;
}
.bg-orange {
  background-color: orange;
}
.bg-gold {
  background-color: gold;
}
.bg-gray {
  background-color: gray;
}
.bg-lightpink {
  background-color: lightpink;
}
.bg-chocolate {
  background-color: chocolate;
}
.bg-tomato {
  background-color: tomato;
}
</style>



Ⅴ.效果图

本文同步发表在我的个人博客:https://www.lubaojun.com/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 228,786评论 6 534
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 98,656评论 3 419
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 176,697评论 0 379
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 63,098评论 1 314
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 71,855评论 6 410
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 55,254评论 1 324
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 43,322评论 3 442
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 42,473评论 0 289
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 49,014评论 1 335
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,833评论 3 355
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 43,016评论 1 371
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,568评论 5 362
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,273评论 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,680评论 0 26
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,946评论 1 288
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,730评论 3 393
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 48,006评论 2 374

推荐阅读更多精彩内容

  • 基于Vue的一些资料 内容 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 element★...
    尝了又尝阅读 1,165评论 0 1
  • 第一节 vue:读音: v-u-eview vue和angular区别?vue——简单、易学指令以 v-xxx一片...
    黑色的五叶草阅读 1,137评论 0 1
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    小姜先森o0O阅读 9,575评论 0 72
  • Vue 学习笔记1(01-39) 学习地址:https://ke.qq.com/course/279700 目录:...
    Allenem阅读 467评论 0 0
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    你猜_3214阅读 11,106评论 0 118