Google Org Chart with hyperlink and 1 click expand/collapse

后端 未结 2 1910
半阙折子戏
半阙折子戏 2021-01-20 12:26

I am trying to use the Google Org Chart control. I would like it to have a single click expand/collapse of nodes (instead of the default double click one) and also provide a

相关标签:
2条回答
  • 2021-01-20 13:03

    Just run into the same issue. Another option would be to prevent the propagation of the mousedown event in your links:

    <a onmousedown="event.stopPropagation()" href="https://www.google.com/">google</a>
    
    0 讨论(0)
  • 2021-01-20 13:16

    you could use a regular DOM click event,
    then check the event target

    if an anchor tag (<a>), then follow the address
    else expand / collapse

    see following working snippet...

    google.charts.load('current', {
      callback: function () {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Name');
        data.addColumn('string', 'Manager');
        data.addColumn('string', 'ToolTip');
        data.addRows([
          [{v:'Mike', f:'Mike<div><a href="http://www.google.com">google</a></div>'},'', 'The President'],
          [{v:'Jim', f:'Jim<div><a href="http://www.google.com">google</a></div>'},'Mike', 'VP'],
          ['Alice', 'Mike', ''],
          ['Bob', 'Alice', ''],
          [{v:'John', f:'John<div><a href="http://www.google.com">google</a></div>'},'Bob', 'VP'],
          ['Carol', 'Bob', ''],
          [{v:'Jake', f:'Jake<div><a href="http://www.google.com">google</a></div>'},'John', 'VP']
        ]);
    
        var container = document.getElementById('chart_div');
        var chart = new google.visualization.OrgChart(container);
    
        container.addEventListener('click', function (e) {
          e.preventDefault();
          if (e.target.tagName.toUpperCase() === 'A') {
            console.log(e.target.href);
            // window.open(e.target.href, '_blank');
            // or
            // location.href = e.target.href;
          } else {
            var selection = chart.getSelection();
            if (selection.length > 0) {
              var row = selection[0].row;
              var collapse = (chart.getCollapsedNodes().indexOf(row) == -1);
              chart.collapse(row, collapse);
            }
          }
          chart.setSelection([]);
          return false;
        }, false);
    
        chart.draw(data, {allowHtml:true, allowCollapse:true});
      },
      packages: ['orgchart']
    });
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div id="chart_div"></div>

    0 讨论(0)
提交回复
热议问题