根据标题,
我的数据表只显示结果的前两页。
在iDisplayLength中编辑值会导致在单个页面中显示更多或更少的记录,但不会更改数据表仅显示前两页的事实...
这是数据表的初始化:
var datatable = datatableElement.DataTable({
"responsive": true,
"searching": false,
"processing": true,
"serverSide": true,
"iDisplayLength": 20,
"lengthChange": false,
"stateSave": false,
"aaSorting": [0, 'desc'],
"pagingType": "simple",
"info": false,
"columnDefs": [
{"orderable": false, "targets": [1, 2]}
],
"ajax": {
"url": siteData.ajaxurl,
"type": 'POST',
"data": function(data){
data.action = "transactions_history";
data.startDate = transactionStartDate.val();
data.endDate = transactionEndDate.val();
data.type = transactionType.val();
}
},
"language": {
"url": siteData.ajaxurl + "?action=datatable_internazionalization"
}
});
这是ajax功能:
public static function ajax_transactions_history()
{
$start = filter_input(INPUT_POST, 'start') ?: 0;
$length = filter_input(INPUT_POST, 'length') ?: 20;
$draw = intval(filter_input(INPUT_POST, 'draw'));
$startDate = filter_input(INPUT_POST, 'startDate') ?: '1/1/2010';
$endDate = filter_input(INPUT_POST, 'endDate') ?: date("n/j/Y");
$type = filter_input(INPUT_POST, 'type');
$order_post = filter_input(INPUT_POST, 'order');
$order = ($order_post['dir'] == 'desc') ? 'ascending' : 'descending';
// add 1 day in seconds to get records also for end date
$endTimestamp = strtotime($endDate) + 86410;
$endDate = date("n/j/Y", $endTimestamp);
$gs_result = Gs_Api::member_transaction_history($type, $startDate, $endDate, $order, $length, $start);
$transactions = array();
if ($type == 'deposit' && isset($gs_result->scriptData) && isset($gs_result->scriptData->deposit)) {
$transactions = $gs_result->scriptData->deposit;
} elseif ($type == 'withdrawal' && isset($gs_result->scriptData) && isset($gs_result->scriptData->withdrawal)) {
$transactions = $gs_result->scriptData->withdrawal;
}
$total = (count($transactions) == $length) ? $length + 1 : $length;
$result['draw'] = $draw;
$result['iTotalRecords'] = $total;
$result['iTotalDisplayRecords'] = $total;
$result['data'] = array();
foreach ($transactions as $transaction) {
$date = DateTime::createFromFormat ("D F j Y H:i:s", $transaction->time);
$result_row = array();
$result_row[] = $date->format('m-d-Y H:i:s');
$result_row[] = $transaction->amount;
$result_row[] = __('transaction_status_'.$transaction->status, core_plugin::I18N_DOMAIN);
$result['data'][] = $result_row;
}
wp_send_json($result);
}
正如您所看到的,我希望在服务器端处理的数据表中显示结果,每次调用记录20个记录并将它们放入单个页面。
每个数据表的页面只能有20条记录。
但它搞砸了。例如:如果我有60条记录,则表格仅显示前两页。
如果我尝试将iDisplayLength和$ length更改为10,则数据表应显示6页,每页10条记录,但它只显示两页,每页10条记录。
我究竟做错了什么?!
更新:问题似乎是$ result ['iTotalDisplayRecords'] = $ total;
如果我删除它,数据表会显示所有记录,但页面数量是无限的。由于记录总数为46,我尝试将其设置为50,数据表正确显示3页,每页20条记录(最后一条记录为6条记录)。但如果我设置了iTotalDisplayRecords = $ length,它会进入完整的bug模式,并将分页阻为两页.......