0
我使用PHP为一组数据表格生成HTML和JS,每个数据表格都需要单独初始化,以便可以分别过滤不同的表格。据我所知,我的代码生成正确,但似乎我的Javascript运行不正常,因为我没有得到任何表初始化,我得到一个“TypeError:'undefined'不是一个函数(评估'$ ('#datatable_0')。datatable')“日志中的错误。使用PHP初始化数据表格
这里是我的PHP代码缩短版($ schedule_options只是一个像9:00的时候阵列 - 10:30):
<script src="./js/jquery.dataTables.min.js"></script>
<script src="./js/TableTools.min.js"></script>
<script src="./js/ZeroClipboard.js"></script>
<script type="text/javascript" src="./js/check_in.js.php"></script>
<?php
$counter = 0;
foreach($schedule_options as $option)
{
echo '<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered datatable" id="datatable_' . $counter . '">
<thead>
<tr>
<th>ID</th>
<th>Student Name</th>
<th>Nickname</th>
<th>User Name</th>
<th>Season/Year</th>
<th>Age</th>
<th>Level</th>
<th>Class Time</th>
<th>Instructor</th>
<th>Size</th>
<th>Comments</th>
</tr>
</thead>
</table>';
$counter++;
}
这里是JS文件check_in.js。 php:
<?php
Header("content-type: application/x-javascript");
echo '$(document).ready(function() {';
$counter = 0;
foreach($schedule_options as $option)
{
echo 'var datatable_' . $counter . ' = $(\'#datatable_' . $counter . '\').datatable({
"sDom": "<\'row-fluid\'<\'span6\'T><\'span6\'f>r>t<\'row-fluid\'<\'span6\'i><\'span6\'p>>",
"sPaginationType": "bootstrap",
"oLanguage": {
"sLengthMenu": "_MENU_ records per page"
},
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "./datatables/students_table.php",
"fnDrawCallback" : function() {
$("[rel=popover]").popover();
},
"oTableTools":
{
"sRowSelect": "single",
"sSwfPath": "./includes/copy_csv_xls_pdf.swf",
"aButtons": [
"copy",
"csv",
"xls",
{
"sExtends": "pdf",
"sTitle": "HSS Students",
"sFileName": "HSS Students.pdf",
"sPdfMessage": "Season: ",
"sPdfOrientation": "landscape"
},
"print"]
}
});
';
$counter++;
}
感谢您的帮助。我相信我遇到的主要问题是由于Javascript的初始化顺序或其他原因。如果有帮助,我可以发布完整的生成代码(HTML和JS)。
你确定自己已经包含了jQuery吗?该错误消息似乎表明$未定义。 – glomad
是的,JQuery被包含在主文件中较高的