bootstrap table表格插件之服務(wù)器端分頁實(shí)例代碼
Bootstrap Table是基于Bootstrap的輕量級(jí)表格插件,只需要簡單的配置就可以實(shí)現(xiàn)強(qiáng)大的支持固定表頭、單復(fù)選、排序、分頁、搜索以及自定義表頭等功能。
因公司的項(xiàng)目需要實(shí)現(xiàn)用戶管理的表格實(shí)現(xiàn),所以選用了bootstrap-table用于動(dòng)態(tài)獲取后臺(tái)的用戶數(shù)據(jù)顯示到前臺(tái)。
示例截圖:

客戶端代碼:
//引入的css文件 <link href="../public/static/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet" /> <link href="../public/static/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="external nofollow" rel="stylesheet"> //引入的js文件 <script src="../public/static/js/jquery.min.js"></script> <script src="../public/static/js/bootstrap.min.js"></script> <script src="../public/static/js/plugins/bootstrap-table/bootstrap-table.min.js"></script> <script src="../public/static/js/plugins/bootstrap-table/bootstrap-table-zh-CN.min.js"></script>
html文件代碼
<div class="panel panel-default"> <div class="panel-heading"> 查詢條件 </div> <div class="panel-body form-group" style="margin-bottom:0px;"> <label class="col-sm-1 control-label" style="text-align: right; margin-top:5px">姓名:</label> <div class="col-sm-2"> <input type="text" class="form-control" name="Name" id="search_name"/> </div> <label class="col-sm-1 control-label" style="text-align: right; margin-top:5px">手機(jī)號(hào):</label> <div class="col-sm-2"> <input type="text" class="form-control" name="Name" id="search_tel"/> </div> <div class="col-sm-1 col-sm-offset-4"> <button class="btn btn-primary" id="search_btn">查詢</button> </div> </div> </div> <table id="mytab" class="table table-hover"></table> <div id="toolbar" class="btn-group pull-right" style="margin-right: 20px;"> <button id="btn_edit" type="button" class="btn btn-default" style="display: none; border-radius: 0"> <span class="glyphicon glyphicon-pencil" aria-hidden="true" ></span>修改 </button> <button id="btn_delete" type="button" class="btn btn-default" style="display: none;"> <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>刪除 </button> <button id="btn_add" type="button" class="btn btn-default"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增 </button> </div>
js文件代碼:
//根據(jù)窗口調(diào)整表格高度
$(window).resize(function() {
$('#mytab').bootstrapTable('resetView', {
height: tableHeight()
})
})
//生成用戶數(shù)據(jù)
$('#mytab').bootstrapTable({
method: 'post',
contentType: "application/x-www-form-urlencoded",//必須要有?。。?!
url:"../index.php/admin/index/userManagement",//要請(qǐng)求數(shù)據(jù)的文件路徑
height:tableHeight(),//高度調(diào)整
toolbar: '#toolbar',//指定工具欄
striped: true, //是否顯示行間隔色
dataField: "res",//bootstrap table 可以前端分頁也可以后端分頁,這里
//我們使用的是后端分頁,后端分頁時(shí)需返回含有total:總記錄數(shù),這個(gè)鍵值好像是固定的
//rows: 記錄集合 鍵值可以修改 dataField 自己定義成自己想要的就好
pageNumber: 1, //初始化加載第一頁,默認(rèn)第一頁
pagination:true,//是否分頁
queryParamsType:'limit',//查詢參數(shù)組織方式
queryParams:queryParams,//請(qǐng)求服務(wù)器時(shí)所傳的參數(shù)
sidePagination:'server',//指定服務(wù)器端分頁
pageSize:10,//單頁記錄數(shù)
pageList:[5,10,20,30],//分頁步進(jìn)值
showRefresh:true,//刷新按鈕
showColumns:true,
clickToSelect: true,//是否啟用點(diǎn)擊選中行
toolbarAlign:'right',工具欄對(duì)齊方式
buttonsAlign:'right',//按鈕對(duì)齊方式
toolbar:'#toolbar',//指定工作欄
columns:[
{
title:'全選',
field:'select',
//復(fù)選框
checkbox:true,
width:25,
align:'center',
valign:'middle'
},
{
title:'ID',
field:'ID',
visible:false
},
{
title:'登錄名',
field:'LoginName',
sortable:true
},
{
title:'姓名',
field:'Name',
sortable:true
},
{
title:'手機(jī)號(hào)',
field:'Tel',
},
{
title:'郵箱',
field:'Email'
},
{
title:'注冊(cè)日期',
field:'CreateTime',
sortable:true
},
{
title:'狀態(tài)',
field:'Attribute',
align:'center',
//列數(shù)據(jù)格式化
formatter:operateFormatter
}
],
locale:'zh-CN',//中文支持,
responseHandler:function(res){
//在ajax獲取到數(shù)據(jù),渲染表格之前,修改數(shù)據(jù)源
return res;
}
})
//三個(gè)參數(shù),value代表該列的值
function operateFormatter(value,row,index){
if(value==2){
return '<i class="fa fa-lock" style="color:red"></i>'
}else if(value==1){
return '<i class="fa fa-unlock" style="color:green"></i>'
}else{
return '數(shù)據(jù)錯(cuò)誤'
}
}
//請(qǐng)求服務(wù)數(shù)據(jù)時(shí)所傳參數(shù)
function queryParams(params){
return{
//每頁多少條數(shù)據(jù)
pageSize: params.limit,
//請(qǐng)求第幾頁
pageIndex:params.pageNumber,
Name:$('#search_name').val(),
Tel:$('#search_tel').val()
}
}
//查詢按鈕事件
$('#search_btn').click(function(){
$('#mytab').bootstrapTable('refresh', {url: '../index.php/admin/index/userManagement'});
})
//tableHeight函數(shù)
function tableHeight(){
//可以根據(jù)自己頁面情況進(jìn)行調(diào)整
return $(window).height() -280;
}
傳入后臺(tái)的數(shù)據(jù):

后臺(tái)傳來的數(shù)據(jù)

只勾選一項(xiàng),可以修改刪除

勾選多項(xiàng)只能刪除

開發(fā)過程中遇到的問題 (分頁后重新搜素問題)
如果點(diǎn)擊到第二頁,我再搜索框中輸入搜索條件,點(diǎn)擊查詢按鈕,調(diào)用bootstrap table refresh()方法,pageIndex會(huì)從第二頁開始,應(yīng)該改為第一頁
網(wǎng)上大多數(shù)方法為 :
$(“#mytab”).bootstrapTable(‘destroy');先要將table銷毀,否則會(huì)保留上次加載的內(nèi)容
……//然后重新初使化表格,相當(dāng)于重新創(chuàng)建。
因?yàn)楦杏X太過麻煩,所以找了很久終于找到了簡單的解決方法
再點(diǎn)擊查詢按鈕時(shí)
$(‘#mytab').bootstrapTable(‘refreshOptions',{pageNumber:1,pageSize:10});//便可以了
代碼地址:https://github.com/hanxue10180/bootstrapTable
總結(jié)
以上所述是小編給大家介紹的bootstrap table表格插件之服務(wù)器端分頁實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- bootstrap table 服務(wù)器端分頁例子分享
- bootstrap-table后端分頁功能完整實(shí)例
- Bootstrap table分頁問題匯總
- 第一次動(dòng)手實(shí)現(xiàn)bootstrap table分頁效果
- bootstrap table插件的分頁與checkbox使用詳解
- BootStrap中Table分頁插件使用詳解
- BootStrap Table前臺(tái)和后臺(tái)分頁對(duì)JSON格式的要求
- bootstrap table分頁模板和獲取表中的ID方法
- 使用bootstraptable插件實(shí)現(xiàn)表格記錄的查詢、分頁、排序操作
- bootstrap-table實(shí)現(xiàn)服務(wù)器分頁的示例 (spring 后臺(tái))
- BootStrap Table后臺(tái)分頁時(shí)前臺(tái)刪除最后一頁所有數(shù)據(jù)refresh刷新后無數(shù)據(jù)問題
- Bootstrap table 服務(wù)器端分頁功能實(shí)現(xiàn)方法示例
相關(guān)文章
完美解決input[type=number]無法顯示非數(shù)字字符的問題
下面小編就為大家?guī)硪黄昝澜鉀Qinput[type=number]無法顯示非數(shù)字字符的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
利用JS測(cè)試目標(biāo)網(wǎng)站的打開響應(yīng)速度
本文簡單說明利用JS來測(cè)試目標(biāo)網(wǎng)站的打開響應(yīng)速度,方法簡單明了大家一看就明白并附上了腳本源碼2017-12-12
js實(shí)現(xiàn)帶進(jìn)度條提示的多視頻上傳功能
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)帶進(jìn)度條提示的多視頻上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
前端終止請(qǐng)求的3種方式總結(jié)(ajax、axios)
這篇文章主要給大家總結(jié)介紹了關(guān)于前端終止請(qǐng)求的3種方式,其中包括ajax、axios的相關(guān)資料, 取消請(qǐng)求在前端有時(shí)候會(huì)用到,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09
Ajax+FormData+javascript實(shí)現(xiàn)無刷新表單信息提交
在前端開發(fā)中ajax,formdata和js實(shí)現(xiàn)無刷新表單信息提交非常棒,接下來通過本文給大家介紹Ajax+FormData+javascript實(shí)現(xiàn)無刷新表單信息提交的相關(guān)資料,需要的朋友可以參考下2016-10-10
BootStrap智能表單實(shí)戰(zhàn)系列(八)表單配置json詳解
這篇文章主要介紹了BootStrap智能表單實(shí)戰(zhàn)系列(八)表單配置json詳解的相關(guān)資料,本章節(jié)屬于高級(jí)部分,介紹一些表單中的配置知識(shí),非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
在web中js實(shí)現(xiàn)類似excel的表格控件
這篇文章主要介紹了如何在web中實(shí)現(xiàn)類似excel的表格控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09

