OrgChart icon indicating copy to clipboard operation
OrgChart copied to clipboard

Chart displays but 'init.orgchart' does not fire if using ajax

Open andyp05 opened this issue 6 years ago • 6 comments

Love the package, but I have run into a problem.

When using ajax, the initcompleted function fires but the DOM has not been updated so adding an onclick event does not work. additionally, oc.$chart.on('init.orgchart', function(e) {...} never fires.

var oc = $('#chart-container').orgchart({
      'data': '/api/overview/',
      'nodeContent': 'title',
      'verticalLevel': 3,
      'direction': 't2b',
      'visibleLevel': 4,
      'parentNodeSymbol': '',
      'nodeId': 'id',
      'initCompleted': function($chart) {
        // nodes are not rendered in the DOM at this point, so the find() returns nothing
         $chart.find('.et-1').click(function() {
          window.location.href = '/et/1/';
        });
      },
    });

    // this never fires:
    oc.$chart.on('init.orgchart', function(e) {...}

If I use a local datasource, they both work as expected.

How can I add an onclick event to nodes with a certain className when using ajax?

Thanks for your help.

andyp05 avatar Oct 05 '19 20:10 andyp05

I'm very appreciated for your applying orgchart plugin in your project 😊

Do you got the same error when using latest version 2.1.9 ?

dabeng avatar Oct 12 '19 02:10 dabeng

2.1.9 does not fix the issue.

If I set 'data' the 'initCompleted' fires with the DOM updated.

If I set 'url', 'initCompleted' fires with no DOM for the orgchart. Therefore I cannot set an onclick event.

andyp05 avatar Oct 14 '19 18:10 andyp05

Yes. same goes for on-demand loading. it would be nice to fire an event every time new nodes are added so that we can initialize tooltips and such. Let me know if you need me to send a PR.

shahimclt avatar Oct 29 '19 14:10 shahimclt

I have come across this in the latest version. When using an Ajax datasource, initCompleted runs straight away and not actually when it has all been loaded

DigitalSkateboarder avatar Dec 29 '20 12:12 DigitalSkateboarder

I have come across this in the latest version. When using an Ajax datasource, initCompleted runs straight away and not actually when it has all been loaded

Same here

diogenesc avatar Aug 29 '21 20:08 diogenesc

I encountered the same problem as discussed here. I'm thinking it's because of the promises based call to the ajax backend that causes the init callback to be called before the content is loaded. This meant that I couldn't centre the chart since the chart didn't exist at that point.

If you're interested, I think I have a (somewhat hacky) solution to this problem. By using a MutationObserver it's possible to detect when the hierarchy is added and force my centre function to be called.

      var mo = new MutationObserver(function (mutations) {
        centreOrgChart:
          for (var i = 0; i < mutations.length; i++) {
            for (var j = 0; j < mutations[i].addedNodes.length; j++) {
              if (mutations[i].addedNodes[j].classList.contains('hierarchy')) {
                centreChart();
                mo.disconnect();
                break centreOrgChart;
              }
            }
          }
      });
      mo.observe(document.querySelector("#chart-container"), { childList: true, subtree: true });

The centreChart() function just applies a scrollLeft function to the chart, which I have seen on other issues for this project.

      function centerChart() {
        var $container = $('#chart-container');
        $container.scrollLeft(Math.max($container[0].scrollWidth - $container.width(), $container.width())/2);
      }

philipnorton42 avatar Feb 28 '23 16:02 philipnorton42