我有一个使用 DataTables 的表格,它包含大量行,因此这会导致页面加载非常缓慢,因为我假设浏览器在显示页面之前等待表格填满。
我只想加载表格的一页(10 行),并且只在用户浏览表格时显示更多数据,显示加载标志也很棒。
我研究并听说过一个名为“deferRender”的 DataTables 函数,它应该可以满足我的需要,但我无法让它与我的表一起使用。
我的表有 8 列 + html 是使用 PHP 生成的,它根据文本文件中的数据构建表:
<?php
     $tdcount = 1; $numtd = 8; // number of cells per row
     $str = "<table id=\"table1\" class=\"table1 table table-striped table-bordered\">
                                     <thead>
                                     <th>1</th>
                                     <th>2</th>
                                     <th>3</th>
                                     <th>4</th>
                                     <th>5</th>
                                     <th>6</th>
                                     <th>7</th>
                                     <th>8</th>
                 </thead>
                                     <tbody>
 ";
     $f = fopen("tabledata.txt", "r");
     if ( $f === FALSE ) {
 exit;
    }
     while (!feof($f)) {
         $arrM = explode(",",fgets($f));
         $row = current ( $arrM );
         if ($tdcount == 1)
             $str .= "<tr>"; $str .= "<td>$row </td>";
         if ($tdcount == $numtd) {
             $str .= "</tr>";
             $tdcount = 1;
         } else {
             $tdcount++;
         }
     }
     if ($tdcount!= 1) {
         while ($tdcount <= $numtd) {
             $str .= "<td> </td>"; $tdcount++;
         } $str .= "</tr>";
     }
     $str .= "</tbody></table>";
     echo $str;
然后我使用以下代码将其转换为数据表:
<script>
        $(document).ready(function() {
        $('#table1').basictable({
          forceResponsive: false
          });
        $('#table1').DataTable( { "order": [[ 0, "desc" ]] } );
          });
</script>
我已阅读此处的说明:https : //datatables.net/examples/server_side/defer_loading.html 并知道我需要向 JS 添加参数:
"processing": true,
"serverSide": true,
"ajax": "scripts/server_processing.php",
"deferLoading": 57
并使用 server_processing 脚本,但是该示例仅显示了在连接到数据库时如何使用它,而不是在使用 php 从文本文件加载数据时使用它。
我怎样才能做到这一点?
