Reputation: 91
I have a dashboard containing a table and a string filtering box. I would like to interact with the table to be able to select rows and retrieve respective data from it.
after slight changes I got the getSelection() method working but there appeared another problem.. With the code below I try to filter the table and then select and get row data.. It all seems okay but when I do so the filtered table row numbers and the row numbers in the actual data does not match. that is I end up alerting the row data with reference to the pre-filtered table... Again any suggestion is highly valued.. thanks..
var dashboard, table, data;
function drawVisualization() {
var array = new Array(['ticker','time','bid','open','high','low','volume']);
var ticker, time, bid, open, high, low, volume;
$.get('php/getdata.php', {input: 'stocklist'}, function(data1){
$.each(data1, function(index, value){
ticker = value.ticker;
time = value.time;
bid = parseFloat(value.bid);
open = parseFloat(value.open);
high = parseFloat(value.high);
low = parseFloat(value.low);
volume = parseFloat(value.volume);
array.push([ticker, time, bid, open, high, low, volume]);
});
data = google.visualization.arrayToDataTable(array);
var stringFilter = new google.visualization.ControlWrapper({
'controlType': 'StringFilter',
'containerId': 'control1',
'options': {
'filterColumnLabel': 'ticker'
}
});
table = new google.visualization.ChartWrapper({
'chartType': 'Table',
'containerId': 'chart1',
'options': {'showRowNumber': false, 'height': '130px', 'width': '1000px'}
});
dashboard = new google.visualization.Dashboard(document.getElementById('dashboard'))
dashboard.bind(stringFilter, table);
dashboard.draw(data);
google.visualization.events.addListener(table, 'select', selectHandler);
}, "json");
}
function selectHandler() {
var selection = table.getChart().getSelection();
for (var i = 0; i < selection.length; i++) {
var item = selection[i];
if (item.row != null && item.column != null) {
} else if (item.row != null) {
stockID = data.getFormattedValue(item.row, 0);
} else if (item.column != null) {
stockID = data.getFormattedValue(0, item.column);
}
}
if (stockID == '') {
return;
}
alert(stockID);
}
google.setOnLoadCallback(drawVisualization);
Upvotes: 2
Views: 968
Reputation: 137
I've had a similar problem. You have to get the new DataTable after applying the filter. Try changing
stockID = data.getFormattedValue(item.row, 0);
to
stockID = table.getDataTable().getFormattedValue(item.row, 0);
and the same for the other case.
cheers
Upvotes: 1