You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Angular Google Charts(OrgChart)中获取节点的childrenIndexes

angular-google-charts 获取OrgChart选中节点子节点索引方案

getChildrenIndexes(row)是Google Charts原生OrgChart实例的内置方法,angular-google-charts上层组件没有直接暴露这个方法,你需要按以下步骤拿到原生实例后再调用:

  • 模板部分:给图表组件添加模板引用变量,绑定ready和select事件
<google-chart
  #orgChartRef
  [type]="'OrgChart'"
  [data]="orgChartData"
  [options]="orgChartOptions"
  (ready)="handleChartReady()"
  (select)="handleNodeSelect()"
></google-chart>
  • 组件逻辑部分:通过@ViewChild拿到图表组件实例,在ready回调中缓存原生OrgChart实例
import { Component, ViewChild } from '@angular/core';
import { GoogleChartComponent } from 'angular-google-charts';

@Component({
  selector: 'app-org-chart',
  templateUrl: './org-chart.component.html'
})
export class OrgChartComponent {
  @ViewChild('orgChartRef') orgChart!: GoogleChartComponent;
  private nativeChartInstance: any;

  // 图表渲染完成后缓存原生实例
  handleChartReady(): void {
    this.nativeChartInstance = this.orgChart.chart.getChart();
  }

  // 节点选中时获取子节点索引
  handleNodeSelect(): void {
    const selection = this.orgChart.chart.getSelection();
    if (!selection.length || selection[0].row === null) return;
    
    const selectedRow = selection[0].row;
    // 调用原生方法获取子节点索引数组
    const childIndexes = this.nativeChartInstance.getChildrenIndexes(selectedRow);
    console.log('选中节点子节点索引:', childIndexes);
  }
}

注意:必须等ready事件触发完成后再调用原生方法,图表未渲染完成时原生实例未初始化,直接调用会抛出方法不存在的错误。

内容的提问来源于stack exchange,提问作者Ahsan Hasan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.02 00:39:36