dabeng / OrgChart

It's a simple and direct organization chart plugin. Anytime you want a tree-like chart, you can turn to OrgChart.
MIT License
2.88k stars 772 forks source link

Having no children element in any element throws error if you use local datasource #197

Open Aweorih opened 7 years ago

Aweorih commented 7 years ago

It seems that if you use jquery 2 (not much tested but that's the big difference I see at the moment after comparing the example to my use case)

$.each(nodeData.children, function (index, child) {  
  child.parentId = nodeData.id;  
});  

at this line throws an error (Uncaught TypeError: Cannot read property 'length' of undefined) if there is any element that has no 'children' attribute if you use local json datasource. Problem could be easily fixed if you wrap a check around it, e.g. changing it to:

if ('undefined' !== typeof nodeData.children) {  
  $.each(nodeData.children, function (index, child) {  
    child.parentId = nodeData.id;  
  });  
}
sbubaron commented 7 years ago

Same is true when using AJAX/JSON, best bet is to set children to empty array