如何在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
相关产品推荐
相关产品推荐

