jQuery 水平层次树状视图插件 hortree

  • 源码大小:52.01KB
  • 所需积分:1积分
  • 源码编号:19JP-3761
  • 浏览次数:865次
  • 最后更新:2023年07月20日
  • 所属栏目:其他
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

hortree是一个小而简单的jQuery插件,它有助于从JSON文件中指定的层次数据中呈现类似水平树视图的图表。

参见:

  • JavaScript和纯CSS中的10个最佳树状视图插件
  • JavaScript或纯CSS中的7个最佳组织结构图生成器

如何使用它:

1.创建一个容器元素来放置水平层次树。

  1. <div id="my-container"></div>

2.包含jQuery库和JavaScript文件jquery.hortree.min.js查询在网页底部。

  1. <script src="//code.jquery.com/jquery.min.js"></script>
  2. <script src="jquery.hortree.min.js"></script>

3.如果您想在父节点和子节点之间画一条线,请包含jQuery Line插件。

  1. <script src="jquery.line.js"></script>

4.在JSON文件中定义分层数据。

  1. [
  2. {
  3. "description": "Element ROOT",
  4. "children": [
  5. {
  6. "description": "Element 1",
  7. "children": [
  8. {
  9. "description": "Element 1.1",
  10. "children": []
  11. },
  12. {
  13. "description": "Element 1.2",
  14. "children": [
  15. {
  16. "description": "Element 1.2.1",
  17. "children": []
  18. }
  19. ]
  20. }
  21. ]
  22. },
  23. {
  24. "description": "Element 2",
  25. "children": [
  26. {
  27. "description": "Element 2.1",
  28. "children": []
  29. },
  30. {
  31. "description": "Element 2.2",
  32. "children": []
  33. }
  34. ]
  35. },
  36. {
  37. "description": "Element 3",
  38. "children": [
  39. {
  40. "description": "Element 3.1",
  41. "children": []
  42. },
  43. {
  44. "description": "Element 3.2",
  45. "children": []
  46. },
  47. {
  48. "description": "Element 3.3",
  49. "children": [
  50. {
  51. "description": "Element 3.3.1",
  52. "children": []
  53. },
  54. {
  55. "description": "Element 3.3.2",
  56. "children": []
  57. }
  58. ]
  59. }
  60. ]
  61. }
  62. ]
  63. }
  64. ]

5.在您刚刚创建的容器中渲染默认层次结构树的JavaScript。

  1. $.getJSON('data.json', function (jsonData) {
  2. $('#my-container').hortree({
  3. data: jsonData
  4. });
  5. });

6.将您自己的CSS样式应用于层次结构树。

  1. .hortree-wrapper *, .hortree-wrapper *:before, .hortree-wrapper *:after {
  2. -webkit-box-sizing: border-box;
  3. -moz-box-sizing: border-box;
  4. box-sizing: border-box;
  5. }
  6.  
  7. .hortree-wrapper {
  8. position: relative;
  9. font-family: sans-serif, Verdana, Arial;
  10. font-size: 0.9em;
  11. }
  12.  
  13. .hortree-branch {
  14. position: relative;
  15. margin-left: 180px;
  16. }
  17.  
  18. .hortree-branch:first-child { margin-left: 0; }
  19.  
  20. .hortree-entry {
  21. position: relative;
  22. margin-bottom: 50px;
  23. }
  24.  
  25. .hortree-label {
  26. display: block;
  27. width: 150px;
  28. padding: 2px 5px;
  29. line-height: 30px;
  30. text-align: center;
  31. border: 2px solid #4b86b7;
  32. border-radius: 3px;
  33. position: absolute;
  34. left: 0;
  35. z-index: 10;
  36. background: #fff;
  37. }

7.或直接包括jquery.hortree.css查询头部部分。

  1. <link rel="stylesheet" href="jquery.hortree.css">

8.默认插件选项。

  1. $('#my-container').hortree({
  2. lineStrokeWidth: 2,
  3. lineZindex: 8,
  4. lineColor: '#4b86b7',
  5. data: [],
  6. onComplete: function () {
  7. // onComplete callback
  8. }
  9. });

更新日志:

2022-01-04

  • v1.0.2版本

2017-05-02

  • 更新了getPos函数

2017-03-23

  • 添加了工具提示管理

2017-03-18

  • 删除了一个空的测试函数

预览截图