这是我的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个结果的任何想法?
答案 0 :(得分:2)
您是否选中了limit
关键字?
SELECT * FROM customers LIMIT 0,10
并且一般limit
会与ORDER BY some_column DESC
结合使用以提供更多感官(例如,按时间或按ID)。
<强>更新强>:
进行分页,只需在切换页面时传入不同的参数:
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
答案 1 :(得分:0)
在数据表中使用pageLength
设置 - 例如
$('#myTable').dataTable( {
"pageLength": 10
});
这将输出10行
答案 2 :(得分:0)
好的,我找到了解决问题的方法。
这是我的新数据数组:
$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"
}
]
});