官网介绍:
基于 Bootstrap 的 jQuery 表格插件,通过简单的设置,就可以拥有强大的单选、多选、排序、分页,以及编辑、导出、过滤(扩展)等等的功能。
主要功能
支持 Bootstrap 3 和 Bootstrap 2
自适应界面
固定表头
非常丰富的配置参数
直接通过标签使用
显示/隐藏列
显示/隐藏表头
通过 AJAX 获取 JSON 格式的数据
支持排序
格式化表格
支持单选或者多选
强大的分页功能
支持卡片视图
支持多语言
支持插件
优点
学习成本较低,配置简单,文档齐全
跟Bootstrap无缝衔接,整体风格一致,也便于二次开发
开发者活跃,Github定期维护
使用bootstrap table可以很方便的开发后台表格,对数据进行异步更新,编辑。下面就来介绍一下bootstrap table的详细使用方法:
因为之前在官网也找了很久的教程,源码感觉隐藏的比较隐秘,其他扩展功能也很难找到,其实都在:
http://issues.wenzhixin.net.cn/bootstrap-table/index.html
这里面,点击上面的tab可以找到具体的功能实现(特别是extension和issues,之前以为issues不是教程,结果点进去才知道):
最简单的异步表单
开始使用
1、在网页的head标签里插入下面的代码
<!– 引入bootstrap样式 –>
<link href=”
https://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css
” rel=”stylesheet”>
<!– 引入bootstrap-table样式 –>
<link href=”
https://cdn.bootcss.com/bootstrap-table/1.11.1/bootstrap-table.min.css
” rel=”stylesheet”>
<!– jquery –>
<script src=”
https://cdn.bootcss.com/jquery/2.2.3/jquery.min.js
“></script>
<script src=”
https://cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js
“></script>
<!– bootstrap-table.min.js –>
<script src=”
https://cdn.bootcss.com/bootstrap-table/1.11.1/bootstrap-table.min.js
“></script>
<!– 引入中文语言包 –>
<script src=”
https://cdn.bootcss.com/bootstrap-table/1.11.1/locale/bootstrap-table-zh-CN.min.js
“></script>
2、在页面body代码块里定义表格展示的区域
<table id=”table”></table>
3.javascript
/*bootstrap table*/
$(‘#table’).bootstrapTable({
url:”/wadmin/permission/doRuleList”,//请求数据url
queryParams: function (params) {
return {
offset: params.offset, //页码
limit: params.limit, //页面大小
search : params.search, //搜索
order : params.order, //排序
ordername : params.sort, //排序
};
},
showHeader : true,
showColumns : true,
showRefresh : true,
pagination: true,//分页
sidePagination : ‘server’,//服务器端分页
pageNumber : 1,
pageList: [5, 10, 20, 50],//分页步进值
search: true,//显示搜索框
//表格的列
columns: [
{
field: ‘id’,//域值
title: ‘规则ID’,//标题
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘5%’,
},
{
field: ‘name’,//域值
title: ‘唯一英文标识’,//标题
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘30%’,
editable:true,
},
{
field: ‘title’,//域值
title: ‘中文描述’,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘35%’,
editable:true,
},
{
field: ‘status’,//域值
title: ‘状态’,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘20%’,
formatter : function (value, row, index) {
if (row[‘status’] === 1) {
return ‘正常’;
}
if (row[‘status’] === 0) {
return ‘禁用’;
}
return value;
}
}
]
});
上面代码中,除了url和下面的column需要动态变之外,其他的基本上都是固定的,可以复制粘贴到处使用。因为规则的固定,所以后台的规则也是比较固定的。
3.thinkPHP5.0实现后台(主要是返回total和rows)
controller
public function doRoleRuleList() {
$id = input(“id”);
if (!$id) return “”;
$limit = input(“limit”);
$offset = input(“offset”);
$search = input(“search”);
$order = input(“order”, “desc”);
$ordername = input(“ordername”);
if (!$ordername) {
$ordername = ‘id’;
$order = ‘desc’;
}
$perModel = new PermissionModel();
$rs = $perModel->doRoleRuleList($id, $offset, $limit, $search, $order, $ordername);
return json($rs);
}
model
public function doRoleRuleList($id, $offset, $limit, $search, $order, $ordername) {
$total = Db::connect(“dbUser”)->table(“auth_rule”)->where([‘status’=>[‘<>’, 0]])->count();
$auth_group = Db::connect(“dbUser”)->table(“auth_group”)->field(“id,rules”)->where(“id”, $id)->find();
$rules =”-1″;
if ($auth_group && $auth_group[‘rules’]) $rules = $auth_group[‘rules’];
$rows = Db::connect(“dbUser”)->table(“auth_rule”)
->field([‘id’,’name’,’title’,”if(id in ({$rules}), 1, 0)”=>’selected’])
->where([
‘status’=>[‘<>’, 0],
// ‘name|title’ => [‘like’, “%{$search}%”]
])
->order($ordername.” “.$order)
->select();
return [‘total’=>$total,’rows’=>$rows];
}
说明图:
复杂表格(行内编辑(编辑文字,下拉选择),样式改变,自定义图标,文件上传),因为js比较复杂,这里先列出代码功能的说明:
1.显示详情使用如下代码:
表格参数中添加代码
detailView : true,
detailFormatter : function (index, row) {
var image = ‘<div class=”photos”>’
+'<a target=”_blank” href=”‘+row[‘jumpUrl’]+'”><img alt=”image” class=”feed-photo” src=”‘+row[‘picUrl’]+'”></a>’
+'</div>’;
return image;
}
可以在detailFormatter进行代码的格式化,以字符串的形式返回即可,实现效果如下:
2.表格列内容的格式化
列参数中添加代码
formatter : function (value, row, index) {
return “<img style=’width: 50px;height: 50px;’ src='”+value+”‘ alt=”>”
}
效果如下:
3 . 表格的样式自定义
列参数中添加带代码
cellStyle : function (value, row, index) {
return {
css: {
“max-width”: “300px”,
“word-wrap”: “break-word”,
“word-break”: “normal”
}
};
}
4 . 新增数据
在js文件中添加如下代码:
$(“#btn_add”).click(function () {
$.ajax({
type : “POST”,
url : “/wadmin/ad/addAd”,
dataType : ‘JSON’,
success : function (data) {
if (data.result != 0) {
toastr.info(“info”, data.message);
return ;
}
toastr.success(“success”, ‘标签’);
$(“#table”).bootstrapTable(‘insertRow’, {index:0, row:data.data});
}
});
});
绑定的按钮是toolbar里面的,点击如果进行ajax请求,再根据请求的结果自行判断添加
5.表格中列格式化成图标并监听事件
6.文件上传(基于5)
列参数中添加如下代码
{
field: ‘operate’,
title: ‘操作’,
align: ‘center’,
events: operateEvents,
width : ‘25%’,
formatter: operateFormatter
}
operateFormatter(这里注意需要添加a标签包住图标,并添加class)
function operateFormatter(value, row, index) {
return [
‘<a class=”using” href=”javascript:void(0)” title=”Remove”>’,
‘<i class=”fa fa-check”></i>’,
‘</a> ’,
‘<a class=”upload” style=”cursor: pointer” href=”javascript:void(0)” title=”Upload”>’,
‘<label style=”cursor: pointer” for=”‘+row[‘adId’]+'”>’,
‘<i class=”fa fa-upload”></i>’,
‘<input type=”file” name=”adUpload” style=”display: none;” class=”adUpload” id=”‘+row[‘adId’]+'” >’,
‘</label>’,
‘</a> ’,
‘<a class=”remove” href=”javascript:void(0)” title=”Using”>’,
‘<i class=”fa fa-times”></i>’,
‘</a>’,
].join(”);
}
operateEvents(监听事件,注意这里是window.operateEvents)
window.operateEvents = {
‘click .remove’: function (e, value, row, index) {
$.ajax({
type : “POST”,
url : “/wadmin/ad/deleteAd”,
data : {
adId : row[‘adId’]
},
dataType : ‘JSON’,
success : function (data) {
if (data.result != 0) {
toastr.info(“info”, data.message);
return ;
}
toastr.success(“success”, ‘删除成功’);
$(“#table”).bootstrapTable(‘remove’, {
field: ‘adId’,
values: [row[‘adId’]]
});
}
});
return false;
},
‘click .using’: function (e, value, row, index) {
$.ajax({
type : “POST”,
url : “/wadmin/ad/usingAd”,
data : {
adId : row[‘adId’]
},
dataType : ‘JSON’,
success : function (data) {
if (data.result != 0) {
toastr.info(“info”, data.message);
return ;
}
toastr.success(“success”, ‘使用该广告’);
$(“#table”).bootstrapTable(‘refresh’);
}
});
return false;
},
‘click .upload’: function (e, value, row, index) {
$(‘.adUpload’).fileupload({
url : ‘/wadmin/ad/adUpload/adId/’+row[‘adId’],
dataType: ‘json’,
add: function (e, data) {
data.submit();
},
done: function (e, data) {
var response = data.result;
if (response.result != 0) {
toastr.error(response.message);
return false;
}
toastr.success(“上传成功”);
$(“#table”).bootstrapTable(‘refresh’);
}
});
return false;
}
};
实现的效果如下:
7.行内编辑
普通的编辑只需要在列参数中设置即可:
列参数中添加代码
editable : true,
-
1
需要下拉编辑的,使用如下代码:
editable: {
type: ‘select’,
source: [ //0->无广告,1->静态不可点击,2->静态可点击,3->动态不可点击,4->动态可点击
{value: 0, text: ‘无广告’},
{value: 1, text: ‘静态不可点击’},
{value: 2, text: ‘静态可点击’},
{value: 3, text: ‘动态不可点击’},
{value: 4, text: ‘动态可点击’},
]
}
这里还有一些行内编辑的样式是需要导入第三方lib的,比如说如果要实现行内时间的编辑,需要下载导入combodate.js ,然后添加如下代码(这些js文件后面会给出的,也可以去官网下载最新版):
editable: {
type: ‘combodate’,
viewformat: ‘YYYY-MM-DD HH:mm:ss’,
template: ‘YYYY-MM-DD HH:mm:ss’,
format: ‘YYYY-MM-DD HH:mm:ss’,
combodate: {
minuteStep: 1,
secondStep: 1,
maxYear: 5000,
minYear: 2016,
}
}
然后再表格参数中添加如下代码监听事件:
onEditableSave: function (field, row, oldValue, $el) {
$.ajax({
type: “post”,
url: “/wadmin/ad/updateAdInfo”,
dataType : ‘json’,
data: row,
success: function (data, status) {
if (status == “success” && data.result == 0) {
toastr.success(‘更新成功’);
if (field == ‘jumpUrl’) {
$(‘#table’).bootstrapTable(“refresh”);
}
return true;
} else {
toastr.info(data.message);
$(‘#table’).bootstrapTable(“refresh”);
}
},
error: function () {
alert(“Error”);
},
complete: function () {
}
});
}
实现的效果如下:
其中,调用bootstraptable的一些方法可以动态更新表格(增删改等),用法如下:
$(“#table”).bootstrapTable(‘insertRow’, {index:0, row:data.data});
$(“#table”).bootstrapTable(‘remove’, {
field: ‘adId’,
values: [row[‘adId’]]
});
$(“#table”).bootstrapTable(‘refresh’);
$(“#table”).bootstrapTable(‘updateCell’, {
index : index,
field: ‘status’,
value: row[‘status’] ? 0 : 1
});
因为引入的js和css库有点多,而且需要实现行内编辑的话需要需要引入的js文件比较难找,下面给出下载地址(密码:jjdk):
http://pan.baidu.com/s/1bpiRObt
完整代码
html
<div id=”toolbar” class=”btn-group”>
<button id=”btn_add” type=”button” class=”btn btn-default”>
<span class=”glyphicon glyphicon-plus” aria-hidden=”true”></span>新增广告
</button>
</div>
<table id=”table”></table>
javascript
/**
* Created by raid on 2016/12/28.
*/
$(function () {
window.operateEvents = {
‘click .remove’: function (e, value, row, index) {
$.ajax({
type : “POST”,
url : “/wadmin/ad/deleteAd”,
data : {
adId : row[‘adId’]
},
dataType : ‘JSON’,
success : function (data) {
if (data.result != 0) {
toastr.info(“info”, data.message);
return ;
}
toastr.success(“success”, ‘删除成功’);
$(“#table”).bootstrapTable(‘remove’, {
field: ‘adId’,
values: [row[‘adId’]]
});
}
});
return false;
},
‘click .using’: function (e, value, row, index) {
$.ajax({
type : “POST”,
url : “/wadmin/ad/usingAd”,
data : {
adId : row[‘adId’]
},
dataType : ‘JSON’,
success : function (data) {
if (data.result != 0) {
toastr.info(“info”, data.message);
return ;
}
toastr.success(“success”, ‘使用该广告’);
$(“#table”).bootstrapTable(‘refresh’);
}
});
return false;
},
‘click .upload’: function (e, value, row, index) {
$(‘.adUpload’).fileupload({
url : ‘/wadmin/ad/adUpload/adId/’+row[‘adId’],
dataType: ‘json’,
add: function (e, data) {
data.submit();
},
done: function (e, data) {
var response = data.result;
if (response.result != 0) {
toastr.error(response.message);
return false;
}
toastr.success(“上传成功”);
$(“#table”).bootstrapTable(‘refresh’);
}
});
return false;
}
};
/*bootstrap table*/
$(‘#table’).bootstrapTable({
url:”/wadmin/ad/doAdList”,//请求数据url
toolbar : “#toolbar”,
// toolbarAlign : “right”,
queryParams: function (params) {
return {
offset: params.offset, //页码
limit: params.limit, //页面大小
search : params.search, //搜索
order : params.order, //排序
ordername : params.sort, //排序
};
},
detailView : true,
detailFormatter : function (index, row) {
var image = ‘<div class=”photos”>’
+'<a target=”_blank” href=”‘+row[‘jumpUrl’]+'”><img alt=”image” class=”feed-photo” src=”‘+row[‘picUrl’]+'”></a>’
+'</div>’;
return image;
},
showHeader : true,
showColumns : true,
showRefresh : true,
pagination: true,//分页
sidePagination : ‘server’,//服务器端分页
pageNumber : 1,
pageList: [5, 10, 20, 50],//分页步进值
search: true,//显示搜索框
//表格的列
columns: [
{
field: ‘adId’,//域值
title: ‘广告ID’,//标题
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘5%’,
},
{
field: ‘picUrl’,//域值
title: ‘图片’,//标题
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘15%’,
formatter : function (value, row, index) {
return “<img style=’width: 50px;height: 50px;’ src='”+value+”‘ alt=”>”
}
},
{
field: ‘jumpUrl’,//域值
title: ‘跳转链接’,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘10%’,
editable : true,
cellStyle : function (value, row, index) {
return {
css: {
“max-width”: “300px”,
“word-wrap”: “break-word”,
“word-break”: “normal”
}
};
}
},
{
field: ‘adDesc’,//域值
title: ‘描述’,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘10%’,
editable : true,
},
{
field: ‘displayType’,//域值
title: ‘表现形式’,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘10%’,
editable: {
type: ‘select’,
source: [ //0->无广告,1->静态不可点击,2->静态可点击,3->动态不可点击,4->动态可点击
{value: 0, text: ‘无广告’},
{value: 1, text: ‘静态不可点击’},
{value: 2, text: ‘静态可点击’},
{value: 3, text: ‘动态不可点击’},
{value: 4, text: ‘动态可点击’},
]
}
},
{
field: ‘displaySeconds’,//域值
title: ‘时间’,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘5%’,
editable : true,
},
{
field: ‘scope’,//域值
title: ‘影响范围’,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘10%’,
editable: {
type: ‘select’,
source: [ //0->全国
{value: 0, text: ‘全国’},
]
}
},
{
field: ‘userType’,//域值
title: ‘影响群体’,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘5%’,
editable: {
type: ‘select’,
source: [ //0->全部
{value: 0, text: ‘全部’},
]
}
},
{
field: ‘status’,//域值
title: ‘状态’,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘10%’,
formatter : function (value, row, index) {
return value==1||value==’1′ ? ‘正在使用’ : ‘没有使用’;
}
},
{
field: ‘addTime’,//域值
title: ‘时间’,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘10%’,
}
,{
field: ‘operate’,
title: ‘操作’,
align: ‘center’,
events: operateEvents,
width : ‘25%’,
formatter: operateFormatter
}
],
onEditableSave: function (field, row, oldValue, $el) {
$.ajax({
type: “post”,
url: “/wadmin/ad/updateAdInfo”,
dataType : ‘json’,
data: row,
success: function (data, status) {
if (status == “success” && data.result == 0) {
toastr.success(‘更新成功’);
if (field == ‘jumpUrl’) {
$(‘#table’).bootstrapTable(“refresh”);
}
return true;
} else {
toastr.info(data.message);
$(‘#table’).bootstrapTable(“refresh”);
}
},
error: function () {
alert(“Error”);
},
complete: function () {
}
});
}
});
$(“#btn_add”).click(function () {
$.ajax({
type : “POST”,
url : “/wadmin/ad/addAd”,
dataType : ‘JSON’,
success : function (data) {
if (data.result != 0) {
toastr.info(“info”, data.message);
return ;
}
toastr.success(“success”, ‘标签’);
$(“#table”).bootstrapTable(‘insertRow’, {index:0, row:data.data});
}
});
});
function operateFormatter(value, row, index) {
return [
‘<a class=”using” href=”javascript:void(0)” title=”Remove”>’,
‘<i class=”fa fa-check”></i>’,
‘</a> ’,
‘<a class=”upload” style=”cursor: pointer” href=”javascript:void(0)” title=”Upload”>’,
‘<label style=”cursor: pointer” for=”‘+row[‘adId’]+'”>’,
‘<i class=”fa fa-upload”></i>’,
‘<input type=”file” name=”adUpload” style=”display: none;” class=”adUpload” id=”‘+row[‘adId’]+'” >’,
‘</label>’,
‘</a> ’,
‘<a class=”remove” href=”javascript:void(0)” title=”Using”>’,
‘<i class=”fa fa-times”></i>’,
‘</a>’,
].join(”);
}
});
表格参数
表格的参数定义在 jQuery.fn.bootstrapTable.defaults。
名称 |
标签 |
类型 |
默认 |
描述 |
– |
data-toggle |
String |
‘table’ |
不用写 JavaScript 直接启用表格。 |
classes |
data-classes |
String |
‘table table-hover’ |
表格的类名称。默认情况下,表格是有边框的,你可以添加 ‘table-no-bordered’ 来删除表格的边框样式。 |
sortClass |
data-sort-class |
String |
undefined |
被排序的td元素的类名。 |
height |
data-height |
Number |
undefined |
定义表格的高度。 |
undefinedText |
data-undefined-text |
String |
‘-‘ |
当数据为 undefined 时显示的字符。 |
striped |
data-striped |
Boolean |
false |
设置为 true 会有隔行变色效果。 |
sortName |
data-sort-name |
String |
undefined |
定义排序列,通过url方式获取数据填写字段名,否则填写下标。 |
sortOrder |
data-sort-order |
String |
‘asc’ |
定义排序方式,’asc’ 或者 ‘desc’。 |
sortStable |
data-sort-stable |
Boolean |
false |
设置为 true 将获得稳定的排序,我们会添加\_position属性到 row 数据中。 |
iconsPrefix |
data-icons-prefix |
String |
‘glyphicon’ |
定义字体库 (‘Glyphicon’ or ‘fa’ for FontAwesome),使用”fa”时需引用 FontAwesome,并且配合 icons 属性实现效果。
Glyphicon 集成于Bootstrap可免费使用,参考:
FontAwesome 参考: |
icons |
data-icons |
Object |
{ paginationSwitchDown: ‘glyphicon-collapse-down icon-chevron-down’, paginationSwitchUp: ‘glyphicon-collapse-up icon-chevron-up’, refresh: ‘glyphicon-refresh icon-refresh’ toggle: ‘glyphicon-list-alt icon-list-alt’ columns: ‘glyphicon-th icon-th’ detailOpen: ‘glyphicon-plus icon-plus’ detailClose: ‘glyphicon-minus icon-minus’ } |
自定义图标 |
columns |
– |
Array |
[] |
列配置项,详情请查看 列参数 表格. |
data |
– |
Array |
[] |
加载json格式的数据。 |
ajax |
data-ajax |
Function |
undefined |
自定义 AJAX 方法,须实现 jQuery AJAX API。 |
method |
data-method |
String |
‘get’ |
服务器数据的请求方式 ‘get’ 或 ‘post’。 |
url |
data-url |
String |
undefined |
服务器数据的加载地址。 |
cache |
data-cache |
Boolean |
true |
设置为 false 禁用 AJAX 数据缓存。 |
contentType |
data-content-type |
String |
‘application/json’ |
发送到服务器的数据编码类型。 |
dataType |
data-data-type |
String |
‘json’ |
服务器返回的数据类型。 |
ajaxOptions |
data-ajax-options |
Object |
{} |
提交ajax请求时的附加参数,可用参数列请查看 |
queryParams |
data-query-params |
Function |
function(params) { return params; } |
请求服务器数据时,你可以通过重写参数的方式添加一些额外的参数,例如 toolbar 中的参数 如果 queryParamsType = ‘limit’ ,返回参数必须包含 limit, offset, search, sort, order 否则, 需要包含: pageSize, pageNumber, searchText, sortName, sortOrder. 返回false将会终止请求。 |
queryParamsType |
data-query-params-type |
String |
‘limit’ |
设置为 ‘limit’ 则会发送符合 RESTFul 格式的参数。 |
responseHandler |
data-response-handler |
Function |
function(res) { return res; } |
加载服务器数据之前的处理程序,可以用来格式化数据。 参数:res为从服务器请求到的数据。 |
pagination |
data-pagination |
Boolean |
false |
设置为 true 会在表格底部显示分页条。 |
paginationLoop |
data-pagination-loop |
Boolean |
true |
设置为 true 启用分页条无限循环的功能。 |
onlyInfoPagination |
data-only-info-pagination |
Boolean |
false |
设置为 true 只显示总数据数,而不显示分页按钮。需要设置 pagination=’true’。 |
sidePagination |
data-side-pagination |
String |
‘client’ |
设置在哪里进行分页,可选值为 ‘client’ 或者 ‘server’。设置 ‘server’时,必须设置服务器数据地址(url)或者重写ajax方法。 |
pageNumber |
data-page-number |
Number |
1 |
如果设置了分页,首页页码。 |
pageSize |
data-page-size |
Number |
10 |
如果设置了分页,页面数据条数。 |
pageList |
data-page-list |
Array |
[10, 25, 50, 100, All] |
如果设置了分页,设置可供选择的页面数据条数。设置为 All 或者 Unlimited,则显示所有记录。 |
selectItemName |
data-select-item-name |
String |
‘btSelectItem’ |
radio 或者 checkbox 的字段 name 名。 |
smartDisplay |
data-smart-display |
Boolean |
true |
设置为 true 是程序自动判断显示分页信息和 card 视图。 |
escape |
data-escape |
Boolean |
false |
转义HTML字符串,替换 &, <, >, “, \`, 和 ‘ 字符。 |
search |
data-search |
Boolean |
false |
是否启用搜索框。 |
searchOnEnterKey |
data-search-on-enter-key |
Boolean |
false |
设置为 true时,按回车触发搜索方法,否则自动触发搜索方法。 |
strictSearch |
data-strict-search |
Boolean |
false |
设置为 true启用全匹配搜索,否则为模糊搜索。 |
searchText |
data-search-text |
String |
” |
初始化搜索文字。 |
searchTimeOut |
data-search-time-out |
Number |
500 |
设置搜索超时时间。 |
trimOnSearch |
data-trim-on-search |
Boolean |
true |
设置为 true 将自动去掉搜索字符的前后空格。 |
showHeader |
data-show-header |
Boolean |
true |
是否显示列头。 |
showFooter |
data-show-footer |
Boolean |
false |
是否显示列脚。 |
showColumns |
data-show-columns |
Boolean |
false |
是否显示内容列下拉框。 |
showRefresh |
data-show-refresh |
Boolean |
false |
是否显示刷新按钮。 |
showToggle |
data-show-toggle |
Boolean |
false |
是否显示切换视图(table/card)按钮。 |
showPaginationSwitch |
data-show-pagination-switch |
Boolean |
false |
是否显示切换分页按钮。 |
showFullscreen |
data-show-fullscreen |
Boolean |
false |
是否显示全屏按钮。 |
minimumCountColumns |
data-minimum-count-columns |
Number |
1 |
最小隐藏列的数量。 |
idField |
data-id-field |
String |
undefined |
指定主键列。 |
uniqueId |
data-unique-id |
String |
undefined |
对每一行指定唯一标识符。 |
cardView |
data-card-view |
Boolean |
false |
设置为 true将显示card视图,适用于移动设备。否则为table试图,适用于pc端。 |
detailView |
data-detail-view |
Boolean |
false |
设置为 true 可以显示详细页面模式。 |
detailFormatter |
data-detail-formatter |
Function |
function(index, row) { return ”; } |
格式化详细页面模式的视图。 |
searchAlign |
data-search-align |
String |
‘right’ |
指定 搜索框 水平方向的位置。’left’ 或 ‘right’。 |
buttonsAlign |
data-buttons-align |
String |
‘right’ |
指定 按钮栏 水平方向的位置。’left’ 或 ‘right’。 |
toolbarAlign |
data-toolbar-align |
String |
‘left’ |
指定 toolbar 水平方向的位置。’left’ 或 ‘right’。 |
paginationVAlign |
data-pagination-v-align |
String |
‘bottom’ |
指定 分页条 在垂直方向的位置。’top’,’bottom’ 或 ‘both’。 |
paginationHAlign |
data-pagination-h-align |
String |
‘right’ |
指定 分页条 在水平方向的位置。’left’ 或 ‘right’。 |
paginationDetailHAlign |
data-pagination-detail-h-align |
String |
‘left’ |
指定 分页详细信息 在水平方向的位置。’left’ 或 ‘right’。 |
paginationPreText |
data-pagination-pre-text |
String |
‘<‘ |
指定分页条中上一页按钮的图标或文字。 |
paginationNextText |
data-pagination-next-text |
String |
‘>’ |
指定分页条中下一页按钮的图标或文字。 |
clickToSelect |
data-click-to-select |
Boolean |
false |
设置 true 将在点击行时,自动选择 rediobox 和 checkbox。 |
ignoreClickToSelectOn |
data-ignore-click-to-select-on |
Function |
{ return $.inArray(element.tagName, [‘A’, ‘BUTTON’]); } |
包含一个参数: element: 点击的元素。 返回 true 是点击事件会被忽略,返回 false 将会自动选中。该选项只有在 clickToSelect 为 true 时才生效。 |
singleSelect |
data-single-select |
Boolean |
false |
设置 true 将禁止多选。 |
toolbar |
data-toolbar |
String |
undefined |
一个jQuery 选择器,指明自定义的 toolbar。例如: #toolbar, .toolbar. |
buttonsToolbar |
data-buttons-toolbar |
String | Node |
undefined |
一个jQuery 选择器,指明自定义的 buttons toolbar。例如: #buttons-toolbar, .buttons-toolbar 或 DOM 节点。 |
checkboxHeader |
data-checkbox-header |
Boolean |
true |
设置 false 将在列头隐藏全选复选框。 |
maintainSelected |
data-maintain-selected |
Boolean |
false |
设置为 true 在点击分页按钮或搜索按钮时,将记住checkbox的选择项。 |
sortable |
data-sortable |
Boolean |
true |
设置为false 将禁止所有列的排序。 |
silentSort |
data-silent-sort |
Boolean |
true |
设置为 false 将在点击分页按钮时,自动记住排序项。仅在 sidePagination设置为 server时生效。 |
rowStyle |
data-row-style |
Function |
function(row,index) { return class; } |
自定义行样式 参数为: row: 行数据 index: 行下标 返回值可以为class或者css |
rowAttributes |
data-row-attributes |
Function |
function(row,index) { return attributes; } |
自定义行属性 参数为: row: 行数据 index: 行下标 返回值可以为class或者css 支持所有自定义属性 |
customSearch |
data-custom-search |
Function |
$.noop |
自定义搜索方法来替代内置的搜索功能,它包含一个参数: text:搜索文字。 用法示例:
function customSearch(text) { //Search logic here. //You must use `this.data` array in order to filter the data. NO use `this.options.data`. } |
customSort |
data-custom-sort |
Function |
$.noop |
自定义排序方法来替代内置的搜索功能,它包含一个参数: sortName: 排序名。 sortOrder: 排序顺序。 用法示例:
function customSort(sortName, sortOrder) { //Sort logic here. //You must use `this.data` array in order to sort the data. NO use `this.options.data`. } |
列参数
The column options is defined in jQuery.fn.bootstrapTable.columnDefaults.
Name |
Attribute |
Type |
Default |
Description |
radio |
data-radio |
Boolean |
false |
True to show a radio. The radio column has fixed width. |
checkbox |
data-checkbox |
Boolean |
false |
True to show a checkbox. The checkbox column has fixed width. |
field |
data-field |
String |
undefined |
The column field name. |
title |
data-title |
String |
undefined |
The column title text. |
titleTooltip |
data-title-tooltip |
String |
undefined |
The column title tooltip text. This option also support the title HTML attribute |
class |
class / data-class |
String |
undefined |
The column class name. |
rowspan |
rowspan / data-rowspan |
Number |
undefined |
Indicate how many rows a cell should take up. |
colspan |
colspan / data-colspan |
Number |
undefined |
Indicate how many columns a cell should take up. |
align |
data-align |
String |
undefined |
Indicate how to align the column data. ‘left’, ‘right’, ‘center’ can be used. |
halign |
data-halign |
String |
undefined |
Indicate how to align the table header. ‘left’, ‘right’, ‘center’ can be used. |
falign |
data-falign |
String |
undefined |
Indicate how to align the table footer. ‘left’, ‘right’, ‘center’ can be used. |
valign |
data-valign |
String |
undefined |
Indicate how to align the cell data. ‘top’, ‘middle’, ‘bottom’ can be used. |
width |
data-width |
Number {Pixels or Percentage} |
undefined |
The width of column. If not defined, the width will auto expand to fit its contents. Also you can add ‘%’ to your number and the bootstrapTable will use the percentage unit, otherwise, you can add or no the ‘px’ to your number and then the bootstrapTable will use the pixels |
sortable |
data-sortable |
Boolean |
false |
True to allow the column can be sorted. |
order |
data-order |
String |
‘asc’ |
The default sort order, can only be ‘asc’ or ‘desc’. |
visible |
data-visible |
Boolean |
true |
False to hide the columns item. |
cardVisible |
data-card-visible |
Boolean |
true |
False to hide the columns item in card view state. |
switchable |
data-switchable |
Boolean |
true |
False to disable the switchable of columns item. |
clickToSelect |
data-click-to-select |
Boolean |
true |
True to select checkbox or radiobox when the column is clicked. |
formatter |
data-formatter |
Function |
undefined |
The context (this) is the column Object. The cell formatter function, take three parameters: value: the field value. row: the row record data. index: the row index. |
footerFormatter |
data-footer-formatter |
Function |
undefined |
The context (this) is the column Object. The function, take one parameter: data: Array of all the data rows. the function should return a string with the text to show in the footer cell. |
events |
data-events |
Object |
undefined |
The cell events listener when you use formatter function, take three parameters: event: the jQuery event. value: the field value. row: the row record data. index: the row index. |
sorter |
data-sorter |
Function |
undefined |
The custom field sort function that used to do local sorting, take two parameters: a: the first field value. b: the second field value. rowA: the first row. rowB: the second row. |
sortName |
data-sort-name |
String |
undefined |
Provide a customizable sort-name, not the default sort-name in the header, or the field name of the column. For example, a column might display the value of fieldName of “html” such as “<b><span style=”color:red”>abc</span></b>”, but a fieldName to sort is “content” with the value of “abc”. |
cellStyle |
data-cell-style |
Function |
undefined |
The cell style formatter function, take three parameters: value: the field value. row: the row record data. index: the row index. field: the row field. Support classes or css. |
searchable |
data-searchable |
Boolean |
true |
True to search data for this column. |
searchFormatter |
data-search-formatter |
Boolean |
true |
True to search use formated data. |
escape |
data-escape |
Boolean |
false |
Escapes a string for insertion into HTML, replacing &, <, >, “, \`, and ‘ characters. |
showSelectTitle |
data-show-select-title |
Boolean |
false |
True to show the title of column with ‘radio’ or ‘singleSelect’ ‘checkbox’ option. |
事件
Option 事件 |
jQuery 事件 |
参数 |
描述 |
onAll |
all.bs.table |
name, args |
所有的事件都会触发该事件,参数包括: name:事件名, args:事件的参数。 |
onClickRow |
click-row.bs.table |
row, $element |
当用户点击某一行的时候触发,参数包括: row:点击行的数据, $element:tr 元素, field:点击列的 field 名称。 |
onDblClickRow |
dbl-click-row.bs.table |
row, $element |
当用户双击某一行的时候触发,参数包括: row:点击行的数据, $element:tr 元素, field:点击列的 field 名称。 |
onClickCell |
click-cell.bs.table |
field, value, row, $element |
当用户点击某一列的时候触发,参数包括: field:点击列的 field 名称, value:点击列的 value 值, row:点击列的整行数据, $element:td 元素。 |
onDblClickCell |
dbl-click-cell.bs.table |
field, value, row, $element |
当用户双击某一列的时候触发,参数包括: field:点击列的 field 名称, value:点击列的 value 值, row:点击列的整行数据, $element:td 元素。 |
onSort |
sort.bs.table |
name, order |
当用户对某列进行排序时触发,参数包括: name:排序列的 filed 名称, order:排序顺序。 |
onCheck |
check.bs.table |
row |
当用户选择某一行时触发,参数包含: row:与点击行对应的记录, $element:选择的DOM元素。 |
onUncheck |
uncheck.bs.table |
row |
当用户反选某一行时触发,参数包含: row:与点击行对应的记录, $element:选择的DOM元素。 |
onCheckAll |
check-all.bs.table |
rows |
当用户全选所有的行时触发,参数包含: rows:最新选择的所有行的数组。 |
onUncheckAll |
uncheck-all.bs.table |
rows |
当用户反选所有的行时触发,参数包含: rows:最新选择的所有行的数组。 |
onCheckSome |
check-some.bs.table |
rows |
当用户选择某些行时触发,参数包含: rows:相对于之前选择的行的数组。 |
onUncheckSome |
uncheck-some.bs.table |
rows |
当用户反选某些行时触发,参数包含: rows:相对于之前选择的行的数组。 |
onLoadSuccess |
load-success.bs.table |
data |
远程数据加载成功时触发成功。 |
onLoadError |
load-error.bs.table |
status |
远程数据加载失败时触发成功。 |
onColumnSwitch |
column-switch.bs.table |
field, checked |
当切换列的时候触发。 |
onColumnSearch |
column-search.bs.table |
field, text |
当搜索列时触发。 |
onPageChange |
page-change.bs.table |
number, size |
当页面更改页码或页面大小时触发。 |
onSearch |
search.bs.table |
text |
当搜索表格时触发。 |
onToggle |
toggle.bs.table |
cardView |
切换表格视图时触发。 |
onPreBody |
pre-body.bs.table |
data |
在表格 body 渲染之前触发。 |
onPostBody |
post-body.bs.table |
none |
在表格 body 渲染完成后触发。 |
onPostHeader |
post-header.bs.table |
none |
在表格 header 渲染完成后触发。 |
onExpandRow |
expand-row.bs.table |
index, row, $detail |
当点击详细图标展开详细页面的时候触发。 |
onCollapseRow |
collapse-row.bs.table |
index, row |
当点击详细图片收起详细页面的时候触发。 |
onRefreshOptions |
refresh-options.bs.table |
options |
刷新选项之后并在销毁和初始化表之前触发。 |
onRefresh |
refresh.bs.table |
params |
点击刷新按钮后触发。 |
onScrollBody |
scroll-body.bs.table |
表格 body 滚动时触发。 |
方法
使用方法的语法:$(‘#table’).bootstrapTable(‘method’, parameter);。
名称 |
参数 |
描述 |
例子 |
getOptions |
none |
返回表格的 Options。 |
|
getSelections |
none |
返回所选的行,当没有选择任何行的时候返回一个空数组。 |
|
getAllSelections |
none |
返回所有选择的行,包括搜索过滤前的,当没有选择任何行的时候返回一个空数组。 |
|
getData |
useCurrentPage |
或者当前加载的数据。假如设置 useCurrentPage 为 true,则返回当前页的数据。 |
|
getRowByUniqueId |
id |
根据 uniqueId 获取行数据。 |
|
load |
data |
加载数据到表格中,旧数据会被替换。 |
|
showAllColumns |
none |
显示所有列。 |
|
hideAllColumns |
none |
隐藏所有列。 |
|
append |
data |
添加数据到表格在现有数据之后。 |
|
prepend |
data |
插入数据到表格在现有数据之前。 |
|
remove |
params |
从表格中删除数据,包括两个参数: field: 需要删除的行的 field 名称, values: 需要删除的行的值,类型为数组。 |
|
removeAll |
– |
删除表格所有数据。 |
|
removeByUniqueId |
id |
根据 uniqueId 删除指定的行。 |
|
insertRow |
params |
插入新行,参数包括: index: 要插入的行的 index, row: 行的数据,Object 对象。 |
|
updateRow |
params |
更新指定的行,参数包括: index: 要更新的行的 index, row: 行的数据,Object 对象。 |
|
showRow |
params |
显示指定的行,参数包括: index: 要更新的行的 index 或者 uniqueId, isIdField: 指定 index 是否为 uniqueid。 |
|
hideRow |
params |
显示指定的行,参数包括: index: 要更新的行的 index, uniqueId: 或者要更新的行的 uniqueid。 |
|
getHiddenRows |
show |
获取所有隐藏的行,如果show参数为true,行将再次显示,否则,只返回隐藏的行。 |
|
mergeCells |
options |
将某些单元格合并到一个单元格,选项包含以下属性: index: 行索引, field: 字段名称, rowspan: 要合并的rowspan数量, colspan: 要合并的colspan数量。 |
|
updateCell |
params |
更新一个单元格,params包含以下属性: index: 行索引。 field: 字段名称。 value: 新字段值。 |
|
refresh |
params |
刷新远程服务器数据,可以设置{silent: true}以静默方式刷新数据,并设置{url: newUrl}更改URL。 要提供特定于此请求的查询参数,请设置{query: {foo: ‘bar’}}。 |
|
refreshOptions |
options |
刷新选项。 |
|
resetSearch |
text |
设置搜索文本。 |
|
showLoading |
none |
显示加载状态。 |
|
hideLoading |
none |
隐藏加载状态。 |
|
checkAll |
none |
选中当前页面所有行。 |
|
uncheckAll |
none |
取消选中当前页面所有行。 |
|
check |
index |
选中某一行,行索引从0开始。 |
|
uncheck |
index |
取消选中某一行,行索引从0开始。 |
|
checkBy |
params |
按值或数组选中某行,参数包含: field: 用于查找记录的字段的名称, values: 要检查的行的值数组。 例子: $(“#table”).bootstrapTable(“checkBy”, {field:”field_name”, values:[“value1″,”value2″,”value3”]}) |
|
uncheckBy |
params |
按值数组取消选中某行,参数包含: field: 用于查找记录的字段的名称, values: 要检查的行的值数组。 例子: $(“#table”).bootstrapTable(“uncheckBy”, {field:”field_name”, values:[“value1″,”value2″,”value3”]}) |
|
resetView |
params |
重置引导表视图,例如重置表高度。 |
|
resetWidth |
none |
调整页眉和页脚的大小以适合当前列宽度。 |
|
destroy |
none |
销毁表。 |
|
showColumn |
field |
显示指定的列。 |
|
hideColumn |
field |
隐藏指定的列。 |
|
getHiddenColumns |
– |
获取隐藏的列。 |
|
getVisibleColumns |
– |
获取可见列。 |
|
scrollTo |
value |
滚动到指定位置,单位为 px,设置 ‘bottom’ 表示跳到最后。 |
|
getScrollPosition |
none |
获取当前滚动条的位置,单位为 px。 |
|
filterBy |
params |
(只能用于 client 端)过滤表格数据, 你可以通过过滤{age: 10}来显示 age 等于 10 的数据。 |
|
selectPage |
page |
跳到指定的页。 |
|
prevPage |
none |
跳到上一页。 |
|
nextPage |
none |
跳到下一页。 |
|
togglePagination |
none |
切换分页选项。 |
|
toggleView |
none |
切换 card/table 视图 |
|
expandRow |
index |
如果详细视图选项设置为True,可展开索引为 index 的行。 |
|
collapseRow |
index |
如果详细视图选项设置为True,可收起索引为 index 的行。. |
|
expandAllRows |
none |
如果详细视图选项设置为True,可展开所有行。 |
|
collapseAllRows |
none |
如果详细视图选项设置为True,可收起开所有行。 |
多语言
Edit on GitHub
Name |
Parameter |
Default |
formatLoadingMessage |
– |
‘Loading, please wait…’ |
formatRecordsPerPage |
pageNumber |
‘%s records per page’ |
formatShowingRows |
pageFrom, pageTo, totalRows |
‘Showing %s to %s of %s rows’ |
formatDetailPagination |
totalRows |
‘Showing %s rows’ |
formatSearch |
– |
‘Search’ |
formatNoMatches |
– |
‘No matching records found’ |
formatRefresh |
– |
‘Refresh’ |
formatToggle |
– |
‘Toggle’ |
formatColumns |
– |
‘Columns’ |
formatAllRows |
– |
‘All’ |
formatFullscreen |
– |
‘Fullscreen’ |
PS:
We can import
all locale files
what you need:
Copy
<script src=”bootstrap-table-en-US.js”></script><script src=”bootstrap-table-zh-CN.js”></script>
…
And then use JavaScript to switch locale:
Copy
$.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales[‘en-US’]);
// $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales[‘zh-CN’]);
// …