[英]Event Listener in Google Charts based on Fusion Tables
I am trying to use Google Fusion Tables with Google Charts to construct a table that would response to changes in a drop down menu. 我正在尝试将Google Fusion Tables与Google Charts一起使用,以构建一个可以响应下拉菜单中的更改的表。
I am following this example very closely: 我正在密切关注这个示例:
https://developers.google.com/fusiontables/docs/samples/gviz_datatable https://developers.google.com/fusiontables/docs/samples/gviz_datatable
I can draw the table using data from the Fusion Tables. 我可以使用Fusion Tables中的数据绘制表格。 The table would response to the selection menu without issues.
该表将响应选择菜单而不会出现问题。
I want to implement a selectHandler that would store the content of the row that is selected by the user. 我想实现一个selectHandler,它将存储用户选择的行的内容。 I am going to pass the content of the row to other functions, but I just couldn't get the selectHandler to work correctly.
我将把行的内容传递给其他函数,但是我无法使selectHandler正常工作。
google.load('visualization', '1', {packages: ['table']});
function drawTable() {
var query = "SELECT 'key', 'description' as Style, " +
"'business_name' as Name, 'Rating' " +
'FROM 15bCp26r1CDuN86Tu8hMOGRWlZwNI30Pl60srz9g';
var vendors = document.getElementById('vendors').value;
if (vendors) {
query += " WHERE 'description' = '" + vendors + "'";
}
var queryText = encodeURIComponent(query);
var gvizQuery = new google.visualization.Query(
'http://www.google.com/fusiontables/gvizdata?tq=' + queryText);
gvizQuery.send(function(response) {
var table = new google.visualization.Table(
document.getElementById('visualization'));
var data = response.getDataTable();
table.draw(data, {
showRowNumber: false,
sortColumn: 3,
sortAscending: false
});
google.visualization.events.addListener(table, 'select', selectHandler);
function selectHandler() {
//alert("Selected");
var selectedItem = table.getSelection()[0];
var value = data.getValue(selectedItem.row, selectedItem.column);
alert(value);
}
});
}
I am following the example pretty closely. 我正在密切关注这个例子。 The selectHandler does work.
selectHandler确实起作用。 I can get an alert box to pop up when the user click on a row, but I can't store the content of the row to the variable value.
当用户单击一行时,我可以弹出一个警告框,但不能将行的内容存储到变量值中。
What am I doing wrong? 我究竟做错了什么?
When you declare var value
inside the selectHandler
function, the scope of value
is local to the function. 当在
selectHandler
函数中声明var value
, value
的范围是该函数的局部范围。 Once the function returns, the local variables are marked for garbage collection and made unaccessible. 函数返回后,将局部变量标记为垃圾回收,并使其不可访问。 If you want to store
value
longer-term, then it needs to be declared outside the local scope of selectHandler
, like this: 如果要长期存储
value
,则需要在selectHandler
的本地范围之外声明它,如下所示:
var value;
function selectHandler () {...}
Incidentally, in the selectHandler
function, you should be testing for the length of the selection, as it could be zero (which would throw an error in your code) or more than 1 (in which case you are not capturing all of the relevant information). 顺便说一句,在
selectHandler
函数中,您应该测试选择的长度,因为它可能为零(这将在您的代码中引发错误)或大于1(在这种情况下,您不会捕获所有相关信息) )。 Try something like this: 尝试这样的事情:
function selectHandler() {
var selection = table.getSelection();
if (selection.length > 0) {
// do something
}
}
or this: 或这个:
function selectHandler() {
var selection = table.getSelection();
for (var i = 0; i < selection.length; i++) {
// do something
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.