jquery.orgchart.js 简单demo,自定义内容,自定义事件

在这里插入图片描述

orgchart简单demo

GIT地址: https://github.com/Vlovely/orgchartDemo.git
预览图

在这里插入图片描述

代码示例
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Organization Chart Plugin</title>
  <link rel="icon" href="img/logo.png">
  <link rel="stylesheet" href="css/jquery.orgchart.css">
  <link rel="stylesheet" href="css/style.css">
  <link rel="stylesheet" href="font/iconfont.css">
  <style>
    /* 取消默认箭头 不标准。官网给出的用js修改class类名方法*/
    .edge{display: none;}
  </style>
</head>
<body>
  <div id="chart-container"></div>

  <script type="text/javascript" src="font/iconfont.js"></script>
  <script type="text/javascript" src="js/jquery.min.js"></script>
  <script type="text/javascript" src="js/jquery.orgchart.js"></script>
  <script type="text/javascript" src="js/jquery.mockjax.min.js"></script>
  <script type="text/javascript">
  $(function() {
    // 模拟请求数据
    $.mockjax({
      url: '/orgchart/children',
      contentType: 'application/json',
      responseTime: 1000,
      responseText: { 'children': [
        { 'id': 'id6', 'title': '二2-三1', 'type': '2' },
        { 'id': 'id7', 'title': '二2-三2', 'type': '2' }
      ]}
    });
    // 默认数据
    var datascource = {
      'id': 'id1',
      'title': '一',
      'type': '1',
      'relationship': '001', 
      //第一个字符代表当前节点是否具有父节点;
      //第二个字符代表当前节点有同级节点;
      //第三个字符代表当前节点是否有子节点。
      'children': [
        {'id': 'id2', 'title': '二1', 'type': '2' , 'children': [
          {
            'id': 'id3',
            'title': '二1-三1',
            'type': '3'
          }
        ]},
        {'id': 'id4', 'title': '二2', 'type': '2' },
        {'id': 'id5', 'title': '二3', 'type': '2' },
      ]
    };
    // HTML模板
    var nodeTemplate = function(data) {
      let iconName
      if (data.children && data.children.length > 0) {
        iconName = 'icon-suoxiao'
      } else {
        iconName = 'icon-fangda'
      }
      return `
      <img src="./img/avatar/${data.type}.jpg" style="width:100%" title="${data.title}"/>
      <span>${data.title}</span>
      <i class="iconfont ${iconName}" style="width:14px;height:14px;top:-4px;left:-4px;position:absolute;"></i>
      `;
    };
    // 初始化
    const oc = $('#chart-container').orgchart({
      'data' : datascource,
      'visibleLevel': '3', // 它表示在最开始的orgchart扩展到的级别。
      'nodeTemplate': nodeTemplate,
      'nodeId': 'id'
    });
    // 绑定点击事件
    $('#chart-container').delegate('.icon-suoxiao', 'click', function() {
      $(this).removeClass('icon-suoxiao').addClass('icon-fangda');
      $(this).parent().parent().parent().siblings().hide()
    })
    $('#chart-container').delegate('.icon-fangda', 'click', function() {
      if ($(this).parent().parent().parent().siblings().length > 0) {
        $(this).parent().parent().parent().siblings().show()
        $(this).removeClass('icon-fangda').addClass('icon-suoxiao');
      } else if ($(this).parent().attr('id') === 'id4') {
        let that = this
        $.post("/orgchart/children", function(res) {
          oc.addChildren($(that).parent(), res.children)
          $(that).removeClass('icon-fangda').addClass('icon-suoxiao');
        });
      }
    })
  });
  </script>
  </body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值