0

我正在使用 PHP 为一组数据表生成 HTML 和 JS,每个数据表都需要单独初始化,以便我可以分别过滤不同的表。据我所知,我的代码生成正确,但似乎我的 Javascript 没有正常运行,因为我没有得到任何表初始化,并且我得到一个“TypeError:'undefined' is not a function (evalating '$ ('#datatable_0').datatable')" 日志中的错误。

这是我的 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)。

4

1 回答 1

1

尝试更改.datatable(...).dataTable(...).

JS 区分大小写 :)

于 2013-01-11T16:17:45.987 回答