【问题标题】:Datatables returns all rows instead 10数据表返回所有行而不是 10
【发布时间】:2015-05-05 12:30:03
【问题描述】:

这是我的 AJAX:

$('.customers-datatable').dataTable( {
    "order": [[ 0, "asc" ]],
    "processing": true,
    "serverSide": true,
    "ajax": {
        url: 'ajax/customers.php?action=list',
        type: "POST"
    },
    "columns": [
        null,
        null,
        null,
        null,
        null,
        { "orderable": false, "width": "20px" }
    ]
});

这是我的 PHP/MySql :

$req = $pdo->prepare('SELECT * FROM customers');

$req->execute();

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

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

    $result['data'][] = array($row['lastname'] . ' ' . $row['firstname'], $row['zipcode'], $row['city'], $row['email'], $row['telephone'], "");

}

$req->closeCursor();

所以,我得到了整个列表,而不是 10 个元素。这是渲染的预览:

知道如何将表格限制为 10 个结果吗?

【问题讨论】:

    标签: php jquery mysql ajax datatables


    【解决方案1】:

    你检查过limit关键字吗?

    SELECT * FROM customers LIMIT 0,10

    通常limit 会与ORDER BY some_column DESC 结合以赋予它更多意义(例如,按时间或按身份)。

    更新

    做分页,只需要在切换页面时传入不同的参数即可:

    SELECT * FROM customers LIMIT 0,10 // 1-10 rows for page 1 
    SELECT * FROM customers LIMIT 10,10 // 11-20 rows for page 2
    SELECT * FROM customers LIMIT 20,10 // 21-30 rows for page 3
    .... and so on
    

    【讨论】:

    • 此查询将因分页而失败。
    • @XavierC。你对代码中的分页一无所知
    • @XavierC。我刚刚用分页的一些想法更新了答案
    • 我知道如何使用 LIMIT 参数来限制结果的数量,但除非我错了,否则使用 datatables jquery 脚本我不需要使用它,因为它会自己完成这项工作。
    【解决方案2】:

    在数据表中使用pageLength 设置——比如

    $('#myTable').dataTable( {
        "pageLength": 10 
    });
    

    这将输出 10 行

    【讨论】:

    • 好的,你能创建一个小提琴,这样我们就可以看到发生了什么 - 控制台中是否有任何错误?
    【解决方案3】:

    好的,我找到了解决问题的方法。

    这是我的新数据数组:

    $result['data'][] = array(  "name"      =>  $row['lastname'] . ' ' . $row['firstname'],
                                "zipcode"   =>  $row['zipcode'],
                                "city"      =>  $row['city'],
                                "email"     =>  $row['email'],
                                "telephone" =>  $row['telephone'],
                                "action"    =>  "<a href=\"#\" class=\"button-delete\" id=\"" . $row['customer_id'] . "\"><i class=\"fa fa-close fa-2x text-danger\"></i></a>"
                                );
    

    这是我的 ajax 调用(我已将 serverSide 设置为 false,因为它破坏了分页):

    $('.customers-datatable').dataTable( {
        "order": [[ 0, "asc" ]],
        //responsive: true,
        "processing": true,
        "serverSide": false,
        "ajax": {
            url: 'ajax/customers.php?action=list',
            type: "POST"
        },
        "columns": [
            { "data": "name" },
            { "data": "zipcode" },
            { "data": "city" },
            { "data": "email" },
            { "data": "telephone" },
            { "data": "action",
              "orderable": "false",
              "width": "20px"
            }
        ]
    });
    

    【讨论】:

    • 您应该阅读服务器端数据表发送的startlength参数(您可以使用var_dump($_POST)检查所有发送的参数)进行分页。服务器应该负责查询数据库(分页、搜索、排序……),否则你不会从数据表提供的所有选项/灵活性中受益。
    猜你喜欢
    • 2018-05-10
    • 1970-01-01
    • 2022-07-15
    • 1970-01-01
    • 2016-01-31
    • 2021-12-01
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多