Google Chart getSelection没有列属性 [英] Google Chart getSelection doesn't have column property

查看:97
本文介绍了Google Chart getSelection没有列属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用时:

chart.getChart().getSelection()[0]

在图表上(从chartwrapper,因此首先是getChart()),getSelection()函数返回 即使我的图表"是一个表,也只有行属性,但没有列属性,单击其中的任何位置都应返回行和列属性.

on a chart (from a chartwrapper, hence the getChart() first), the getSelection() function returns only a row-property but no column property even though my 'chart' is a table and clicking anywhere within it should return both a row and column property.

这是已知的Google图表错误吗?有人知道解决方法吗?

Is this a known google charts bug? Does anyone know of a workaround?

我也在Google网上论坛中找到了这个主题: https://groups.google.com/forum/# !topic/google-visualization-api/O_t7-s96A9w

Also I have found this topic on google groups: https://groups.google.com/forum/#!topic/google-visualization-api/O_t7-s96A9w

他们在这里说: 当前,Table对象仅支持行选择,因此column属性始终是未定义的.如果这对您很重要,则可以通过在每个单元格的格式化值中添加一些特殊的html代码来自己捕获这些事件.

here they say: Currently the Table object only supports row selection and therefore the column property is always undefined. If this is important for you, you can catch these events by yourself by adding some special html code int he formatted value of each cell.

任何人都知道如何执行此操作吗?

Anyone have an idea how to do this?

推荐答案

即使google.visualization.table仅支持行选择(它解释了为什么column属性返回null的原因),您仍可以考虑以下访问列属性的方法:

Even though google.visualization.table supports row selection only (it explains why column property returns null), you could consider the following approach to access column property:

google.load('visualization', '1', {
    packages: ['table']
});
google.setOnLoadCallback(drawTable);

function drawTable() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Name');
    data.addColumn('number', 'Salary');
    data.addColumn('boolean', 'Full Time Employee');
    data.addRows([
      ['Mike', {
          v: 10000,
          f: '$10,000'
      },
        true
      ],
      ['Jim', {
          v: 8000,
          f: '$8,000'
      },
        false
      ],
      ['Alice', {
          v: 12500,
          f: '$12,500'
      },
        true
      ],
      ['Bob', {
          v: 7000,
          f: '$7,000'
      },
        true
      ]
    ]);

    var table = new google.visualization.Table(document.getElementById('table_div'));
    google.visualization.events.addListener(table, 'select', function(){
        selectHandler(table);
    });
    table.draw(data, {
        showRowNumber: false
    });
}



function selectHandler(table) {
  var selection = table.getSelection();
  if(selection.length === 0)
      return;

  var e = event || window.event;
  var cell = e.target; //get selected cell
 
  document.getElementById('output').innerHTML = "Row: " +  selection[0].row + " Column: " +  cell.cellIndex;

}

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div id="table_div"></div>
<div id="output"></div>

这篇关于Google Chart getSelection没有列属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆