谷歌组织结构图与超链接和1单击展开/折叠

时间:2016-08-05 21:10:14

标签: javascript html charts orgchart

我正在尝试使用Google Org Chart控件。我希望它只需单击一次展开/折叠节点(而不是默认的双击一个),并提供指向用户的个人资料页面的超链接。

我的超链接代码可以正常使用默认双击展开/折叠。但是,如果我为'选择'添加一个监听器。要启用单击展开/折叠的事件,超链接将停止工作。

JSFiddle here https://jsfiddle.net/oxzabtyg/

这是我的代码

 google.charts.load('current', {packages:["orgchart"]});
      google.charts.setOnLoadCallback(drawChart);

   function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Name');
        data.addColumn('string', 'Manager');
        data.addColumn('string', 'ToolTip');
        // For each orgchart box, provide the name, manager, and tooltip to show.
        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']
        ]);
        // Create the chart.
        var chart = new google.visualization.OrgChart(document.getElementById('chart_div'));



        // selection
        google.visualization.events.addListener(chart, 'select', function () {
          // get the row of the node clicked
          var selection = chart.getSelection();
          var row = selection[0].row;
          // get a list of all collapsed nodes
          var collapsed = chart.getCollapsedNodes();
          // if the node is collapsed, we want to expand it
          // if it is not collapsed, we want to collapse it
          var collapse = (collapsed.indexOf(row) == -1);
          chart.collapse(row, collapse);
          // clear the selection so the next click will work properly
          chart.setSelection();
                });


         // Draw the chart, setting the allowHtml option to true for the tooltips.
        chart.draw(data, {allowHtml:true, allowCollapse:true});
            }

2 个答案:

答案 0 :(得分:2)

您可以使用常规DOM点击事件,
然后检查事件目标

如果是锚标记(import {ROUTER_DIRECTIVES} from "@angular/router"; ),则按照地址
否则展开/折叠

请参阅以下工作代码段...

<a>
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']
});

答案 1 :(得分:0)

碰到同样的问题。另一个选择是防止mousedown事件在您的链接中传播:

<a onmousedown="event.stopPropagation()" href="https://www.google.com/">google</a>