jquery dataTable 后臺(tái)加載數(shù)據(jù)并分頁(yè)實(shí)例代碼
使用 dataTable后臺(tái)加載數(shù)據(jù)并分頁(yè)。網(wǎng)上版本很多,但很多都是不能用或者不詳細(xì)的,這里是已經(jīng)驗(yàn)證過(guò)的。
引用 js文件
<script src="static/ace/js/jquery-2.0.3.min.js"></script>
<script src="static/ace/js/jquery.dataTables.min.js"></script> <script src="static/ace/js/jquery.dataTables.bootstrap.js"></script>
添加一個(gè)table 標(biāo)簽,<tbody></tbody> 可以不用,可以動(dòng)態(tài)加載
<table id="sample-table-2" class="table table-striped table-bordered table-hover"> <thead> <tr> <th class="center"><label> <input type="checkbox" class="ace" /> <span class="lbl"></span> </label> </th> <th>名稱</th> <th>apiKey</th> <th>secretKey</th> <th><i class="icon-time bigger-110 hidden-480"></i> 創(chuàng)建時(shí)間</th> <th class="hidden-480">Status</th> <th>操作</th> </tr> </thead> </table>
關(guān)鍵的JS代碼:
<script type="text/javascript"> jQuery(function($) { //初始化table var oTable1 = $('#sample-table-2') .dataTable( { "bPaginate" : true,//分頁(yè)工具條顯示 //"sPaginationType" : "full_numbers",//分頁(yè)工具條樣式 "bStateSave" : true, //是否打開客戶端狀態(tài)記錄功能,此功能在ajax刷新紀(jì)錄的時(shí)候不會(huì)將個(gè)性化設(shè)定回復(fù)為初始化狀態(tài) "bScrollCollapse" : true, //當(dāng)顯示的數(shù)據(jù)不足以支撐表格的默認(rèn)的高度 "bLengthChange" : true, //每頁(yè)顯示的記錄數(shù) "bFilter" : false, //搜索欄 "bSort" : true, //是否支持排序功能 "bInfo" : true, //顯示表格信息 "bAutoWidth" : true, //自適應(yīng)寬度 "bJQueryUI" : false,//是否開啟主題 "bDestroy" : true, "bProcessing" : true, //開啟讀取服務(wù)器數(shù)據(jù)時(shí)顯示正在加載中……特別是大數(shù)據(jù)量的時(shí)候,開啟此功能比較好 "bServerSide" : true,//服務(wù)器處理分頁(yè),默認(rèn)是false,需要服務(wù)器處理,必須true "sAjaxDataProp" : "aData",//是服務(wù)器分頁(yè)的標(biāo)志,必須有 "sAjaxSource" : "${basePath}pushEntity/getTableData",//通過(guò)ajax實(shí)現(xiàn)分頁(yè)的url路徑。 "aoColumns" : [//初始化要顯示的列 { "mDataProp" : "id",//獲取列數(shù)據(jù),跟服務(wù)器返回字段一致 "sClass" : "center",//顯示樣式 "mRender" : function(data, type, full) {//返回自定義的樣式 return "<label><input type='checkbox' class='ace' /><span class='lbl'></span></label>" } }, { "mDataProp" : "appName" }, { "mDataProp" : "apiKey" }, { "mDataProp" : "secretKey" }, { "mDataProp" : "createTime", "mRender" : function(data, type, full) { return new Date(data)//處理時(shí)間顯示 .toLocaleString(); } }, { "mDataProp" : "createTime", "mRender" : function(data, type, full) { return "<span class='label label-sm label-info arrowed arrowed-righ'>Sold</span>" } }, { "mDataProp" : "createTime", "mRender" : function(data, type, full) { return "<div class='visible-md visible-lg hidden-sm hidden-xs action-buttons'><a class='blue' href='#'><i class='icon-zoom-in bigger-130'></i></a><a class='green' href='#'><i class='icon-pencil bigger-130'></i></a><a class='red' href='#'><i class='icon-trash bigger-130'></i></a></div>" } } ], "aoColumnDefs" : [ {//用來(lái)設(shè)置列一些特殊列的屬性 "bSortable" : false, "aTargets" : [ 0 ] //第一列不排序 }, { "bSortable" : false, "aTargets" : [ 5 ] }, { "bSortable" : false, "aTargets" : [ 6 ] } ], "oLanguage" : {//語(yǔ)言設(shè)置 "sProcessing" : "處理中...", "sLengthMenu" : "顯示 _MENU_ 項(xiàng)結(jié)果", "sZeroRecords" : "沒(méi)有匹配結(jié)果", "sInfo" : "顯示第 _START_ 至 _END_ 項(xiàng)結(jié)果,共 _TOTAL_ 項(xiàng)", "sInfoEmpty" : "顯示第 0 至 0 項(xiàng)結(jié)果,共 0 項(xiàng)", "sInfoFiltered" : "(由 _MAX_ 項(xiàng)結(jié)果過(guò)濾)", "sInfoPostFix" : "", "sSearch" : "搜索:", "sUrl" : "", "sEmptyTable" : "表中數(shù)據(jù)為空", "sLoadingRecords" : "載入中...", "sInfoThousands" : ",", "oPaginate" : { "sFirst" : "首頁(yè)", "sPrevious" : "上頁(yè)", "sNext" : "下頁(yè)", "sLast" : "末頁(yè)" }, "oAria" : { "sSortAscending" : ": 以升序排列此列", "sSortDescending" : ": 以降序排列此列" } } }); //全選 $('table th input:checkbox').on( 'click', function() { var that = this; $(this).closest('table').find( 'tr > td:first-child input:checkbox').each( function() { this.checked = that.checked; $(this).closest('tr').toggleClass('selected'); }); }); }); </script>
后臺(tái)代碼:
// 獲取前端過(guò)來(lái)的參數(shù),下面三個(gè)參數(shù)是 dataTable默認(rèn)的,不要隨便更改 Integer sEcho = Integer.valueOf(params.get("sEcho"));// 記錄操作的次數(shù) 每次加1 Integer iDisplayStart = Integer.valueOf(params.get("iDisplayStart"));// 起始 Integer iDisplayLength = Integer.valueOf(params.get("iDisplayLength"));// 每頁(yè)顯示的size Map<String, Object> map = new HashMap<String, Object>(); try { // 查詢數(shù)據(jù),分頁(yè)的話我這邊使用的是 PageHelper,這邊不介紹了 PagedResult<PushEntity> list = pushEntityService.findByUserId( pushUser.getId(), iDisplayStart, iDisplayLength); // 為操作次數(shù)加1,必須這樣做 int initEcho = sEcho + 1;
//返回參數(shù)也是固定的 map.put("sEcho", initEcho); map.put("iTotalRecords", list.getTotal());//數(shù)據(jù)總條數(shù) map.put("iTotalDisplayRecords", list.getTotal());//顯示的條數(shù) map.put("aData", list.getDataList());//數(shù)據(jù)集合 } catch (Exception e) { e.printStackTrace(); } return map;
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jQuery插件DataTables分頁(yè)開發(fā)心得體會(huì)
- Asp.net MVC 中利用jquery datatables 實(shí)現(xiàn)數(shù)據(jù)分頁(yè)顯示功能
- jquery DataTable實(shí)現(xiàn)前后臺(tái)動(dòng)態(tài)分頁(yè)
- jquery datatable服務(wù)端分頁(yè)
- ASP.NET MVC+EF在服務(wù)端分頁(yè)使用jqGrid以及jquery Datatables的注意事項(xiàng)
- jQuery DataTables插件自定義Ajax分頁(yè)實(shí)例解析
- JQuery 構(gòu)建客戶/服務(wù)分離的鏈接模型中Table分頁(yè)代碼效率初探
- jQuery實(shí)現(xiàn)Table分頁(yè)效果
相關(guān)文章
jquery 彈出層注冊(cè)頁(yè)面等(asp.net后臺(tái))
jquery 彈出層注冊(cè)頁(yè)面,盡力提高用戶體驗(yàn),吸引用戶注冊(cè)。2010-06-06使用jquery實(shí)現(xiàn)圖文切換效果另加特效
白天活干完,弄個(gè)jquery仿凡客誠(chéng)品圖片切換的效果;以前寫的不是很好,今天重新做個(gè)jquery特效,其實(shí)很簡(jiǎn)單,感興趣的朋友可以了解下哦,希望本文對(duì)你有幫助2013-01-01jquery html動(dòng)態(tài)生成select標(biāo)簽出問(wèn)題的解決方法
用jquery,json從后臺(tái)獲取一個(gè)列表,然后用一個(gè)動(dòng)態(tài)生成的select標(biāo)簽顯示出來(lái),結(jié)果出現(xiàn)錯(cuò)誤,下面為大家分享個(gè)不錯(cuò)的解決方法,喜歡的朋友可以參考下2013-11-11Jquery實(shí)現(xiàn)點(diǎn)擊切換圖片并隱藏顯示內(nèi)容(2種方法實(shí)現(xiàn))
電腦屏幕大小是固定的,那么如何在有限的空間放更多的內(nèi)容呢,jquery的tab切換式瀏覽卻可以解決這個(gè)問(wèn)題2013-04-04jQuery+css實(shí)現(xiàn)的tab切換標(biāo)簽(兼容各瀏覽器)
這篇文章主要介紹了jQuery+css實(shí)現(xiàn)的tab切換標(biāo)簽,可兼容各瀏覽器,涉及jQuery響應(yīng)鼠標(biāo)事件動(dòng)態(tài)操作頁(yè)面元素的相關(guān)技巧,需要的朋友可以參考下2016-01-01jQuery實(shí)現(xiàn)表格行上下移動(dòng)和置頂效果
本文給大家分享的是一款由jQuery實(shí)現(xiàn)的表格行上下移動(dòng)以及置頂效果的代碼,非常的簡(jiǎn)單實(shí)用,這里給出了核心代碼,有需要的小伙伴可以參考下。2015-06-06