我想单击 Google 树控件中的一个矩形,并在页面的其他部分设置一个值。
我正在使用以下代码,但没有任何警报发生。
tree.draw(data, {
minColor: '#f00',
midColor: '#ddd',
maxColor: '#0d0',
headerHeight: 15,
fontColor: 'black',
showScale: true
});
}
// this code does not work
function myOnClickFunction(){
TreeMap.getSelection();
var txt = TreeMap.getValue(0, TreeMap.getSelection());
alert(txt);
}
google.visualization.events.addListener(TreeMap, 'select', myOnClickFunction);
// end of none working code
</script>
</head>
<body>
<div id="chart_div" style="width: 500px; height: 900px;"></div>
</body>
</html>
最佳答案
首先,您在绘图函数中将 TreeMap 可视化的变量命名为 tree
,但尝试使用名为 TreeMap
的变量创建事件处理程序。其次,您的事件处理程序不在您的 tree
变量的范围内。第三,getSelection
方法本身不返回列索引,因此您无法在 getValue
方法调用中直接使用输出。更改变量名称并将事件处理程序移动到绘图函数内,然后调整事件处理程序内的代码,如下所示:
function myOnClickFunction(){
var selection = tree.getSelection();
if (selection.length) {
var txt = data.getValue(selection[0].row, selection[0].column);
alert(txt);
}
}
google.visualization.events.addListener(tree, 'select', myOnClickFunction);
关于javascript - 谷歌树控件onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24584210/