这里我有谷歌可视化数据表:
function drawTroskovnik() {
var cssClassNames = {
'headerRow': 'zaglavlje',
'tableRow': 'red',
'oddTableRow': 'red',
'selectedTableRow': 'orange-background large-font',
'hoverTableRow': 'prekoreda',
'headerCell': 'gold-border',
'tableCell': 'cell',
'rowNumberCell': 'underline-blue-font'
};
// Create and populate the data table.
var JSONObject = $.ajax({
url: 'getTroskovnik.php', // make this url point to the data file
dataType: 'json',
data:{id_akt:ajdi},
async: false,
type: 'POST',
}).responseText;
var data = new google.visualization.DataTable(JSONObject, 0.5);
for (var y = 0, maxrows = data.getNumberOfRows(); y < maxrows; y++) {
for (var x = 1, maxcols = data.getNumberOfColumns(); x < maxcols; x++) {
data.setValue(y, x, '<input id="costRedovi" vr="'+ ajdi + '" kol="'+ data.getColumnLabel(x) +'" class="form-control" value="'+data.getValue(y,x)+'">');
}
}
for (var y = 0, maxrows = data.getNumberOfRows(); y < maxrows; y++) {
data.setValue(y, 0, '<input class="span2 form-control" id="pocetak1" size="16" type="text" value="'+data.getValue(y,0)+'" readonly>');
}
data.addColumn('string', 'Kontrole');
for (var y = 0, maxrows = data.getNumberOfRows(); y < maxrows; y++) {
var mc= data.getNumberOfColumns()-1;
data.setValue(y, mc, '<a data-toggle="modal" data-target="#update" href="#" class="btn btn-success"><i class="fa fa-pencil"></i> Details</a>');
}
var table = new google.visualization.ChartWrapper({
'chartType': 'Table',
'containerId': 'chart2',
'cssClassNames': 'cssClassNames',
'options': {
cssClassNames: cssClassNames,
allowHtml: true
}
});
// Create and draw the visualization.
visualization = new google.visualization.Table(document.getElementById('tpltroskovnik'));
visualization.draw(data, {'allowHtml': true, cssClassNames: {
'headerRow': 'zaglavlje',
'tableRow': 'red',
'oddTableRow': 'red',
'selectedTableRow': 'orange-background large-font',
'hoverTableRow': 'prekoreda',
'headerCell': 'gold-border',
'tableCell': 'cell',
'rowNumberCell': 'underline-blue-font'
}});
}
new google.visualization.events.addListener(visualisation, 'ready', function () {
google.visualization.events.addListener(visualisation, 'select', function () {
var selection = visualisation.getSelection();
console.log(selection);
//alert(data.getValue(visualization.getSelection()[0].row, 0));
ajdi = data.getDataTable().getValue(selection[i].row,0);
console.log(data.getDataTable().getValue(selection[i].row,0));
console.log(data.getDataTable().getColumnLabel(selection[i]));
})
});
但我需要添加活动:
new google.visualization.events.addListener(visualisation, 'ready', function () {
google.visualization.events.addListener(visualisation, 'select', function () {
我尝试了代码的结尾,但是我得到了错误:未定义可视化...还有如何选择行的getselection()?以及如何在表准备好后运行函数SSS();
?
答案 0 :(得分:1)
需要在与可视化相同的范围内创建事件处理程序。此外,通常,您希望在绘制图表之前设置所有事件处理程序(尤其是“就绪”事件处理程序)。
在你的情况下,它看起来像这样:
visualization = new google.visualization.Table(document.getElementById('tpltroskovnik'));
// add select event
google.visualization.events.addListener(visualisation, 'select', function () {
var selection = visualisation.getSelection();
for (var i = 0; i < selection.length; i++) {
// selection[i].row contains the row index, eg:
console.log(data.getValue(selection[i].row, 1));
}
});
// run SSS when table is ready
google.visualization.events.addListener(visualisation, 'ready', SSS);
visualization.draw(data, {
'allowHtml': true,
cssClassNames: {
'headerRow': 'zaglavlje',
'tableRow': 'red',
'oddTableRow': 'red',
'selectedTableRow': 'orange-background large-font',
'hoverTableRow': 'prekoreda',
'headerCell': 'gold-border',
'tableCell': 'cell',
'rowNumberCell': 'underline-blue-font'
}
});