我想在ajax调用后在同一页面上显示数据表+谷歌图表。我从ajax调用中获取numberOfStudents和numberOfFamiliesUsingApp,我将其传递给谷歌图表。当我尝试下面的代码页时,只是没有显示。在开发人员工具内部,我一直看到“缓冲区使用”,但没有显示任
<script src='assets/js/jquery.dataTables.min.js'></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
var classId = "<s:property value="classId"/>" ;
var schoolId = "<s:property value="schoolId"/>" ;
var tableData = [];
var numberOfStudents = 0;
var numberOfFamiliesUsingApp = 0;
$(document).ready(function() {
$.getJSON( "json1/getJSONParentResult.json?schoolId="+schoolId+"&classId="+classId, function( data ) {
tableData = data.data;
numberOfStudents = data.numberOfStudents;
numberOfFamiliesUsingApp = data.numberOfFamiliesUsingApp;
//Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Total');
data.addColumn('number', 'Usage');
//replace the values with javascript variables which are set after ajax call
data.addRows([
['Total Students', numberOfStudents],
['Total Usage', numberOfFamiliesUsingApp]
]);
// Set chart options
var options = {'title':'School Rush Activities',
'width':500,
'height':400};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
$('#example').dataTable( {
"bPaginate": true,
"bProcessing": true,
"bServerSide": false,
"iDisplayLength": 100,
"aaSorting": [[1, 'asc']],
"aaData": tableData
} );
});
} );
</script>
<div id="chart_div"></div>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Last Login</th>
<th>Student LastName</th>
<th>Student FirstName</th>
<th>Account Username</th>
</tr>
</thead>
</table>
如果我评论谷歌图表代码,那么数据表会正确显示。无法理解这里出了什么问题。请帮忙。
答案 0 :(得分:0)
您的脚本存在语法错误,因为您尚未正确转义字符串。这些是这样的原因:
var classId = "<s:property value="classId"/>";
var schoolId = "<s:property value="schoolId"/>";
您可以使用反斜杠转义"
,也可以使用'
。像这样:
var classId = "<s:property value=\"classId\"/>";
var schoolId = '<s:property value="schoolId" />';
(请注意代码的颜色化如何使这一点非常清晰。如果您还没有,使用good text editor将帮助您避免将来出现类似问题