2015-05-12 70 views
1

我有一个约30000行的mysql表。我必须将所有行放入DataTable中,并在每次加载表页时加载每个段(当您单击分页时)。我看到我可以在我的JS中使用deferLoading参数,但是当我使用它时,我的页面没有加载。正如你所看到的,我需要加载图片,所以我绝对有做内容的轻负载...DataTables:延迟加载无法正常工作

这里是我的HTML:

<table class="table table-striped table-bordered table-hover datatable products-datatable"> 
    <thead> 
     <tr> 
      <th></th> 
      <th><?=_("Product")?></th> 
      <th></th> 
     </tr> 
    </thead> 
    <tfoot> 
     <tr> 
      <th></th> 
      <th><?=_("Product")?></th> 
      <th></th> 
     </tr> 
    </tfoot> 
</table> 

这里是我的JS:

var table = $('.products-datatable').dataTable({ 
    "order": [[ 1, "asc" ]], 
    "processing": true, 
    "serverSide": true, 
    "deferLoading": 30000, 
    "ajax": { 
     url: location.protocol + '//' + location.hostname + '/ajax/products.php?action=list', 
     type: "POST" 
    }, 
    "columns": [ 
     { "data": "image", 
      "orderable": false, 
      "width": "80px" }, 
     { "data": "product" }, 
     { "data": "action", 
      "orderable": false, 
      "width": "20px", 
      "sClass": "class", 
     } 
    ] 
}); 

这里是我的AJAX:

$req = $pdo->prepare('SELECT product_id, name FROM products'); 

if ($req->execute()) { 

    if ($req->rowCount()) { 

     $result['draw'] = 1; 
     $result['recordsTotal'] = $req->rowCount(); 
     $result['recordsFiltered'] = 10; 
     $result['data'] = array(); 
     $result['DT_RowId'][] = array(); 

     while($row = $req->fetch()) { 

      if ($row['name']) { $name = $row['name']; } else { $name = "N/A"; } 

      $result['data'][] = array( "DT_RowId"  => $row['product_id'], 
             "DT_RowClass" => 'myclass', 
             "image"   => '<a href="' . HOSTNAME._("product").'/'.$row['product_id'] . '"><img src="' . HOSTNAME.'assets/img/products/' . $row['product_id'] . '.jpg" class="product_thumb"></a>', 
             "product"  => '<a href="' . HOSTNAME._("product").'/'.$row['product_id'] . '">' . $name . '</a>', 
             "action"  => "<a href=\"#\" class=\"button-delete\" id=\"" . $row['product_id'] . "\"><i class=\"fa fa-close fa-2x text-danger\"></i></a>" 
             ); 

     } 

    } 

} 

$req->closeCursor(); 

我敢肯定有东西,我错过了...... :-(

回答

0

我相信你不需要使用deferLoading受益于服务器端处理。

您当前的脚本只是返回所有记录,不会进行排序或筛选。您需要使用ssp.class.php(可在DataTables分发包中获得)或emran/ssp类来正确处理服务器上的AJAX请求。

DataTables库将在AJAX请求中发送startlength参数,指示需要哪部分数据,并且您的服务器端处理类将为您正确处理它。

有关更多信息,请参阅server-side processing的示例。

+0

非常感谢! –