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實(shí)現(xiàn)圖片上傳之前預(yù)覽的方法
這篇文章主要介紹了jquery實(shí)現(xiàn)圖片上傳之前預(yù)覽的方法,涉及jquery針對(duì)圖片及頁(yè)面元素的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
jquery實(shí)現(xiàn)checkbox全選全不選的簡(jiǎn)單實(shí)例
本篇文章主要是對(duì)jquery實(shí)現(xiàn)checkbox全選全不選的簡(jiǎn)單實(shí)例進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12
jquery仿京東導(dǎo)航/仿淘寶商城左側(cè)分類導(dǎo)航下拉菜單效果
jquery實(shí)現(xiàn)下拉菜單效果,jquery寫的仿京東導(dǎo)航菜單,一個(gè)經(jīng)典的左側(cè)多級(jí)導(dǎo)航菜單,學(xué)會(huì)了可以任意改變布局,感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-04-04
jquery插件制作 提示框插件實(shí)現(xiàn)代碼
今天我們介紹的是提示框插件tooltip的制作,其中還會(huì)介紹到自定義選擇器插件的開發(fā)2012-08-08
寫得不錯(cuò)的jquery table鼠標(biāo)經(jīng)過(guò)變色代碼
鼠標(biāo)經(jīng)過(guò)table變色的效果,想必大家都有遇到過(guò)吧,其實(shí)實(shí)現(xiàn)并不難,在本文為大家詳細(xì)介紹下jquery是如何實(shí)現(xiàn)的,感興趣的朋友可以參看下2013-09-09
jQuery獲得包含margin的outerWidth和outerHeight的方法
這篇文章主要介紹了jQuery獲得包含margin的outerWidth和outerHeight的方法,涉及jQuery中outerWidth及outerHeight方法的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
jQuery.Form實(shí)現(xiàn)Ajax上傳文件同時(shí)設(shè)置headers的方法
這篇文章主要介紹了jQuery.Form實(shí)現(xiàn)Ajax上傳文件同時(shí)設(shè)置headers的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06
jQuery綁定點(diǎn)擊事件與改變事件的方式總結(jié)及多個(gè)元素綁定多個(gè)事件
我們一說(shuō)到j(luò)Query中對(duì)于元素的修改,就是元素的內(nèi)容、屬性、樣式的修改,下面這篇文章主要給大家介紹了關(guān)于jQuery綁定點(diǎn)擊事件與改變事件的方式總結(jié)及多個(gè)元素綁定多個(gè)事件的相關(guān)資料,需要的朋友可以參考下2022-12-12

