我就是这样做的,
首次加载:
var pageSize = 100; // See, this is my default page size. You can change it.
$.ajax({
url: "http://localhost:66252/Home/Getoptions",
data:{ skip: 0, take:100 },
success: function(result) {
var total = result.length;
if (total > pageSize) { // pageSize is 100, we declared on top
var pageTotal = Math.ceil(total / pageSize);
var j;
for (var i = 0; i < pageTotal; i++)
{
j = i + 1;
//Here I'm creating li for all the pages. You can create any number of li then create a button called Next. Pass the value in Next button.
var pages = '<li class="_li"><a id="page_' + j + '" class="pageNumber" style="margin-left:0px;" href="javascript:;">' + (i + 1) + '</a></li>';
$('#_ul').append(pages); //This is my id of Ul from HTML
}
}
});
在页码上点击:
$(document).on("click", ".pageNumber", function () {
var value = $(this).attr('id').split('_')[1];
var skip = value == 1 ? 0 : (value * pageSize) - pageSize;
//Then call once again your ajax method here by passing skip value
$.ajax({
url: "http://localhost:66252/Home/Getoptions",
data:{ skip: skip, take:100 },
success: function(result) {
var total = result.length;
if (total > pageSize) {
var pageTotal = Math.ceil(total / pageSize);
var j;
for (var i = 0; i < pageTotal; i++)
{
j = i + 1;
//Here I'm creating li for all the pages. You can create any number of li then create a button called Next. Pass the value in Next button.
var pages = '<li class="_li"><a id="page_' + j + '"
class="pageNumber" style="margin-left:0px;"
href="javascript:;">' + (i + 1) + '</a></li>';
$('#_ul').append(pages); //This is my id of Ul from HTML
}
}
});
});
然后在你的Action Method中,你可以检索查询字符串的值,例如,
string _skip = Request.QueryString["skip"];
string _take = Request.QueryString["take"];
然后您可以在 Linq 或 Entity Framework 查询中添加 Skip 和 Take 方法,例如,
var result =
myContext.GetData.Skip(int.Parse(_skip)).Take(int.Parse(_take)).ToList();
return json(result,jsonRequestBehaviour.AllowGet);
注意:请根据您的要求相应地更改 CSS 或 HTML。比如你Ul的BG颜色,或者点击的Li颜色,或者enable disable li等等。
希望对你有帮助:)