bootstrap、jQuery、Ajax資料分頁,jqueryajax
展示 JS代碼
var RecordCount;//全域變數 總條數var PageSize = 5;//全域變數 每頁顯示多少條資料//點擊查詢時,開始分頁function btnSearch_Query() { AjaxPage(1, PageSize);}//分頁方法function AjaxPage(CurPage, PageSize) { //清空資料顯示Table $('#SearchQuery_Table tbody tr').remove(); $('#pageCount').html('0'); $('#MainContent_AspNetPager_Msg').html(" <a disabled='disabled' style='margin-right:5px;'>← 上一頁</a><span class='cpb' style='margin-right:5px;'>1</span><a disabled='disabled' style='margin-right:5px;'>下一頁 →</a>"); $('#CurrentPageSize').html(CurPage);//頁 //名稱 var Name = $('#Search_NameContains').val(); //組合Json查詢條件,查詢分頁資料 (CurPage、CurPage為必要條件) var CacceyJson = '{"Name":"' + Name + '","CurPage":' + CurPage + ',"PageSize":' + PageSize + '}'; $.ajax({ type: 'POST', async: false, cache: false, url: "AjaxPage/PostAjax.aspx?z=SearchQueryByAjaxPage&a=" + Math.random(), dataType: 'json', data: escape(CacceyJson), success: function (data) { if (data == null) { $('#SearchQuery_Table tbody').html("<tr><td colspan='6' class='red'>未查詢到資料!</td></tr>"); } else if (data != null && data.msg != 'login_timeout') {//返回的資料群組合形式,然後將對象轉換JSON字串 //Dictionary<string, object> dic = new Dictionary<string, object>(); //dic.Add("RecordCount", pageArgs.RecordCount);//總條數 //dic.Add("Data", list); if (data["Data"] != null) { var str = ''; for (var i = 0; i < data["Data"].length; i++) { str += "<tr><td></td></tr>";//組合顯示資料 } $('#SearchQuery_Table tbody').html(str); RecordCount = data["RecordCount"]; $('#pageCount').html(RecordCount); //最大頁數 var maxpage = RecordCount % PageSize == 0 ? parseInt(RecordCount / PageSize) : (parseInt(RecordCount / PageSize) + 1); $('#MainContent_AspNetPager_Msg').html('');//分頁連結 var span = "<a style='margin-right: 5px; cursor: pointer;' href='javascript:void(0)' onclick='PageIndexClick(this)' id='TopPage' pageindex='0'>← 上一頁</a>"; var firstPage = 0; var pagecount = 0;//迴圈的次數 if (maxpage > 10) { pagecount = 10; } else { pagecount = maxpage; } if (CurPage > 10) { pagecount = CurPage; var firstPage = CurPage - 10; if (firstPage >= 1) { span += "<a style='margin-right: 5px; cursor: pointer;' href='javascript:void(0)' onclick='CurPageSizeClick(" + firstPage + ",this)' >...</a>"; } } for (var j = firstPage + 1; j < pagecount + 1; j++) { if (j == CurPage) { span += "<span class='cpb' style='margin-right: 5px; cursor: pointer;' onclick='CurPageSizeClick(" + j + ",this)'>" + j + "</span>"; } else { span += "<a style='margin-right: 5px; cursor: pointer;' href='javascript:void(0)' onclick='CurPageSizeClick(" + j + ",this)' >" + j + "</a>"; } } pagecount = pagecount + 1; if (maxpage > pagecount) { span += "<a style='margin-right: 5px; cursor: pointer;' href='javascript:void(0)' onclick='CurPageSizeClick(" + pagecount + ",this)' >...</a>"; } span += "<a style='margin-right: 5px; cursor: pointer;' href='javascript:void(0)' onclick='PageIndexClick(this)' id='NextPage' pageindex='0'>→ 下一頁</a>"; $('#MainContent_AspNetPager_Msg').html(span); } else { $('#SearchQuery_Table tbody').html("<tr><td colspan='6' class='red'>未查詢到資料!</td></tr>"); } } else if (data.msg == 'login_timeout') { LoginTimeout(); } } });}//首頁、上一頁、下一頁、尾頁點擊function PageIndexClick(obj) { //當前第幾頁 var CurrenPageSize = $.trim($('#CurrentPageSize').html()); if (CurrenPageSize != '') { CurrenPageSize = parseInt(CurrenPageSize); } //id var type = $(obj).attr('id'); //首頁 if (type == 'FirstPage') { CurrenPageSize = 1; AjaxPage(CurrenPageSize, PageSize); $('#CurrentPageSize').html('1'); } //上一頁 else if (type == 'TopPage') { if (CurrenPageSize > 1) { CurrenPageSize = CurrenPageSize - 1; } else { CurrenPageSize = 1; } AjaxPage(CurrenPageSize, PageSize); $('#CurrentPageSize').html(CurrenPageSize); } //下一頁 else if (type == 'NextPage') { var size = CurrenPageSize + 1; var maxpage = RecordCount % PageSize == 0 ? parseInt(RecordCount / PageSize) : (parseInt(RecordCount / PageSize) + 1); if (size <= maxpage) { CurrenPageSize = CurrenPageSize + 1 } AjaxPage(CurrenPageSize, PageSize); $('#CurrentPageSize').html(CurrenPageSize); } //尾頁 else if (type == 'LastPage') { CurrenPageSize = (RecordCount % PageSize == 0 ? parseInt(RecordCount / PageSize) : parseInt(RecordCount / PageSize) + 1); AjaxPage(CurrenPageSize, PageSize); $('#CurrentPageSize').html(CurrenPageSize); }}//頁數點擊 function CurPageSizeClick(CurPage, obj) { $('#CurrentPageSize').html(CurPage); AjaxPage(CurPage, PageSize);}
HTML代碼
<div class="row"> <div class="col-lg-12 col-sm-12 col-xs-12 col-xxs-12"> <table class="table table-striped table-hover table-bordered bootstrap-datatable " id="SearchQuery_Table"> <thead> <tr style="height: 25px;"> <td>條碼</td> <td>名稱</td> <td>上櫃價</td> <td>貨號</td> </tr> </thead> <tbody> </tbody> </table> </div></div><div class="row" style="margin-top: 15px;"> <div class="col-lg-12 col-sm-12 col-xs-12 col-xxs-12"> <div style="font-size: 14px;">共<label style="color: #20A8D8; font-size: 14px;" id="pageCount">0</label>條記錄</div> </div></div><div class="row"> <div class="col-lg-12 col-sm-12 col-xs-12 col-xxs-12"> <div id="MainContent_AspNetPager_Msg" class="paginator"> </div> <div class="hidden" id="CurrentPageSize">1</div> </div> </div>