Coder Social home page Coder Social logo

jorgchart's Introduction

Readme

Overview

Follow me @wesnolte

jQuery OrgChart is a plugin that allows you to render structures with nested elements in a easy-to-read tree structure. To build the tree all you need is to make a single line call to the plugin and supply the HTML element Id for a nested unordered list element that is representative of the data you'd like to display. If drag-and-drop is enabled you'll be able to reorder the tree which will also change the underlying list structure.

Features include:

  • Very easy to use given a nested unordered list element.
  • Drag-and-drop functionality allows reordering of the tree and underlying <ul> structure.
  • Showing/hiding a particular branch of the tree by clicking on the respective node.
  • Nodes can contain any amount of HTML except <li> and <ul>.
  • Easy to style.
  • You can specify that sub-trees should start collapsed, which is useful for very large trees

jQuery OrgChart


Expected Markup & Example Usage

To get up and running you'll need a few things.


The JavaScript Libraries & CSS

You need to include the jQuery as well as the jOrgChart libraries. For example:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript" src="jquery.jOrgChart.js"></script>

If you want to use the drag-and-drop functionality you'll need to include jQuery UI too:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>

The core CSS is necessary to perform some of the basic styling i.e.

<link rel="stylesheet" href="css/jquery.jOrgChart.css"/>

The HTML

You'll need to construct a nest unordered list that represents your node nesting. For example:

<ul id="org" style="display:none">
<li>
  Food
  <ul>
    <li>Beer</li>
    <li>Vegetables
      <ul>
        <li>Pumpkin</li>
        <li><a href="http://tquila.com" target="_blank">Aubergine</a></li>
      </ul>
    </li>
    <li>Bread</li>
    <li>Chocolate
      <ul>
        <li>Topdeck</li>
        <li>Reese's Cups</li>
      </ul>
    </li>
  </ul>
</li>
</ul>

If you want a sub-tree to start off hidden, just add class="collapsed" to a list item (<li>). That list item will appear, but everything below it won't. For example:

<ul id="org" style="display:none">
  <li>Food:
    <ul>
      <li>Beer</li>
      <li class=collapsed>Vegetables
        <ul>
          <li>Carrot</li>
          <li>Pea</li>
        </ul>
      </li>
      <li>Chocolate</li>
    </ul>
  </li>
</ul>

This plugin works by generating the tree as a series of nested tables. Each node in the tree is represented with <div class="node">. You can include any amount of HTML markup in your <li> except for other <ul> or <li> elements. Your markup will be used within the node's <div> element. Any classes you attach to the <li> elements will be copied to the associated node, allowing you to highlight particular parts of the tree. The special collapsed class described above doesn't get copied to the node.


The jQuery Call

And the cherry on the top is the usual call, often but not always on document load. You'll need to specify the Id of the list in this call. For example:

jQuery(document).ready(function() {
    $("#org").jOrgChart();
});

This call will append the markup for the OrgChart to the <body> element by default, but you can specify this as part of the options.


Demo

You can view a demo of this here.


Source Code

Source code with an example is available here.


Configuration

There are only 3 configuration options:

  1. chartElement - used to specify which HTML element you'd like to append the OrgChart markup to. [default='body']
  2. depth - tells the code what depth to parse to. The default value of "-1" instructs it to parse like it's 1999. [default=-1]
  3. chartClass - the name of the style class that is assigned to the generated markup. [default='jOrgChart']
  4. dragAndDrop - determines whether the drag-and-drop feature of tree node elements is enabled. [default=false]

jorgchart's People

Contributors

ahzgdn avatar fschaefer avatar neilb avatar wesnolte avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jorgchart's Issues

Dragdrop doesn't work with latest jquery ui

When using jquery 1.8.3 and jqueryui 1.9.2 you'll get this error when you try to drag:

Uncaught Error: cannot call methods on droppable prior to initialization; attempted to call method 'disable'

Drop callback needed

The simplest way to support all the requests to be able to save modified charts, is to provide a callback function as one of the parameters that gets called when the drop is complete. I tried to add it but ran into an issue that is beyond me. I added the following at the end of the // Drop event handler for nodes section:

// handle callback after drop
if (opts.dropHandler) {
    var a = opts.dropHandler;
    window[a](this);
}

where dropHandler is the new options I added. This works to call the function, but it happens too quickly because the new node positions aren't all drawn yet and if I loop through $('.node').each() ... it finds one more than necessary (the original nodes plus the new temporary dragged node). If someone can tell me how to ensure that the drop and subsequent update of the div is complete before calling my function, I'd appreciate it.

Code licensed under Creative Commons

Hi,

I'm interested in using your jQuery plugin for a project, but that project is going to be under the GPL, which is incompatible with the CC-BY-SA license. The same purpose would be accomplished by both, but CC-BY-SA is not meant to be used for code at all. Can you, at the very least, dual-license the code? Failing that, could you give me license to use the code under the GPL?

Thanks!

Redrawing on collapse

Hi

I'm not much of a programmer, but it would be very helpfull if the graph were to be redrawn on collapse, in order to make a better use of the available space on the container.

For example, my initial tree is very wide, but if the user collapses a few nodes the diagram should redraw to use less space

Thanks!

Show / Hide Branches that are not in use

It's possible to Show/Hide branches that are not in use?

For example, I have 3 main branches, but if the user clicks in the 2nd branch the 1st and 3rd branches will hide allowing the user only can see the nodes of the 2nd branch.

How can I do that?

Thanks for you help

How do i will render the OrgChart from dynamic array data

I get organization data from database and save to an array like below:

Array ( 
[0] => Array ( [hierarchy_id] => 1 [layer] => 1 [parent_id] => [manage_flag] => 1 [pschool_id] => 1 [pschool_name] => Organization ) 
[1] => Array ( [hierarchy_id] => 4 [layer] => 2 [parent_id] => 1 [manage_flag] => 1 [pschool_id] => 9 [pschool_name] => Headerquarter ) 
[2] => Array ( [hierarchy_id] => 6 [layer] => 3 [parent_id] => 4 [manage_flag] => 1 [pschool_id] => 11 [pschool_name] => Kantou branch ) 
[3] => Array ( [hierarchy_id] => 7 [layer] => 3 [parent_id] => 4 [manage_flag] => 1 [pschool_id] => 12 [pschool_name] => Kansai branch ) 
[4] => Array ( [hierarchy_id] => 8 [layer] => 3 [parent_id] => 4 [manage_flag] => 1 [pschool_id] => 13 [pschool_name] => Kyushu branch ) 
[5] => Array ( [hierarchy_id] => 9 [layer] => 4 [parent_id] => 7 [manage_flag] => 1 [pschool_id] => 14 [pschool_name] => Osaka branch ) 
[6] => Array ( [hierarchy_id] => 10 [layer] => 4 [parent_id] => 7 [manage_flag] => 1 [pschool_id] => 15 [pschool_name] => Kyoto branch ) 
[7] => Array ( [hierarchy_id] => 12 [layer] => 5 [parent_id] => 9 [manage_flag] => 0 [pschool_id] => 17 [pschool_name] => osaka shop 1 ) 
[8] => Array ( [hierarchy_id] => 11 [layer] => 5 [parent_id] => 9 [manage_flag] => 0 [pschool_id] => 16 [pschool_name] => Osaka shop 2 ) 
)

How can i render OrgChart from hierarchy data like that?

Responsive OrgChart

Hi,

This is brilliant pice of work, Thans for this. It would be nice if it can be responsive. I mean chart resizes /changes layout depending on the screen size. this will enable us to view big orgchart on phones in a nice way.

-Ravindra

horizontal chart

Doing it with CSS alone it's a little bit tricky, it would be great to support chart in the other 2D axis.

Nodes with one child have inconsistent layout

When a node has only one child, the size of the line segments is not consistent with other lines. This causes the child (and subsequent children) to be out of vertical alignment with sibling nodes. The problem is visible in the Food example with the Granny Smith Apple.

Also, before reviewing the open issues, I modified the script to support the ignoreSpace parameter in a compatible fashion but using a separate function. My initial attempt incorporated styles as described in one of the other comments, but realised that the 'display: none' styling was being overwritten by the table styles.

The appended diffs resolve the layout and incorporate the ignoreSpace change. I have not used GitHub previously and would like to defer the change to someone who has the tools and more experience. There remain some similar problems in cases where the content of the node extends beyond the boundary of the text area.

Very impressive, easy to integrate plug-in. I needed to review a tree with some thousands of nodes and ended up using this with some Excel to generate the tree.

Comparing files CSS\jquery.jOrgChart.css and ORIGINAL\JQUERY.JORGCHART.CSS
***** CSS\jquery.jOrgChart.css
}
.jOrgChart .dot {
height : 23px;
}
.jOrgChart .left {
***** ORIGINAL\JQUERY.JORGCHART.CSS
}
.jOrgChart .left {


Comparing files jquery.jOrgChart.js and ORIGINAL\JQUERY.JORGCHART.JS
***** jquery.jOrgChart.js
chartClass : "jOrgChart",
ignoreSpace : false,
dragAndDrop: false
***** ORIGINAL\JQUERY.JORGCHART.JS
chartClass : "jOrgChart",
dragAndDrop: false


***** jquery.jOrgChart.js
};
function layout(row, state, opts) {
if(opts.ignoreSpace)
row.css('display', state ? '' : 'none');
else
row.css('visibility', state ? '' : 'hidden');
};
var nodeCount = 0;
***** ORIGINAL\JQUERY.JORGCHART.JS
};
var nodeCount = 0;


***** jquery.jOrgChart.js
var $tr = $this.closest("tr");
$this.css('cursor','n-resize');
if($tr.hasClass('contracted')){
$tr.removeClass('contracted').addClass('expanded');
layout($tr.nextAll("tr"), true, opts);
// Update the

  • appropriately so that if the tree redraws collapsed/non-collapsed nodes
    ***** ORIGINAL\JQUERY.JORGCHART.JS
    var $tr = $this.closest("tr");
    if($tr.hasClass('contracted')){
    $this.css('cursor','n-resize');
    $tr.removeClass('contracted').addClass('expanded');
    $tr.nextAll("tr").css('visibility', '');
    // Update the
  • appropriately so that if the tree redraws collapsed/non-collapsed nodes


    ***** jquery.jOrgChart.js
    }else{
    $tr.removeClass('expanded').addClass('contracted');
    layout($tr.nextAll("tr"), false, opts);
    $node.addClass('collapsed');
    ***** ORIGINAL\JQUERY.JORGCHART.JS
    }else{
    $this.css('cursor','s-resize');
    $tr.removeClass('expanded').addClass('contracted');
    $tr.nextAll("tr").css('visibility', 'hidden');
    $node.addClass('collapsed');


    ***** jquery.jOrgChart.js
    var $linesRow = $("");
    if($childNodes.length > 1) {
    $childNodes.each(function() {
    ***** ORIGINAL\JQUERY.JORGCHART.JS
    var $linesRow = $("");
    $childNodes.each(function() {


    ***** jquery.jOrgChart.js
    .removeClass("top");
    }
    else
    $linesRow.append($(" ").addClass("line left dot"))
    .append($(" ").addClass("line right dot"));
    $tbody.append($linesRow);
    ***** ORIGINAL\JQUERY.JORGCHART.JS
    .removeClass("top");
    $tbody.append($linesRow);


    ***** jquery.jOrgChart.js
    console.log($node);
    layout($nodeRow.nextAll('tr'), false, opts);
    $nodeRow.removeClass('expanded');
    ***** ORIGINAL\JQUERY.JORGCHART.JS
    console.log($node);
    $nodeRow.nextAll('tr').css('visibility', 'hidden');
    $nodeRow.removeClass('expanded');


  • New node div get the same id of original elements

    var new_node_id = $node.attr("id");
    if (typeof new_node_id !== 'undefined' && new_node_id !== false) {
    $nodeDiv = $("

    ").addClass("node").attr("id", $node.attr("id")).append($nodeContent);
    }else{
    $nodeDiv = $("
    ").addClass("node").append($nodeContent);
    }

    It's invalid if two element have the same ids.

    Loading Very Large Trees

    Hello All,

    We are running in to major issues when loading very large trees (Nodes=6,000; Depth=10). It kills the browser and runs in to "JavaScript running slow" issues. We have tried class='collapsed' on nodes, but it doesn't work.

    Is there a way we can overcome this problem. Can we load using AJAX? How can I optimize my code?

    Our team would really appreciate any help.

    Regards,
    Vishal

    Make expandable an option.

    Hey Wes, Love the plugin. I think it would be cool if you could make expandable/contractble an option. I was able to do it on my copy locally but other users may find it useful as well.

    Multiple parent nodes for child

    I am trying to represent a company organisation chart using this plugin.
    However, this company has several employees that report to 2 managers.
    Anyway that this can be achieved without duplicating employees on the chart?

    AJAX to expand nodes

    I have requirement to display a large tree with 100s of nodes. Is it possible to child nodes using AJAX request?

    Export tree to JSON ? and limit child members?

    Is there currently any way to export a chart's structure to JSON? I'd like to allow users to dynamically rearrange their flow, add nodes as desired and have a way to export out the current state into json, while "somehow" maintaining the left/right ordering of child nodes, etc..

    Also, is there any way each parent node can restrict the number of child nodes?

    I'd like to use this to display an upsell flow of products during an order checkout process. In my application, each main product (respective parent node) would have accept/reject decision based logic to redirect to "the next upsell" based on 2 child nodes assigned under the respective parent. (Product A -> Accepts to Product B, and Rejects to Product C)

    I'd need to limit 2 child nodes per parent node, and assign them as an "accept" or "reject" stage of the flow.

    This is an awesome application! I look forward to using it in my app.
    Thanks!
    Chris

    Drag and Drop to Rearrange Nodes at the Same Level

    This is more of a feature request, but I'd like to be able to drag and drop nodes on the same level to rearrange them. In my app, order of the tree nodes left to right is important. Any tips or tricks on how to allow dragging and dropping to rearrange the order of nodes?

    Nodes initially collapsed cannot be expanded in new version

    I had a tree with a few nodes set as class="collapsed" which made them display initially contracted but that could be expanded/contracted at will.

    After downloading the new version (the one with the new License terms) I noticed that those nodes with class="collapsed' still show up contracted as expected but clicking on them does not allow them to expand anymore.

    When trying to trace back the issue I found that in the new version the following line had been removed when compared to the previous one in the "Expand and contract nodes" section of the code:

    $tr.nextAll("tr").fadeToggle("fast");

    By adding that line back to the new version the initially collapsed nodes can now be expanded/contracted as before.

    The final section of code looks like this:

    // Expand and contract nodes
    if ($childNodes.length > 0) {
      $nodeDiv.click(function() {
          var $this = $(this);
          var $tr = $this.closest("tr");
          // Added line below to fix issue when node initially collapsed cannot be expanded!
          //            (copied from former version of the library)
          $tr.nextAll("tr").fadeToggle("fast");
    
          if($tr.hasClass('contracted')){
            $this.css('cursor','n-resize');
            $tr.removeClass('contracted').addClass('expanded');
            $tr.nextAll("tr").css('visibility', '');
          }else{
            $this.css('cursor','s-resize');
            $tr.removeClass('expanded').addClass('contracted');
            $tr.nextAll("tr").css('visibility', 'hidden');
          }
        });
    }
    

    I tested this on Internet Explorer 8.0.7601.17514 on Windows 7.

    Awesome work on the library by the way! The functionality is great!

    Hiding Other Trees

    Just curious if you've thought about it, plan to implement it, or don't care about the feature.
    Was curious about opening up a tree branch and all other sibling branches would close, so that way there wouldn't be a slew of open branches if you have a large tree?

    how to add child node dynamic?

    i want to add some function , when click current node's image(like add), and add a child node which parent is current node. how to do that?

    Fade in & Out Effect

    I have added fade In and Out effect on some of the command to make the plug-in looks better, however, it is only working when I refresh the page. Will you have any suggestion for this new add-on function for me. Thank you so much !!

    Lines between nodes are behaving badly

    Hello! Of course this is a great thing you've done, thank you. I'm having some issues with how the lines between nodes are appearing. Basically it's adding extra lines in some instances and bunching them all up to one side in other instances. I'm not sure what the issue is, but I'm hoping you can help me out. If you can reach out to me I can give you a URL to see what's happening.

    Add Ability To Only Display Current Branch

    When displaying a large tree (i.e. many nodes) the usefulness of the app slips a bit. A user has to use the scroll bars to move left/right and up/down.

    Currently I have the following set up locally:

    1. Every node is collapsed by default.
    2. IgnoreWhite space is the default (@gdott9, Issue #35, Pull Request pending).
    3. I'm pulling in data via json feed via dataObject (@goldenhooves, Issue #24, Pull Request pending). This json was a little funky at first but I have it working.

    The 3 steps above have help considerably but how can I improve the user experience if I have many nodes? I think the easiest solution would be to only allow the nodes of the current branch to be expanded. All other nodes will collapse. It's not perfect but I think at the very least, having the ability to only display the current branch is a nice feature.

    HTML links inside the node toggle the nodes

    I believe this is an unwanted behaviour of the plug-int that somehow got unnoticed.

    If I add an HTML link into a node that has child nodes, by clicking on the HTML link inside the node, it opens/closes it (toggles that node). I really don’t want it to open/close (toggle) if I click on an link within the node… Is there an easy fix for this? Thank you in advance!

    p.s. this behaviour is NOT noticeable in your demo as the node with HTML link does not have any children in it, so it does not do the 'toggle behaviour' on link click.

    NC

    The chart does not show in IE9

    When a page with an org chart is opened with IE9, the chart is not shown in the page. Refreshing the page does not help. If you view source, the HTML structure is there, but the chart just does not show. The strange thing is, if you open the developer tools (F12) and refresh the page, the chart will show. The problem can be reproduced by closing and restarting IE9.

    How to center the chart?

    I've tried everything I am capable of to get the chart always centered in the browser window. Right now it is aligned to left, so if I only have a few items in my chart it looks rather ugly due to the left align.

    It should be possible to center align it.

    Support for latest JQuery

    The latest version on github is not compatible with the latest JQuery libraries. In particular, I am using 1.11.2 and the jOrgChart works only with 1.7.1

    How to add onclick event to chart items?

    I wish to add onclick or onmouseover events to chart items.

    How to accomplish this? I'm totally stuck here. I've tried things like:

    $('li').click() {
    // do something
    }

    $('#org li').click() {
    // do something
    }

    But nothing I am trying applies...

    Recommend Projects

    • React photo React

      A declarative, efficient, and flexible JavaScript library for building user interfaces.

    • Vue.js photo Vue.js

      🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

    • Typescript photo Typescript

      TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

    • TensorFlow photo TensorFlow

      An Open Source Machine Learning Framework for Everyone

    • Django photo Django

      The Web framework for perfectionists with deadlines.

    • D3 photo D3

      Bring data to life with SVG, Canvas and HTML. 📊📈🎉

    Recommend Topics

    • javascript

      JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

    • web

      Some thing interesting about web. New door for the world.

    • server

      A server is a program made to process requests and deliver data to clients.

    • Machine learning

      Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

    • Game

      Some thing interesting about game, make everyone happy.

    Recommend Org

    • Facebook photo Facebook

      We are working to build community through open source technology. NB: members must have two-factor auth.

    • Microsoft photo Microsoft

      Open source projects and samples from Microsoft.

    • Google photo Google

      Google ❤️ Open Source for everyone.

    • D3 photo D3

      Data-Driven Documents codes.