[英]pagination in server side datatable not working (without changing the URL)
我有一個數據表,我的jquery如下,
$('#example').DataTable({
"lengthMenu": [[10,15,20,25,-1], [10,15,20,25,'All']],
"processing": true,
"serverSide": true,
ajax: {
url: 'url2',
dataSrc: 'data'
},
columns: [
{ data: 'column1' },
{ data: 'column2' },
{ data: 'column3' },
{ data: 'column4' },
{ data: 'column5' },
{ data: 'column6' }
]
});
這工作正常。 唯一的問題是,盡管它顯示了分頁鏈接,但它們不可單擊,並且所有行都顯示在首頁本身中。
例如 如果有100行,則會生成1-10個鏈接,但所有100條記錄都顯示在首頁上。
我已提及
https://datatables.net/examples/data_sources/server_side
我在這里做錯了什么?
謝謝你的建議
服務器端代碼-
$total_records = $this->model2->getTotal();
$query['results'] = $this->model1->get_Data();
$data = array('data' => $query['results'],
"draw" => (isset($_REQUEST["draw"]) ? $_REQUEST["draw"] : 0),
"recordsTotal" => $total_records,
"recordsFiltered" => $total_records
);
echo json_encode($data);
我想我知道我在做什么錯,當我在PHP代碼中打印$ _GET時,結果為空。 但是它應該具有極限值和偏移值。 如何在$ _GET中發送限價抵消?
您需要知道頁面上有多少條記錄,以及有多少條記錄。
例如,您的SQL將以OFFSET 30 LIMIT 10
結尾。
首先獲得總記錄。 頁面將通過您的URL進入,如果未設置,則默認為頁面1。
偏移量的計算如下(示例,第3頁):
$totalPages = ceil($totalRecords / $numPerPage); // round up the way.
$offset = ($page * $numPerPage) - $numPerPage; // (3 * 10 = 30) - 10 = offset 20
因此,您的SQL將使用OFFSET 20 LIMIT 10
獲取記錄
這就是您的server.php外觀。 認為您缺少一些代碼行:
<?php
$table = 'employees';
$primaryKey = 'id'; // Table's primary key
$columns = array(
array( 'db' => 'id', 'dt' => 0 ),
array( 'db' => 'first_name', 'dt' => 1 ),
array( 'db' => 'last_name', 'dt' => 2 ),
array( 'db' => 'position', 'dt' => 3 ),
array( 'db' => 'date', 'dt' => 4 ),
array( 'db' => 'updated', 'dt' => 5 ),
);
$sql_details = array(
'user' => 'username',
'pass' => 'password',
'db' => 'database',
'host' => 'localhost'
);
require( 'ssp.class.php' );
echo json_encode(
SSP::simple( $_POST, $sql_details, $table, $primaryKey, $columns )
);
?>
最終結果: https : //databasetable-net.000webhostapp.com/
這應該為您指明正確的方向。 認為可能也需要添加ssp.class.php文件,不確定是否有(可以在github上找到)。 希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.