【发布时间】:2015-07-17 17:46:16
【问题描述】:
我正在使用 Play!框架。我有一个 scala.html 模板文件。
我正在尝试添加一个 Google javascript 库以将图表添加到 Web 应用程序。
基本上需要用我自己的值填充以下函数:
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Date', 'Sales'],
['2004', 1000],
['2005', 1170],
['2006', 660],
['2007', 1030]
]);
所以我做了以下事情(这适用于 HTML 文件的其他部分,但不适用于 Javasript):
@for(run <- currentPage.getList) {
[@run.date.format("dd MMM yyyy"),@run.sales],
}
但是以 @ 符号为前缀的 Scala 代码在 Javascript 中不起作用。
谁能给点建议?
谢谢。
这是整个代码:
@main {
<h1 id="homeTitle">@Messages("runs.listRuns.title", currentPage.getTotalRowCount)</h1>
@if(flash.containsKey("success")) {
<div class="alert-message warning">
<strong>Done!</strong> @flash.get("success")
</div>
}
<!-- CHART -->
<html>
<head>
<script type="text/javascript"
src="https://www.google.com/jsapi?autoload={
'modules':[{
'name':'visualization',
'version':'1',
'packages':['corechart']
}]
}"></script>
<script type="text/javascript">
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Date', 'Success Percentage'],
@for(run <- currentPage.getList) {
[@run.runDate.format("dd MMM yyyy"), @run.successPercentage],
}
]);
var options = {
title: 'Engineless Performance Monitoring',
curveType: 'function',
legend: { position: 'bottom' }
};
var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="curve_chart" style="width: 80%; height: 500px"></div>
</body>
</html>
<!-- CHART -->
<div id="actions">
<form action="@link(0, "name")" method="GET">
<input type="search" id="searchbox" name="f" value="@currentFilter" placeholder="Filter by Run Name...">
<input type="submit" id="searchsubmit" value="Filter by Run Name" class="btn primary">
</form>
</div>
【问题讨论】:
-
你能发布整个通话(结合javascript和scala)吗? (它应该有效,我一直在这样做;))
-
您是否为此使用单独的 javascript 文件,然后将其包含在 html 中?
-
@Peanut 谢谢。我更新了原来的问题。
-
检查您的浏览器控制台!它不起作用,因为 JS 不接受最后一个逗号字符,也许你还会发现其他错误
-
检查你的源代码,检查它在那里打印的元素,以获取 graphData 值。
标签: javascript scala playframework playframework-2.0