javascript Google Charts API:新的 google.visualization.Table() - Uncaught TypeError: undefined is not a function
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6963437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Charts API: new google.visualization.Table() - Uncaught TypeError: undefined is not a function
提问by php-b-grader
I have copied the Google Code example into a php script however I am getting the error "undefined is not a function"
我已将 Google 代码示例复制到 php 脚本中,但是出现错误“未定义不是函数”
it is happening specifically on this line:
它特别发生在这条线上:
var table = new google.visualization.Table(document.getElementById('table_sort_div'));
var table = new google.visualization.Table(document.getElementById('table_sort_div'));
It appeats that the Table function does not exist???
看来 Table 函数不存在???
I have copied the code directly from Googles Code examples so I can't understand what I have done wrong... I'm tending to believe that there is an issue with the example but I'm gonna assume I'd make a mistake before google would?
我直接从 Google 的代码示例中复制了代码,所以我无法理解我做错了什么......我倾向于认为该示例存在问题,但我假设我会犯错在谷歌之前?
Code was copied directly from: http://code.google.com/apis/chart/interactive/docs/examples.html#interaction_example
代码直接复制自:http: //code.google.com/apis/chart/interactive/docs/examples.html#interaction_example
采纳答案by Joe
You need to wait for the scripts to load. For example:
您需要等待脚本加载。例如:
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['table']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
var table = new google.visualization.Table(document.getElementById('table_sort_div'));
}
should work, because the scripts have been loaded. A better table reference here
应该可以工作,因为脚本已经加载。更好的表格参考here
回答by Tom Stickel
Also if you want to load multiple packages, you can do that as well like :
此外,如果您想加载多个包,您也可以这样做:
google.load('visualization', '1', { packages: ['corechart', 'table'] });
回答by Prasanna Sujeewa
google.charts.load('current', {'packages': ['table']});
google.charts.setOnLoadCallback(drawTable);
function drawTable() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Name');
data.addColumn('number', 'Salary');
data.addColumn('boolean', 'Full Time');
data.addRows(5);
data.setCell(0, 0, 'John');
data.setCell(0, 1, 10000, ',000');
data.setCell(0, 2, true);
data.setCell(1, 0, 'Mary');
data.setCell(1, 1, 25000, ',000');
data.setCell(1, 2, true);
data.setCell(2, 0, 'Steve');
data.setCell(2, 1, 8000, ',000');
data.setCell(2, 2, false);
data.setCell(3, 0, 'Ellen');
data.setCell(3, 1, 20000, ',000');
data.setCell(3, 2, true);
data.setCell(4, 0, 'Mike');
data.setCell(4, 1, 12000, ',000');
data.setCell(4, 2, false);
var table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(data, {showRowNumber: true, width: '100%', height: '100%'});
google.visualization.events.addListener(table, 'select', function () {
var row = table.getSelection()[0].row;
alert('You selected ' + data.getValue(row, 0));
});
}