Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
<head>
    <title>jQuery orgChart</title>  
</head>
<body onload="blockFirstDelButton()">
  <h3 align="center">Org-Chart</h3>
    <div id="orgChartContainer">
      <div id="orgChart"></div>
    </div>
</body>
</html>
              
            
!

CSS

              
                #orgChart{
    width: auto;
    height: auto;
}

#orgChartContainer{
    width: auto;
    height: 580px;
    overflow: auto;
    background: #eeeeee;
}

.node{
        border-radius: 10px !important;
        background: #1794d4 !important;
        color: white;
        height: 80px !important;
        width: 160px !important;
        border: 2px solid black !important;
        font-family: sans-serif;
        font-size: smaller;
    }

    #jquery-script-menu {
position: fixed;
height: 90px;
width: 100%;
top: 0;
left: 0;
border-top: 5px solid #316594;
background: #fff;
-moz-box-shadow: 0 2px 3px 0px rgba(0, 0, 0, 0.16);
-webkit-box-shadow: 0 2px 3px 0px rgba(0, 0, 0, 0.16);
box-shadow: 0 2px 3px 0px rgba(0, 0, 0, 0.16);
z-index: 999999;
padding: 10px 0;
-webkit-box-sizing:content-box;
-moz-box-sizing:content-box;
box-sizing:content-box;
}

.jquery-script-center ul {
width: 212px;
float:left;
line-height:45px;
margin:0;
padding:0;
list-style:none;
}
.jquery-script-center a {
    text-decoration:none;
}

.jquery-script-clear {
clear:both;
height:0;
}

div.orgChart {
  margin : 10px;
  padding : 20px;
}

div.orgChart h2 {
  margin : 0px;
  font-size : 16px;
  font-weight: normal;
  min-height: 20px;
  display: block;
}

div.orgChart h3 {
  margin : 0px;
  font-size : 16px;
  font-weight: normal;
  min-height: 20px;
  display: block;
}

div.orgChart h2:hover {
  color: yellow;
  cursor: text;
  transform: scale(1.4);
}

div.orgChart h3:hover {
  color: yellow;
  cursor: text;
  transform: scale(1.6);
}

div.orgChart ul {
  list-style : none;
  margin : 4px;
  padding : 0px;
  font-size : 0.8em;
  text-align : left;
}

div.orgChart ul.stack,
div.orgChart ul.stack ul { text-align : center; }

div.orgChart table { width : 100%; }

div.orgChart tr.lines td.line {
  width : 1px;
  height : 20px;
}

div.orgChart tr.lines td.top { border-top : 2px solid black; }

div.orgChart tr.lines td.left { border-right : 2px solid black; }

div.orgChart tr.lines td.right { border-left : 2px soid black; }

div.orgChart tr.lines td.half { width : 50%; }

div.orgChart td {
  text-align : center;
  vertical-align : top;
  padding : 0px 2px;
}

div.orgChart div.node {
  cursor : default;
  border : 1px solid #e7e7e7;
  display : inline-block;
  padding : 5px;
  width : 96px;
  height : 60px;
  background: #ffffff; /* Old browsers */
  background: -moz-linear-gradient(top, #ffffff 0%, #fbfbfb 100%); /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #ffffff), color-stop(100%, #fbfbfb)); /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #ffffff 0%, #fbfbfb 100%); /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #ffffff 0%, #fbfbfb 100%); /* Opera 11.10+ */
  background: -ms-linear-gradient(top, #ffffff 0%, #fbfbfb 100%); /* IE10+ */
  background: linear-gradient(to bottom, #ffffff 0%, #fbfbfb 100%); /* W3C */
  line-height : 1.3em;
  border-radius : 4px;
  -moz-border-radius : 4px;
  -webkit-border-radius : 4px;
  position: relative;
  box-shadow: 1px 1px 0px #ddd;
}

.org-add-button,
.org-del-button,
.org-confirm-del-button {
  position: absolute;
  font-size: 12px;
}

.org-add-button {
  bottom: 3px;
  left: 5px;
  padding-left: 13px;
  background: url(http://imgh.us/add_2.png) no-repeat 0 3px;
}

.org-add-button:hover,
.org-del-button:hover {
  color: yellow;
  transform: scale(1.2);
  cursor: pointer;
}

.org-del-button {
  background:url(http://imgh.us/delete_4.png)   no-repeat;
  width: 12px;
  height: 12px;
  bottom: 7px;
  right: 18px;
}
.org-input { width: 90px;
background-color: white;
    color: black; }

.org-confirm-del-button { display: none; }
              
            
!

JS

              
                (function($) {
    $.fn.orgChart = function(options) {
        var opts = $.extend({}, $.fn.orgChart.defaults, options);
        return new OrgChart($(this), opts);        
    }

    $.fn.orgChart.defaults = {
        data: [{id:1, name:'Root', title:'Amount', parent: 0, hierarchy: 0}],
        showControls: false,
        allowEdit: false,
        onAddNode: null,
        onDeleteNode: null,
        onClickNode: null,
        newNodeText: 'Add',
        delNodeText: 'del'
    };

    function OrgChart($container, opts){
        var data = opts.data;
        var nodes = {};
        var rootNodes = [];
        this.opts = opts;
        this.$container = $container;
        var self = this;

        this.draw = function(){
            $container.empty().append(rootNodes[0].render(opts));
     document.getElementsByClassName('org-del-button')[0].style.display = 'none';
          $container.find('.node').click(function(){
                if(self.opts.onClickNode !== null){
                    self.opts.onClickNode(nodes[$(this).attr('node-id')]);
                }
            });

            if(opts.allowEdit){
                $container.find('.node h2').click(function(e){
                    var thisId = $(this).parent().attr('node-id');
                    self.startEdit(thisId);
                    e.stopPropagation();
                });
                $container.find('.node h3').click(function(e){
                    var thisId = $(this).parent().attr('node-id');
                    self.startEdith3(thisId);
                    e.stopPropagation();
                });
            }

            // add "add button" listener
            $container.find('.org-add-button').click(function(e){
                var thisId = $(this).parent().attr('node-id');

                if(self.opts.onAddNode !== null){
                    self.opts.onAddNode(nodes[thisId]);
                }
                else{
                    self.newNode(thisId);
                }
                e.stopPropagation();
            });

            $container.find('.org-del-button').click(function(e){
                var thisId = $(this).parent().attr('node-id');

                if(self.opts.onDeleteNode !== null){
                    self.opts.onDeleteNode(nodes[thisId]);
                }
                else{
                    self.deleteNode(thisId);
                }
                e.stopPropagation();
            });
        }

        this.startEdit = function(id){
            var inputElement = $('<input class="org-input" type="text" value="'+nodes[id].data.name+'"/>');
            $container.find('div[node-id='+id+'] h2').replaceWith(inputElement);
            var commitChange = function(){
                var h2Element = $('<h2>'+nodes[id].data.name+'</h2>');
                if(opts.allowEdit){
                    h2Element.click(function(){
                        self.startEdit(id);
                    })
                }
                inputElement.replaceWith(h2Element);
            }  
            inputElement.focus();
            inputElement.keyup(function(event){
                if(event.which == 13){
                    commitChange();
                }
                else{
                    nodes[id].data.name = inputElement.val();
                }
            });
            inputElement.blur(function(event){
                commitChange();
            })
        }

        //////////////////////title field//////////////////////////////
                this.startEdith3 = function(id){
            var inputElement = $('<input class="org-input" type="text" value="'+nodes[id].data.title+'"/>');
            $container.find('div[node-id='+id+'] h3').replaceWith(inputElement);
            var commitChange = function(){
                var h3Element = $('<h3>'+nodes[id].data.title+'</h3>');
                if(opts.allowEdit){
                    h3Element.click(function(){
                        self.startEdith3(id);
                    })
                }
                inputElement.replaceWith(h3Element);
            }  
            inputElement.focus();
            inputElement.keyup(function(event){
                if(event.which == 13){
                    commitChange();
                }
                else{
                    nodes[id].data.title = inputElement.val();
                }
            });
            inputElement.blur(function(event){
                commitChange();
            })
        }
        //////////////////////title field ends////////////////////////
        this.newNode = function(parentId){
            var nextId = Object.keys(nodes).length;
            while(nextId in nodes){
                nextId++;
            }

            self.addNode({id: nextId, name: '(New name)', title:'0.0', hierarchy: parentId, parent: parentId});
        }

        this.addNode = function(data){
            var newNode = new Node(data);
            nodes[data.id] = newNode;
            nodes[data.parent].addChild(newNode);
            self.draw();
            self.startEdit(data.id);
            self.startEdith3(data.id);
        }

        this.deleteNode = function(id){
            for(var i=0;i<nodes[id].children.length;i++){
                self.deleteNode(nodes[id].children[i].data.id);
            }
            nodes[nodes[id].data.parent].removeChild(id);
            delete nodes[id];
            self.draw();
        }

        this.getData = function(){
            var outData = [];
            for(var i in nodes){
                outData.push(nodes[i].data);
            }
            return outData;
        }

        // constructor
        for(var i in data){
            var node = new Node(data[i]);
            nodes[data[i].id] = node;
        }

        // generate parent child tree
        for(var i in nodes){
            if(nodes[i].data.parent == 0){
                rootNodes.push(nodes[i]);
            }
            else{
                nodes[nodes[i].data.parent].addChild(nodes[i]);
            }
        }

        // draw org chart
        $container.addClass('orgChart');
        self.draw();
    }

    function Node(data){
        this.data = data;
        this.children = [];
        var self = this;

        this.addChild = function(childNode){
            this.children.push(childNode);
        }

        this.removeChild = function(id){
            for(var i=0;i<self.children.length;i++){
                if(self.children[i].data.id == id){
                    self.children.splice(i,1);
                    return;
                }
            }
        }

        this.render = function(opts){
            var childLength = self.children.length,
                mainTable;

            mainTable = "<table cellpadding='0' cellspacing='0' border='0'>";
            var nodeColspan = childLength>0?2*childLength:2;
            mainTable += "<tr><td colspan='"+nodeColspan+"'>"+self.formatNode(opts)+"</td></tr>";

            if(childLength > 0){
                var downLineTable = "<table cellpadding='0' cellspacing='0' border='0'><tr class='lines x'><td class='line left half'></td><td class='line right half'></td></table>";
                mainTable += "<tr class='lines'><td colspan='"+childLength*2+"'>"+downLineTable+'</td></tr>';

                var linesCols = '';
                for(var i=0;i<childLength;i++){
                    if(childLength==1){
                        linesCols += "<td class='line left half'></td>";    // keep vertical lines aligned if there's only 1 child
                    }
                    else if(i==0){
                        linesCols += "<td class='line left'></td>";     // the first cell doesn't have a line in the top
                    }
                    else{
                        linesCols += "<td class='line left top'></td>";
                    }

                    if(childLength==1){
                        linesCols += "<td class='line right half'></td>";
                    }
                    else if(i==childLength-1){
                        linesCols += "<td class='line right'></td>";
                    }
                    else{
                        linesCols += "<td class='line right top'></td>";
                    }
                }
                mainTable += "<tr class='lines v'>"+linesCols+"</tr>";

                mainTable += "<tr>";
                for(var i in self.children){
                    mainTable += "<td colspan='2'>"+self.children[i].render(opts)+"</td>";
                }
                mainTable += "</tr>";
            }
            mainTable += '</table>';
            return mainTable;
        }

        this.formatNode = function(opts){
            var nameString = '',
                descString = '';
                titleString = '';
            if(typeof data.name !== 'undefined'){
                nameString = '<h2>'+self.data.name+'</h2>';
            }
            if(typeof data.title !== 'undefined'){
                titleString = '<h3>'+self.data.title+'</h3>';
            }
            if(typeof data.description !== 'undefined'){
                descString = '<p>'+self.data.description+'</p>';
            }
            if(opts.showControls){
                var buttonsHtml = "<div class='org-add-button'>"+opts.newNodeText+"</div><div class='org-del-button'>"+opts.delNodeText+"</div>";
            }
            else{
                buttonsHtml = '';
            }
            return "<div class='node' node-id='"+this.data.id+"'>"+nameString+titleString+descString+buttonsHtml+"</div>";
        }
    }

})(jQuery);

///////////////////////////////////////
var testData = [
        {id: 1, name: 'Name', title: 'Amount', parent: 0},
        {id: 2, name: 'Name', title: 'Amount', parent: 1},
        {id: 3, name: 'Name', title: 'Amount', parent: 1},
        {id: 4, name: 'Name', title: 'Amount', parent: 1},
        {id: 6, name: 'Name', title: 'Amount', parent: 1},
        {id: 7, name: 'Name', title: 'Amount', parent: 1},
        {id: 8, name: 'Name', title: 'Amount', parent: 1},
        {id: 5, name: 'Name', title: 'Amount', parent: 3},
        
    ];
    $(function(){
        org_chart = $('#orgChart').orgChart({
            data: testData,
            showControls: true,
            allowEdit: true,
            onAddNode: function(node){ 
                log('Created new node on node '+node.data.id);
                org_chart.newNode(node.data.id); 
            },
            onDeleteNode: function(node){
                log('Deleted node '+node.data.id);
                org_chart.deleteNode(node.data.id); 
            },
            onClickNode: function(node){
                log('Clicked node '+node.data.id);
            }

        });
    });

    // just for example purpose
    function log(text){
        $('#consoleOutput').append('<p>'+text+'</p>')
    }

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-36251023-1']);
  _gaq.push(['_setDomainName', 'jqueryscript.net']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
              
            
!
999px

Console