【发布时间】:2020-03-07 08:47:48
【问题描述】:
我按照 DataTable 文档执行如下,数据被重新渲染但分页无法正常工作。
当我点击dataTable和actula右下角的页码pageSize时,如何获得实际的pageNumber(table.page.info().page) (table.page.info().length) 当我在 ajax 调用之前更改数据表左上角的页面长度时。
示例 1:当我点击页码“1”时,我得到 -
Page Number 0 Page Size 10
但应该是的
Page Number 1 Page Size 10
示例 2:当我将页面长度“10”更改为“25”时,我得到 -
Page Number 0 Page Size 10
但应该是的
Page Number 0 Page Size 25
也就是说我落后了一步,请帮助....
这是我的 html
<table id="allScholarshipResult" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>Id</th>
<th>Scholarship Year</th>
<th>Scholarship Level</th>
<th>Total Candidate</th>
<th>Boy</th>
<th>Girl</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Id</th>
<th>Scholarship Year</th>
<th>Scholarship Level</th>
<th>Total Candidate</th>
<th>Boy</th>
<th>Girl</th>
</tr>
</tfoot>
</table>
这里是js
$(document).ready(function() {
var url = 'all';
var pageNumber;
var pageSize;
var table = $('#allScholarshipResult').DataTable({
"processing": true,
"serverSide": true,
"paging": true,
"searching": { "regex": true },
ajax: function ( data, callback, settings ) {
$.ajax({
url: url,
type: 'GET',
data: {
pageNumber: pageNumber,
pageSize: pageSize
},
success: function( response, textStatus, jQxhr ){
pageNumber = table.page.info().page;
pageSize= table.page.info().length;
console.log('Page Number '+pageNumber + ' Page Size ' + pageSize);
callback({
data: response.responseObject.data,
recordsTotal: response.responseObject.recordsTotal,
recordsFiltered: response.responseObject.recordsFiltered
});
},
error: function( jqXhr, textStatus, errorThrown ){
}
});
},
columns: [
{ data: "id" },
{ data: "examYear" },
{ data: "scholarshipLevelId" },
{ data: "candidateTotal" },
{ data: "candidateBoy" },
{ data: "candidateGirl" },
]
});
} );
从 API 返回的响应
{
"recordsTotal": 5,
"recordsFiltered": 5,
"data": [
{
"id": 76,
"examYear": 2020,
"scholarshipLevelId": 1,
"candidateTotal": 0,
"candidateBoy": 0,
"candidateGirl": 0
},
{
"id": 75,
"examYear": 2020,
"scholarshipLevelId": 1,
"candidateTotal": 0,
"candidateBoy": 0,
"candidateGirl": 0
},
{
"id": 74,
"examYear": 2019,
"scholarshipLevelId": 2,
"candidateTotal": 0,
"candidateBoy": 0,
"candidateGirl": 0
},
{
"id": 73,
"examYear": 2019,
"scholarshipLevelId": 1,
"candidateTotal": 0,
"candidateBoy": 0,
"candidateGirl": 0
},
{
"id": 72,
"examYear": 2020,
"scholarshipLevelId": 2,
"candidateTotal": 0,
"candidateBoy": 0,
"candidateGirl": 0
}
]
}
【问题讨论】:
标签: jquery datatable datatables-1.10