问题描述
我正在使用 jQuery DataTable 在表格中显示大量数据.我在这样的 Ajax 请求中获取数据页面明智:
I am using jQuery DataTable to display huge amount of data in a table. I am getting data page wise on Ajax request like this:
var pageNo = 1;
$('#propertyTable').dataTable( {
"processing": true,
"serverSide": true,
"ajax": "${contextPath}/admin/getNextPageData/"+pageNo+"/"+10,
"columns": [
{ "data": "propertyId" },
{ "data": "propertyname" },
{ "data": "propertyType" },
{ "data": "hotProperties" },
{ "data": "address" },
{ "data": "state" },
{ "data": "beds" },
{ "data": "city" },
{ "data": "zipCode" }
],
"fnDrawCallback": function () {
pageNo = this.fnPagingInfo().iPage+1;
alert(pageNo); // this alerts correct page
}
} );
这里是弹簧控制器:
@RequestMapping(value="/getNextPageData/{pageNo}/{propertyPerPage}")
public @ResponseBody PropertyDatatableDto getNextPageData(@PathVariable Integer pageNo, @PathVariable Integer propertyPerPage) {
System.out.println("called");
System.out.println(pageNo); // this always prints 1, what i need is current pageNo here
PropertyDatatableDto datatableDto = new PropertyDatatableDto();
//datatableDto.setDraw(1);
datatableDto.setRecordsTotal(100);
datatableDto.setRecordsFiltered(100);
datatableDto.setData(adminService.getAllPropertyList());
return datatableDto;
}
问题是,当我更改表格中的页面时,它会在页面上提醒正确的 pageNo
(在 JavaScript 中),但在 spring 控制器中,我总是将初始值分配给变量 pageNo
而不是当前页码.
The problem is that when I change page in table it alerts correct pageNo
on page (in JavaScript), but in spring controller I am always getting the initial value assigned to the variable pageNo
and not the current page number.
如何将 pageNo
动态传递给 spring 控制器?任何帮助表示赞赏.
How do I pass pageNo
dynamically to spring controller? Any help is appreciated.
我像这样更新了 JavaScript:
I updated JavaScript like this:
var oSettings = $('#propertyTable').dataTable().fnSettings();
var currentPageIndex = Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1;
$('#propertyTable').dataTable({
"processing": true,
"serverSide": true,
"ajax": "${contextPath}/admin/getNextPageData/"+currentPageIndex+"/"+10,
"columns": [
{ "data": "propertyId" },
{ "data": "propertyname" },
{ "data": "propertyType" },
{ "data": "hotProperties" },
{ "data": "address" },
{ "data": "state" },
{ "data": "beds" },
{ "data": "city" },
{ "data": "zipCode" }
]
});
但它给了我一个错误:
DataTables 警告:table id=propertyTable - 无法重新初始化 DataTable.
推荐答案
DataTables 已经在请求中发送了参数 start
和 length
可以用来计算页码,参见服务器端处理.
DataTables already sends parameters start
and length
in the request that you can use to calculate page number, see Server-side processing.
如果还需要带页码的URL结构,可以使用下面的代码:
If you still need to have the URL structure with the page number, you can use the code below:
"ajax": {
"data": function(){
var info = $('#propertyTable').DataTable().page.info();
$('#propertyTable').DataTable().ajax.url(
"${contextPath}/admin/getNextPageData/"+(info.page + 1)+"/"+10
);
}
},
这篇关于如何在 Ajax 请求中发送当前页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!