Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带有var视图的Google visualization drawTable()不工作_Javascript_Google Visualization - Fatal编程技术网

Javascript 带有var视图的Google visualization drawTable()不工作

Javascript 带有var视图的Google visualization drawTable()不工作,javascript,google-visualization,Javascript,Google Visualization,以下是我的代码: <head> <!--Load the AJAX API--> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></s

以下是我的代码:

<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">

// Load the Visualization API and the piechart package.
google.load("visualization", "1", {packages:["table"]});

var table = new google.visualization.Table(document.getElementById('chart_div'));

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawTable);
var jsonData = ${requestScope.jsonData};

// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData);
var view = new google.visualization.DataView(data);

var addButton = document.getElementById('add');
var removeButton = document.getElementById('remove');

function drawTable() {
  table.draw(view);
}

removeButton.onclick=function(){
    view.hideColumns([1]);
    drawTable();
}

drawTable();

</script>
</head>
我想把它们放在外面的原因是,我试图从另一个函数访问var视图,该函数将在单击按钮时隐藏视图的列


提前感谢您的帮助。

google.setOnLoadCallback(drawTable)的真正目的是什么是为了避免在库加载之前调用GoogleAPI的函数,因为它是由async加载的。因此,无论何时使用api,它都应该位于
drawTable()
函数内或在该函数之后运行的其他地方

为了能够使用
drawTable()
之外的数据,您只需要在外部创建变量,然后在内部修改它:

var view = null;
var table = null;

function drawTable(){
    //your code... 
    var table = new google.visualization.Table(document.getElementById('chart_div'));
    //your code... 
    var view = new google.visualization.DataView(data);
    //your code... 
}
然后你可以做:

removeButton.onclick=function(){
    if(view != null && table != null){
        view.hideColumns([1]);
        table.draw(view);
    }
}

当然,谷歌api还没有加载,所以会抛出错误。你需要把它放在抽屉里。如果您想在外部访问它,只需使用
var视图外部和内部:
view=newgoogle.visualization.DataView(数据)谢谢!如果你把它作为答案贴出来,我会接受的
removeButton.onclick=function(){
    if(view != null && table != null){
        view.hideColumns([1]);
        table.draw(view);
    }
}