当数据在初始页面加载时提供时,DataTables 可以与 GAS HTMLService 一起正常工作,但 GAS 的更好做法是“异步加载数据,而不是在模板中”。此外,DataTables 的服务器端处理为大型数据表提供了响应更快的 UI。我正在寻找满足这些建议的模式。我从一个基本的 DataTables 示例开始翻译成 GAS HTMLService 模板模型。
GS代码:
function doGet() {
Logger.log("Start");
var start = new Date();
// rt params
// var rtParameters = getRunTimeParameters(request);
// uses HtmlService to send page based on a template
var temp = HtmlService.createTemplateFromFile('chart');
// prepare data for initial page
Logger.log("TIME to return:"+ (new Date() -start));
return temp.evaluate();
}
function include(filename) {
// helper function to load html into template from sep files in project
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
HTML模板 - 图表
...
<body>
<h1>JQUERY DataTables Page Generated in Google Apps Script </h1>
<p>Includes sorting, paging and filtering by default.</p>
<p>Entire data table loaded in one hit.</p>
<div id="demo"></div>
<?!= include('JavaScript'); ?>
</body>
Javascript(内嵌数据表定义)
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>
<script type="text/javascript" charset="utf8">
$(document).ready(function() {
$('#demo').html( '<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>' );
$('#example').dataTable( {
"aaData": [
/* Reduced data set */
[ "Trident", "Internet Explorer 4.0", "Win 95+", 4, "X" ],
[ "Trident", "Internet Explorer 5.0", "Win 95+", 5, "C" ],
[ "Trident", "Internet Explorer 5.5", "Win 95+", 5.5, "A" ],
[ "Trident", "Internet Explorer 6.0", "Win 98+", 6, "A" ],
[ "Trident", "Internet Explorer 7.0", "Win XP SP2+", 7, "A" ],
[ "Gecko", "Firefox 1.5", "Win 98+ / OSX.2+", 1.8, "A" ],
[ "Gecko", "Firefox 2", "Win 98+ / OSX.2+", 1.8, "A" ],
[ "Gecko", "Firefox 3", "Win 2k+ / OSX.3+", 1.9, "A" ],
[ "Webkit", "Safari 1.2", "OSX.3", 125.5, "A" ],
[ "Webkit", "Safari 1.3", "OSX.3", 312.8, "A" ],
[ "Webkit", "Safari 2.0", "OSX.4+", 419.3, "A" ],
[ "Webkit", "Safari 3.0", "OSX.4+", 522.1, "A" ]
],
"aoColumns": [
{ "sTitle": "Engine" },
{ "sTitle": "Browser" },
{ "sTitle": "Platform" },
{ "sTitle": "Version", "sClass": "center" },
{ "sTitle": "Grade", "sClass": "center" }
]
} );
});
更一般地说:如何使用 GAS 实现 DataTables 服务器端处理。