我有一个 API,它接受页码和页面大小作为参数来返回用户详细信息。 加载数据到jquery数据表。每次都需要将页码和大小传递给 API 以获取数据。如何获取页码并将其传递给 webmethod 并始终启用下一个按钮。因为当我第一次加载数据时,它只显示页码为 1 并且下一步按钮被禁用。
var tableUserDetails = $("#grdUser").DataTable({
processing: true,
filter: true,
orderMulti: false,
paging: true,
searching: true,
bFilter: true,
bsort: true,
bInfo: true,
pagingType: "simple",
columns: [{ "data": "Id" },
{ "data": "Name" },
{ "data": "userName" },
{ "data": "email" },
{ "data": "role" }
]
});
function getUsers() {
var info =tableUserDetails.page.info();
$.ajax({
data: '{pageNumber:' + info.page+1 + ', pageSize:' + 10 + '}',
type: "POST",
url: "MyPage.aspx/GetUsers",
contentType: Constants.ContentType,
error: function (XMLHttpRequest, textStatus, errorThrown) {
debugger;
alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
},
success: function (result) {
},
});
}
我认为您正在寻找这个:DataTables server side processing。您拥有的配置未部分加载数据。它假定您在设置数据集时拥有所有行。
$(document).ready(function() {
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "../server_side/scripts/server_processing.php"
} );
} );
而返回数据的格式应该是这样的:
{
"draw": 3,
"recordsTotal": 57,
"recordsFiltered": 57,
"data": [
[
"Airi",
"Satou",
"Accountant",
"Tokyo",
"28th Nov 08",
"$162,700"
],
/* etc */
类似 C# 的响应示例:
/// <summary>
/// Resultset to be JSON stringified and set back to client.
/// </summary>
[Serializable]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public class DataTableResultSet
{
/// <summary>Array of records. Each element of the array is itself an array of columns</summary>
public List<List<string>> data = new List<List<string>>();
/// <summary>value of draw parameter sent by client</summary>
public int draw;
/// <summary>filtered record count</summary>
public int recordsFiltered;
/// <summary>total record count in resultset</summary>
public int recordsTotal;
public string ToJSON()
{
return JsonConvert.SerializeObject(this);
}
}
$(document).ready(function () {
$('#mytable').DataTable({
processing: true,
serverSide: true,
ajax: {
data: '{pageNumber:' + info.page+1 + ', pageSize:' + 10 + '}',
type: "POST",
url: "MyPage.aspx/GetUsers",
contentType: Constants.ContentType,
error: function (XMLHttpRequest, textStatus, errorThrown) {
debugger;
alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
},
success: function (result) {
}
});
});