javascript - 数据表 : DeferLoading is not working correctly

标签 javascript jquery mysql datatables

我有一个大约有 30000 行的 mysql 表。我必须将所有行放在 DataTable 中,并在每次加载表格页面时加载每个段(当您单击分页时)。我看到我可以在我的 JS 中使用 deferLoading 参数,但是当我使用它时我的页面没有加载。如您所见,我必须加载图像,所以我绝对必须对内容进行少量加载...

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

我确定我错过了什么... :-(

最佳答案

我相信您不需要使用 deferLoading 来从服务器端处理中获益。

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

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

请查看 server-side processing 的示例获取更多信息。

关于javascript - 数据表 : DeferLoading is not working correctly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30198115/

相关文章:

javascript - 是否可以使用 .autocomplete() 的修改?

javascript - 如何拥有相同的重复 div 结构,但在每个 div 中解析不同的 xml 数据

javascript - 当得到swf地址后,如何用flashplayer再次显示flash?

javascript - 在 JavaScript 中显示/隐藏

javascript - 选中时单选按钮更改样式

mysql - 我想找到一个在 5 分钟内尝试了 3 次登录的用户。在 MySQL 5.7 中

python - 无法打开包含文件 : 'mysql.h'

mysql - 这个sql update语句有什么问题?

javascript - Element.insertAdjacentHTML() API 在 chrome 55.0.2883.87 中引发错误

javascript - 加载文档时为元素分配一个 id,以便在单击时可以执行不同的功能?