1.這個是bootstrap 中pagination的庫
2..NET後臺ToPagedList的dll
html
1.頁面主體
項目中添加一個view,叫HistoryCase
2.分頁部分,這是一個partial view
3.將分頁部分嵌入頁面主體,綁定對應model
前端
添加一個model,聲明分頁屬性ajax
public class PaginationModelBase { public string QueryString { get; set; } public int? PageIndex { get; set; } public int? PageSize { get; set; } public string Order { get; set; } } public class PaginationModel : PaginationModelBase { public int Type { get; set; } } public class HistoryPaginationModel : PaginationModelBase { public string Status { get; set; } public DateTime? StartTime { get; set; } public DateTime? EndTime { get; set; } }
這裏的分頁,預留了查詢時候須要的接口。查詢條件爲關鍵字(QueryString),狀態(Status),起始時間(End/Start Time)。bootstrap
初始化pagination,根據total count 和 page size,計算出 page count。後端
History.prototype.InitPagination = function (totalCount, isReInit, isReset) { var historyPage = this; var paginationHis = historyPage.$PaginationHis; var total = $('input[name="total-cnt"]').val(); var pageSize = 5; pageSize = parseInt(pageSize); if (totalCount != null) { total = parseInt(totalCount); } if (total == 0) { paginationHis.addClass("hidden"); } else { paginationHis.removeClass("hidden"); } if (isReset) { paginationHis.bootpag({ page: 1 }); } paginationHis.bootpag({ total: Math.ceil(total / pageSize), maxVisible: 10 }).on('page', function (event, num) { var ajaxUrl = "/Home/AjaxLoadCase"; var postData = historyPage.GetSearchFormData(); postData.Pagination = { PageIndex: num, PageSize: pageSize, Status: postData.Status, StartTime: postData.StartDate, EndTime: postData.EndDate } if (!isReInit || isReInit == undefined) { historyPage.LoadDataAjax(ajaxUrl, postData, false); } }); }
當點擊page num 的時候,觸發ajax請求。post
History.prototype.LoadDataAjax = function (ajaxUrl, postData, isRest) { var history = this; $.blockUI(); $.ajax(ajaxUrl, { dataType: 'html', data: postData, timeout: 12000, method: "POST", success: function (data) { $('input[name="total-cnt"]').remove(); var $caseTable = $('.histroy-case'); $caseTable.remove(); $('.hitory-msg').remove(); $(data).insertBefore(history.$PaginationHis); var totalCount = $('input[name="total-cnt"]').val(); history.InitPagination(totalCount, true, isRest); history.AlertSucc("Get case list succeed.") $.unblockUI(); }, error: function (error) { history.AlertError("Internal occurs error, please try again.") $.unblockUI(); }, complete: function () { $.unblockUI(); } }); }
邏輯爲,發送請求,請求成功,將原來的數據remove,將返回的數據從新綁定到對應元素上。this
注意每次要更新total count。prototype
這是第一頁,也是頁面第一次渲染時展現的頁面;插件
點擊第四頁,便可返回正確listcode