UNPKG

orgchart

Version:

Simple and direct organization chart(tree-like hierarchy) library based on pure DOM.

90 lines (86 loc) 3.02 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>OrgChart Expand to Given Depth Demo</title> <link rel="icon" href="img/logo.png"> <link rel="stylesheet" href="css/orgchart.css"> <link rel="stylesheet" href="css/style.css"> <style type="text/css"> .orgchart { background: white; } </style> </head> <body> <section class="demo-notes"> <h1>Expand to Given Depth Demo</h1> <p>This demo expands descendants from a selected node down to a specified depth instead of fully opening the branch.</p> </section> <div id="chart-container"></div> <script type="text/javascript" src="js/orgchart.js"></script> <script type="text/javascript"> document.addEventListener('DOMContentLoaded', () => { const datasource = { 'name': 'Lao Lao', 'title': 'general manager', 'children': [ { 'name': 'Bo Miao', 'title': 'department manager' }, { 'name': 'Su Miao', 'title': 'department manager', 'children': [ { 'name': 'Tie Hua', 'title': 'senior engineer' }, { 'name': 'Hei Hei', 'title': 'senior engineer', 'children': [ { 'name': 'Pang Pang', 'title': 'engineer' }, { 'name': 'Dan Zai', 'title': 'UE engineer', 'children': [ { 'name': 'Er Dan Zai', 'title': 'engineer', 'children': [ { 'name': 'AAA', 'title': 'Intern' }, { 'name': 'BBB', 'title': 'Intern' } ] } ] } ] } ] }, { 'name': 'Hong Miao', 'title': 'department manager' }, { 'name': 'Chun Miao', 'title': 'department manager' } ] }; const showDescendents = function(node, visibleLevel) { if (visibleLevel <= 1) { return false; } const hierarchy = node.closest('.hierarchy'); const childrenLayer = hierarchy ? hierarchy.querySelector(':scope > .nodes') : null; if (!childrenLayer) { return false; } childrenLayer.classList.remove('hidden'); hierarchy.classList.remove('isChildrenCollapsed'); Array.from(childrenLayer.children).forEach((childHierarchy) => { const childNode = childHierarchy.querySelector(':scope > .node'); if (childNode) { childNode.classList.remove('slide-up'); showDescendents(childNode, visibleLevel - 1); } }); }; const oc = new OrgChart({ chartContainer: '#chart-container', 'visibleLevel': 2, 'data' : datasource, 'nodeContent': 'title', 'createNode': function(node) { node.addEventListener('click', (event) => { if (event.target.matches('.bottomEdge.oci-chevron-down, .toggleBtn.oci-plus-square, .toggleBtn.oci-chevron-down')) { showDescendents(node, 3); } }); } }); }); </script> </body> </html>