简体   繁体   English

Google Charts API中的事件监听器

[英]Event Listener in Google Charts API

I'm busy using Google Charts in one of my projects to display data in a table. 我正在一个项目中忙于使用Google图表在表格中显示数据。 Everything is working great. 一切都很好。 Except that I need to see what line a user selected once they click a button. 除非我需要查看用户单击按钮后选择的行。

This would obviously be done with Javascript, but I've been struggling for days now to no avail. 这显然可以用Javascript完成,但是我已经苦苦挣扎了好几天了。 Below I've pasted code for a simple example of the table, and the Javascript function that I want to use (that doesn't work). 下面,我为该表的简单示例粘贴了代码,并粘贴了要使用的Javascript函数(不起作用)。

<html>
<head>
<script type='text/javascript' src='https://www.google.com/jsapi'></script>

<script type='text/javascript'>
  google.load('visualization', '1', {packages:['table']});
  google.setOnLoadCallback(drawTable);
  var table = "";

  function drawTable() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Name');
    data.addColumn('number', 'Salary');
    data.addColumn('boolean', 'Full Time Employee');
    data.addRows(4);
    data.setCell(0, 0, 'Mike');
    data.setCell(0, 1, 10000, '$10,000');
    data.setCell(0, 2, true);
    data.setCell(1, 0, 'Jim');
    data.setCell(1, 1, 8000, '$8,000');
    data.setCell(1, 2, false);
    data.setCell(2, 0, 'Alice');
    data.setCell(2, 1, 12500, '$12,500');
    data.setCell(2, 2, true);
    data.setCell(3, 0, 'Bob');
    data.setCell(3, 1, 7000, '$7,000');
    data.setCell(3, 2, true);

    table = new google.visualization.Table(document.getElementById('table_div'));
    table.draw(data, {showRowNumber: true});
  }

    function selectionHandler() {
        selectedData = table.getSelection();
        row = selectedData[0].row;
        item = table.getValue(row,0);

        alert("You selected :" + item);         

    }

</script>
</head>

<body>
<div id='table_div'></div>

<input type="button" value="Select" onClick="selectionHandler()">   

</body>
</html>

Thanks in advance for anyone taking the time to look at this. 在此先感谢您抽出宝贵时间来研究此问题。 I've honestly tried my best with this, hope someone out there can help me out a bit. 老实说,我已经尽力了,希望有人能帮助我。

Here is a working version. 这是一个工作版本。
There was no listener to the select event, and you mixed up, data and table for the getValue call. 没有侦听器选择事件,您将datatable混合在一起进行getValue调用。

<html>
<head>
<script src='https://www.google.com/jsapi'></script>

<script>
    google.load('visualization', '1', {packages:['table']});
    google.setOnLoadCallback(drawTable);
    var table, data;

    function drawTable() {
        data = new google.visualization.DataTable();
        data.addColumn('string', 'Name');
        data.addColumn('number', 'Salary');
        data.addColumn('boolean', 'Full Time Employee');
        data.addRows(4);
        data.setCell(0, 0, 'Mike');
        data.setCell(0, 1, 10000, '$10,000');
        data.setCell(0, 2, true);
        data.setCell(1, 0, 'Jim');
        data.setCell(1, 1, 8000, '$8,000');
        data.setCell(1, 2, false);
        data.setCell(2, 0, 'Alice');
        data.setCell(2, 1, 12500, '$12,500');
        data.setCell(2, 2, true);
        data.setCell(3, 0, 'Bob');
        data.setCell(3, 1, 7000, '$7,000');
        data.setCell(3, 2, true);

        table = new google.visualization.Table(document.getElementById('table_div'));
        table.draw(data, {showRowNumber: true});
        //add the listener
        google.visualization.events.addListener(table, 'select', selectionHandler);
    }

    function selectionHandler() {
        var selectedData = table.getSelection(), row, item;
        row = selectedData[0].row;
        item = data.getValue(row,0);
        alert("You selected :" + item);         
    }

</script>
</head>

<body>
<div id='table_div'></div>

<input type="button" value="Select" onClick="selectionHandler()">   

</body>
</html>

Hard for me to know what wrong with your code, here example code from the documentation explain how to handle select event: 我很难知道您的代码有什么问题,在此文档中的示例代码说明了如何处理select事件:

// Create our table.
var table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(data, options);

// Add our selection handler.
google.visualization.events.addListener(table, 'select', selectHandler);

// The selection handler.
// Loop through all items in the selection and concatenate
// a single message from all of them.
function selectHandler() {
  var selection = table.getSelection();
  var message = '';
  for (var i = 0; i < selection.length; i++) {
    var item = selection[i];
    if (item.row != null && item.column != null) {
      var str = data.getFormattedValue(item.row, item.column);
      message += '{row:' + item.row + ',column:' + item.column + '} = ' + str + '\n';
    } else if (item.row != null) {
      var str = data.getFormattedValue(item.row, 0);
      message += '{row:' + item.row + ', column:none}; value (col 0) = ' + str + '\n';
    } else if (item.column != null) {
      var str = data.getFormattedValue(0, item.column);
      message += '{row:none, column:' + item.column + '}; value (row 0) = ' + str + '\n';
    }
  }
  if (message == '') {
    message = 'nothing';
  }
  alert('You selected ' + message);
}

You can give it try live code from jsfiddle 您可以尝试使用jsfiddle的实时代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM