2013-02-10 74 views
1

我想用数组作为使用HtmlService和jQuery的Datatables插件的数据源。我无法传递数组。结果表不是渲染数组correcty - 在这个3 col表中,col1包含'1',col2包含','col3包含'2'我做错了什么?在jQuery中使用DataTables插件

function doGet() { 
var temp = HtmlService.createTemplateFromFile('example2'); 
temp.j = [1,2,3];    
return temp.evaluate(); 
} 

<html> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 

    <title>DataTables example</title> 

    <link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.1/css/jquery.dataTables.css"> 
    <script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"></script> 
    <script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.1/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 */ 
     <?= j ?> 

    ], 
    "aoColumns": [ 
     { "sTitle": "Engine" }, 
     { "sTitle": "Browser" }, 
     { "sTitle": "Platform" } 
    ] 
}); 
}); 
    </script> 
</head> 
<body> 
<div id="demo"></div> 

</body> 
</html> 

回答

1

一些想法。让我知道你最喜欢/最好的作品。你可以将你的[1,2,3]写成一个字符串,然后把它扔到你的模板中。就像这样:

temp.j = Utilities.jsonStringify([1,2,3]); 

<?!= j ?> 

注:!必要在这里,因为它迫使可以打印,是字符串。见'Force-Printing Scriptlets'。

或者您也可以通过这样的HTML模板数据的每个元素进行迭代。

[<? for (var i = 0; i < j.length; ++i) { ?> 
    <?!= j[i]?>, 
<? } ?>] 

注:方括号和逗号是必要的,因为j[i]只是返回每个元素的值。

+0

明白了 - 两者都很好,我可以使用二维数组进行工作。谢谢! – 159david 2013-02-10 03:08:13