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

Dashboard中ChartWrapper表格调用getSelection()报类型错误的问题咨询

Fixing "Uncaught TypeError: table_db.getSelection is not a function" in Google Charts Dashboard

Hey there! Let's break down why you're hitting this error when adding a row-click handler to your ChartWrapper table in a Dashboard, and how to fix it.

The Root Cause

When you use a ChartWrapper inside a Google Charts Dashboard, the table_db variable you're referencing is a ChartWrapper instance—not the underlying google.visualization.Table object you work with in standalone examples. The ChartWrapper itself doesn't have a getSelection() method; that method belongs to the actual Table chart it wraps.

Step-by-Step Solutions

1. Access the Underlying Table Instance

First, you need to grab the real Table chart from your ChartWrapper using the getChart() method. This gives you the object that has the getSelection() functionality you need.

2. Bind Events After the Dashboard is Ready

Make sure you attach your click handler only after the Dashboard and its charts have finished rendering. If you try to access getChart() too early, it might return null or an uninitialized object.

Here's a corrected code example that addresses both points:

// Assume you've already set up your Dashboard, controls, and data
const dashboard = new google.visualization.Dashboard(document.getElementById('dashboard_div'));
const table_db = new google.visualization.ChartWrapper({
  chartType: 'Table',
  containerId: 'table_div',
  // Your chart configuration options here
});

// Bind controls and draw the dashboard
dashboard.bind([yourControl], [table_db]);
dashboard.draw(yourData);

// Wait for the Dashboard to fully load before attaching events
google.visualization.events.addListener(dashboard, 'ready', function() {
  // Get the actual Table chart instance
  const tableInstance = table_db.getChart();
  
  // Add the row selection listener
  google.visualization.events.addListener(tableInstance, 'select', function() {
    const selection = tableInstance.getSelection();
    if (selection.length > 0) {
      const selectedRow = selection[0].row;
      // Fetch the value from your data table (adjust column index as needed)
      const selectedValue = yourData.getValue(selectedRow, 0);
      console.log('Selected row value:', selectedValue);
      // Add your logic to use the selected value here
    }
  });
});

3. Double-Check Your Variable Reference

Accidental variable reassignments (common in larger Dashboard setups) could cause table_db to point to something other than your ChartWrapper instance. Add a console.log(table_db) right before you try to use getSelection() to confirm its type—it should show up as a ChartWrapper object.

Why Your Standalone Code Worked

In your standalone example, you were likely working directly with a google.visualization.Table instance, not a ChartWrapper. That's why getSelection() worked out of the box—you were calling it on the correct object from the start.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:17:36