vue-tree-color 树状结构图新增多个class

这篇博客介绍了如何在vue-tree-color组件中新增多个class,修复了相关问题,并详细讲解了实现过程。通过设置`judge`和`NodeClass`参数,实现了自定义节点样式的需求。

最近有兄台提交了一个issue,开始因为公司太忙时间实在转不过来,当然时间是挤出来的(好啦好啦,我就是为偷懒找借口而已)

然后自己看了看修复了一下,大家可以去看看

install

npm install vue-tree-color

注意:以前使用老版本的兄弟们更新一下

Import  Plugins

import Vue from 'vue'
import Vue2OrgTree from 'vue-tree-color'

Vue.use(Vue2OrgTree)

简单的一些起步工作我就不再讲了,可以看之前的文章

https://mp.weixin.qq.com/s/QLzXPxloTJh1fVAY77sZzg

修复

本次改动,主要是新增了一个参数NodeClass , 并且修改了judge的作用

judge

首先来看看 judge 变成什么样了吧

judge: {
    swtich: true 
}

judge 是一个Object格式 里面存在着一个值 {swtich:true || false}

不传或者传入false 都默认为不需要自定义class

NodeClass

新增NodeClass 参数 NodeClass 是一个Array格式 类似于Echartscolor 参数,

如果有放入你需要的class 如果没有则采取默认格式

 NodeClass:[
     "myred",
     "myger",
     "myblue"
 ]

data数据中需要的项中添加 你就需要这样做

{
   id: 0,
   label: "XXX科技有限公司",
   children: [
       {
           id: 2,
           label: "产品研发部",
   		   children: [
   		   {
   		      id: 5,
   		      label: "研发-前端",
   		      swtich: "myred"
   	           },
   	           {
   	              id: 6,
   	              label: "研发-后端",
                      swtich: "myger"
                },
                {
                  id: 9,
                  label: "UI设计",
                },
                {
                   id: 10,
                   label: "产品经理"
                }
             ]
      	},
        {
            id: 3,
            label: "销售部",
            children: [
            	{
                    id: 7,
                    label: "销售一部",
                    swtich: "myblue"
                 },
                 {
                    id: 8,
                    label: "销售二部",
                                   
                 }
             ]
          },
          {
          	id: 4,
           	label: "财务部"
           },
           {
            	id: 9,
                label: "HR人事"
           }
      ]
}

效果

继续深究

怎么实现的呢?爱钻研的童鞋们,问题又来了

在轮子中设定两个参数等待接收

judge: {
      type:Object,
      required:false
},
NodeClass:{
      type:Array,
      required:false
},

然后在js中接收这两个参数进行改造

DOM创建增加class的时候调用一个ChangeTheColor方法

domProps: {
	className:ChangeTheColor(data,judge,NodeClass)
},

执行ChangeTheColor方法

function ChangeTheColor(e,judge,NodeClass){
  // 随便判断一番
  if(judge!==""&&judge!==undefined&&judge!==null&&judge.swtich!==false){
    for(var k in judge) {
      // 获取这一项中的 swtich 值
      var a = (eval("e."+k))
      // 如果 NodeClass 存在
      if(NodeClass){
        // 循环判断
        for(let c =0 ;c<NodeClass.length;c++){
          // 如果当前项的 swtich 值等于 NodeClass 中某一项
          if( a === NodeClass[c])
            // 则添加改项 class
            return NodeClass[c]
          // 当到了最后一次循环 跳出
          else  if(NodeClass.length-1==c)
            return ""
        }
      }else{
        return ""
      }
    }
  }else{
    return ""
  }
}

最清晰的讲解,由最菜的严老湿来描绘

好啦!今天的严老湿小课堂就到这里了

有啥问题,疑问,或者更好的方式去实现 可以在 issue 提交噢

地址:https://github.com/CrazyMrYan/vue-tree-color

### Vue2 实现组织架构图 (Tree-Org) 在 Vue2 中实现组织架构图(tree-org),可以借助 `vue-org-tree` 这类专门用于展示树形结构数据的组件。以下是具体的实现方式以及注意事项。 #### 1. 安装依赖 首先,通过 npm 或 yarn 安装 `vue-org-tree` 组件: ```bash npm install vue-org-tree --save ``` 或者使用 yarn: ```bash yarn add vue-org-tree ``` --- #### 2. 基础配置与样式导入 为了确保组件能够正常渲染,需要引入其默认样式文件。如果未正确加载样式,则可能导致展开/收缩按钮无法显示[^3]。 ```html <style lang="less"> @import '~vue-org-tree/dist/style.css'; </style> ``` --- #### 3. 数据模型定义 `vue-org-tree` 的核心在于绑定的数据源。通常情况下,该数据是一个嵌套的对象数组,其中每个对象代表一个节点,并可能包含子节点列表 (`children`)。 以下是一个简单的示例数据模型: ```javascript export default { data() { return { treeData: [ { name: 'CEO', title: '首席执行官', children: [ { name: 'CTO', title: '技术总监', children: [ { name: 'Alice', title: '前端工程师' }, { name: 'Bob', title: '后端工程师' } ] }, { name: 'CFO', title: '财务总监' } ] } ], }; } }; ``` --- #### 4. 页面模板编写 在页面中引入并注册 `OrgChart` 组件,随后按照官方文档中的推荐方式进行绑定。 ```html <template> <div class="org-chart-container"> <org-chart :datasource="treeData"></org-chart> </div> </template> <script> import OrgChart from 'vue-org-tree'; export default { components: { OrgChart, }, data() { return { treeData: [ { name: 'CEO', title: '首席执行官', children: [ { name: 'CTO', title: '技术总监', children: [ { name: 'Alice', title: '前端工程师' }, { name: 'Bob', title: '后端工程师' } ] }, { name: 'CFO', title: '财务总监' } ] } ], }; } }; </script> <style scoped> .org-chart-container { width: 100%; height: auto; } </style> ``` 上述代码展示了如何利用 `vue-org-tree` 来构建一个基本的企业组织架构图。 --- #### 5. 自定义扩展功能 对于更复杂的需求,比如动态增删节点、自定义样式等,可以通过事件监听器或插槽机制完成。例如,在某个特定条件下隐藏部分节点,或者修改节点的颜色和形状。 ##### 动态更新数据 当用户交互触发某些操作时,可以直接修改绑定的数据源,从而实时反映到视图层。 ```javascript methods: { addChildNode(parentId) { const newNode = { name: 'New Member', title: '新成员' }; this.treeData.forEach((node) => { if (node.name === parentId && node.children) { node.children.push(newNode); } else if (node.children) { this._addChildRecursively(node.children, parentId, newNode); } }); }, _addChildRecursively(childrenArray, targetParentId, newNode) { for (const child of childrenArray) { if (child.name === targetParentId && child.children) { child.children.push(newNode); break; } else if (child.children) { this._addChildRecursively(child.children, targetParentId, newNode); } } } }, ``` 调用此方法即可向指定父级节点添加新的子节点。 --- #### 总结 以上内容介绍了如何在 Vue2 项目中集成 `vue-org-tree` 并创建企业组织架构图。需要注意的是,默认样式的调整可能会因实际需求而有所不同,因此建议开发者熟悉 Less 文件的内容以便进一步定制化设计。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值