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被包含在主文件中較高的