[英]Get data from table row on click
這里我有一張表: http : //jsbin.com/IhEmetI/8用 Google 可視化 api 構建,這里是代碼: http : //jsbin.com/IhEmetI/8/edit
代碼:
var table = new google.visualization.ChartWrapper({
'chartType': 'Table',
'containerId': 'chart2',
'cssClassNames': 'cssClassNames',
'options': {
cssClassNames: cssClassNames,
allowHtml: true
}
});
// Create a dashboard
new google.visualization.Dashboard(document.getElementById('dashboard')).
// Establish bindings, declaring the both the slider and the category
// picker will drive both charts.
bind([slider, categoryPicker, stringFilter], [pie, table], [stringFilter, table]).
// Draw the entire dashboard.
draw(data, {'allowHtml':true, 'cssClassNames': 'cssClassNames'}); }
現在我想在單擊某個表行時獲取數據行? 我怎么能做到這一點?
我怎么能用javascript做到這一點? 是否有一些內置的谷歌 API 函數?
您需要為表使用“選擇”處理程序。 使用 ChartWrapper,其語法與正常情況略有不同(因為您必須將 Table 'ready'
事件處理程序包裝在包裝器的'ready'
事件處理程序中)。 它是這樣工作的:
google.visualization.events.addListener(table, 'ready', function () {
google.visualization.events.addListener(table.getChart(), 'select', function () {
var selection = table.getChart().getSelection();
// iterate over all selected rows
for (var i = 0; i < selection.length; i++) {
// do something with selection[i].row
}
});
});
您必須遍歷所有選定的行,因為用戶可以從表中選擇多行。
使用 data.getFormattedValue(selection[i].row, column) 出於某種原因,getValue 總是為整數列返回 0,對於字符串返回空!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.