bootstrap表格插件——Bootstrap-Table
注:本文引用自:http://www.cnblogs.com/wuhuacong/p/7284420.html
一、概述
1.是什么
是一个基于bootstrap的灌数据式的表格插件
2.能干什么
可以实现查询、分页、排序、复选框、设置显示列、Card view视图、主从表显示、合并列、国际化处理等处理功能,以及一些不错的扩展功能
3.怎么干
插件官网:http://bootstrap-table.wenzhixin.net.cn/
官方示例:http://bootstrap-table.wenzhixin.net.cn/examples/ (FQ查看效果更佳)
下载地址:https://github.com/wenzhixin/bootstrap-table
依赖bootstrap:http://v3.bootcss.com/getting-started/#download (依赖 jQuery,如何引入bootstrap样式请参见bootstrap随笔)
示例demo根据此目录结构引入样式,实际操作时请根据实际情况

样式引入:
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<script src="bootstrap/js/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="bootstrap-table/dist/bootstrap-table.css">
<script src="bootstrap-table/dist/bootstrap-table.js"></script>
<!--汉化文件,放在 bootstrap-table.js 后面-->
<script src="bootstrap-table/dist/locale/bootstrap-table-zh-CN.js"></script>
二、起步
根据文档中的起步:http://bootstrap-table.wenzhixin.net.cn/getting-started/ ,我们知道两种初始化方式:
1.HTML方式:
<table data-toggle="table" data-url="http://localhost:8080/jeesite/f/test/test/btList">
<thead>
<tr>
<th data-field="id">Item ID</th>
<th data-field="name">Item Name</th>
<th data-field="price">Item Price</th>
</tr>
</thead>
</table>
//请注意加上 http://
2.JS方式
<table id="table"></table>
<script type="text/javascript">
$('#table').bootstrapTable({
url: 'http://localhost:8080/jeesite/f/test/test/btList',
columns: [{
field: 'id',
title: 'Item ID'
}, {
field: 'name',
title: 'Item Name'
}, {
field: 'price',
title: 'Item Price'
}, ]
});
</script>
//同样,请注意url的完整性写法
后台使用的是sprinMVC的简单测试:
/**
* 测试Bootstrap-Table
* @return
*/
@RequestMapping(value = "btList")
@ResponseBody
public List<BTItemTest> btList(HttpServletResponse response){
BTItemTest b1 = new BTItemTest("1001","马邦德","100");
BTItemTest b2 = new BTItemTest("1002","县长","200");
BTItemTest b3 = new BTItemTest("1003","黄四郎","400");
List<BTItemTest> list = new ArrayList<BTItemTest>();
list.add(b1);
list.add(b2);
list.add(b3);
response.setHeader("Access-Control-Allow-Origin", "*");
return list;
}
效果:

当然,表格有点长了,这里可以使用栅格系统进行限制:(限制大小,限制偏移)
<div class="row">
<div class="col-md-10">
<table id="table"></table>
</div>
</div>
三、详细使用
常见参数可以参见文首博客作者的随笔,如下:
var $table;
//初始化bootstrap-table的内容
function InitMainTable () {
//记录页面bootstrap-table全局变量$table,方便应用
var queryUrl = '/TestUser/FindWithPager?rnd=' + Math.random()
$table = $('#grid').bootstrapTable({
url: queryUrl, //请求后台的URL(*)
method: 'GET', //请求方式(*)
//toolbar: '#toolbar', //工具按钮用哪个容器
striped: true, //是否显示行间隔色
cache: false, //是否使用缓存,默认为true,所以一般情况下需要设置一下这个属性(*)
pagination: true, //是否显示分页(*)
sortable: true, //是否启用排序
sortOrder: "asc", //排序方式
sidePagination: "server", //分页方式:client客户端分页,server服务端分页(*)
pageNumber: 1, //初始化加载第一页,默认第一页,并记录
pageSize: rows, //每页的记录行数(*)
pageList: [10, 25, 50, 100], //可供选择的每页的行数(*)
search: false, //是否显示表格搜索
strictSearch: true,
showColumns: true, //是否显示所有的列(选择显示的列)
showRefresh: true, //是否显示刷新按钮
minimumCountColumns: 2, //最少允许的列数
clickToSelect: true, //是否启用点击选中行
//height: 500, //行高,如果没有设置height属性,表格自动根据记录条数觉得表格高度
uniqueId: "ID", //每一行的唯一标识,一般为主键列
showToggle: true, //是否显示详细视图和列表视图的切换按钮
cardView: false, //是否显示详细视图
detailView: false, //是否显示父子表
//得到查询的参数
queryParams : function (params) {
//这里的键的名字和控制器的变量名必须一直,这边改动,控制器也需要改成一样的
var temp = {
rows: params.limit, //页面大小
page: (params.offset / params.limit) + 1, //页码
sort: params.sort, //排序列名
sortOrder: params.order //排位命令(desc,asc)
};
return temp;
},
columns: [{
checkbox: true,
visible: true //是否显示复选框
}, {
field: 'Name',
title: '姓名',
sortable: true
}, {
field: 'Mobile',
title: '手机',
sortable: true
}, {
field: 'Email',
title: '邮箱',
sortable: true,
formatter: emailFormatter
}, {
field: 'Homepage',
title: '主页',
formatter: linkFormatter
}, {
field: 'Hobby',
title: '兴趣爱好'
}, {
field: 'Gender',
title: '性别',
sortable: true
}, {
field: 'Age',
title: '年龄'
}, {
field: 'BirthDate',
title: '出生日期',
formatter: dateFormatter
}, {
field: 'Height',
title: '身高'
}, {
field: 'Note',
title: '备注'
}, {
field:'ID',
title: '操作',
width: 120,
align: 'center',
valign: 'middle',
formatter: actionFormatter
}, ],
onLoadSuccess: function () {
},
onLoadError: function () {
showTips("数据加载失败!");
},
onDblClickRow: function (row, $element) {
var id = row.ID;
EditViewById(id, 'view');
},
});
};
例如,简单的客户端分页:(完全的参数请参见文档,例如method等参数)
$(function(){
initTable();
});
var $table;
//初始化table
function initTable(){
var queryUrl = "http://localhost:8080/jeesite/f/test/test/btList";
$table = $('#table').bootstrapTable({
url: queryUrl,
cache: false,
pagination: true,
sidePagination: "client",
pageNumber: 1, //初始化加载第一页,默认第一页,并记录
pageSize: 5,
pageList: [5,10],
columns: [
{ checkbox: true,
visible: true //是否显示复选框
},{
field: 'id',
title: 'Item ID'
}, {
field: 'name',
title: 'Item Name'
}, {
field: 'price',
title: 'Item Price'
}, ]
});
}
效果:

//少量数据适合此方式
服务端分页的形式
注意:
[json]必须包含:total节点(总记录数),rows节点(分页后数据)
即:{“total”:24,”rows”:[…]}
先看简单版的服务端分页(分页插件采用mybatis官方推荐的pagehelper)
前台页面:
$(function(){
initTable();
});
var $table;
//初始化table
function initTable(){
var queryUrl = "http://localhost:8080/jeesite/f/test/test/btList";
$table = $('#table').bootstrapTable({
//此URL传递表格插件的固定参数,获取固定格式数据
url: queryUrl,
//请求方式
method:"GET",
//设置表格样式,是否隔行换色,使用条纹样式
striped:true,
//设置是否使用缓存,一般设置false
cache: false,
//是否显示搜索
search: false,
//是否开启分页
pagination: true,
//分页方式:client客户端分页,server服务端分页(*)
sidePagination: "server",
//初始化加载第一页,默认第一页,并记录
pageNumber: 1,
//每页的记录行数(*)
pageSize: 5,
//可供选择的每页的行数(*)
pageList: [5,10],
//得到查询的参数
queryParams : function (params) {
//这里的键的名字和控制器的变量名必须一直,这边改动,控制器也需要改成一样的
var temp = {
rows: params.limit, //页面大小
page: (params.offset / params.limit) + 1, //页码
};
return temp;
},
columns: [
{
//是否显示复选框
checkbox: true,
visible: true
},{
field: 'id',
title: 'Item ID',
align: 'center'
}, {
field: 'name',
title: 'Item Name',
align: 'center'
}, {
field: 'price',
title: 'Item Price',
align: 'center'
},
{
field: 'id',
title: '操作',
align: 'center',
formatter:function(value,row,index){ //转义函数formatter
//通过formatter可以自定义列显示的内容
//value:当前field的值,即id
//row:当前行的数据
var a = '<a href="" >测试</a>';
}
}]
});
}
完整页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bootstrap-Table</title>
<!-- 引入bootstrap-->
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<script src="bootstrap/js/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<!--引入bootstrap-table -->
<link rel="stylesheet" href="bootstrap-table/dist/bootstrap-table.css">
<script src="bootstrap-table/dist/bootstrap-table.js"></script>
<!--汉化文件,放在 bootstrap-table.js 后面-->
<script src="bootstrap-table/dist/locale/bootstrap-table-zh-CN.js"></script>
</head>
<body>
<h4>数据表格</h4>
<!-- <table data-toggle="table" data-url="http://localhost:8080/jeesite/f/test/test/btList">
<thead>
<tr>
<th data-field="id">Item ID</th>
<th data-field="name">Item Name</th>
<th data-field="price">Item Price</th>
</tr>
</thead>
</table> -->
<div class="row">
<div class="col-md-10">
<table id="table"></table>
</div>
</div>
<script type="text/javascript">
/* $('#table').bootstrapTable({
url: 'http://localhost:8080/jeesite/f/test/test/btList',
columns: [{
field: 'id',
title: 'Item ID'
}, {
field: 'name',
title: 'Item Name'
}, {
field: 'price',
title: 'Item Price'
}, ]
}); */
$(function(){
initTable();
});
var $table;
//初始化table
function initTable(){
var queryUrl = "http://localhost:8080/jeesite/f/test/test/btList";
$table = $('#table').bootstrapTable({
//此URL传递表格插件的固定参数,获取固定格式数据
url: queryUrl,
//请求方式
method:"GET",
//设置表格样式,是否隔行换色,使用条纹样式
striped:true,
//设置是否使用缓存,一般设置false
cache: false,
//是否显示搜索
search: false,
//是否开启分页
pagination: true,
//分页方式:client客户端分页,server服务端分页(*)
sidePagination: "server",
//初始化加载第一页,默认第一页,并记录
pageNumber: 1,
//每页的记录行数(*)
pageSize: 5,
//可供选择的每页的行数(*)
pageList: [5,10],
//得到查询的参数
queryParams : function (params) {
//这里的键的名字和控制器的变量名必须一直,这边改动,控制器也需要改成一样的
var temp = {
rows: params.limit, //页面大小
page: (params.offset / params.limit) + 1, //页码
};
return temp;
},
columns: [
{
//是否显示复选框
checkbox: true,
visible: true
},{
field: 'id',
title: 'Item ID',
align: 'center'
}, {
field: 'name',
title: 'Item Name',
align: 'center'
}, {
field: 'price',
title: 'Item Price',
align: 'center'
},
{
field: 'id',
title: '操作',
align: 'center',
formatter:function(value,row,index){ //转义函数formatter
//通过formatter可以自定义列显示的内容
//value:当前field的值,即id
//row:当前行的数据
var a = '<a href="" >测试</a>';
}
}]
});
}
</script>
</body>
</html>
服务端代码采用经典的SSM
@RequestMapping(value = "btList",method = RequestMethod.GET)
@ResponseBody
public Map<String,Object> btList(Integer rows,Integer page){
System.out.println("页面大小:"+rows);
System.out.println("页码:"+page);
//从第多少页开始,每页显示rows条记录
PageHelper.startPage(page, rows);
//后面紧跟的查询就是分页查询
List<BTItemTest> itemList = testService.findAllItems();
//用PageInfo对结果进行包装,只需要将PageInfo交给页面即可,这里面封装了详细的信息,第二个参数为需要连续显示的记录数
PageInfo pageInfo = new PageInfo(itemList,5);
//构造返回指定格式的map,用于转成JSON
Map<String,Object> map = new HashMap<String,Object>();
//构造返回数据
int totalInt = (int) pageInfo.getTotal();
map.put("total", totalInt);
map.put("rows", pageInfo.getList());
System.out.println(map);
//必须返回指定格式的JSON
return map;
}
实体类还是官方的例子:
public class BTItemTest {
private String id;
private String name;
private String price;
第一个服务端分页的demo效果如下:

bootstrap表格插件——Bootstrap-Table的更多相关文章
- Bootstrap Bootstrap表格插件bootstrap-table配置与应用小结
Bootstrap表格插件bootstrap-table配置与应用小结 by:授客 QQ:1033553122 1. 测试环境 win7 JQuery-3.2.1.min.js 下载地址: h ...
- 好用的自适应表格插件-bootstrap table (支持固定表头)
最近工作中找到了一款十分好用的表格插件,不但支持分页,样式,搜索,事件等等表格插件常有的功能外,最主要的就是他自带的冻结表头功能,让开发制作表格十分容易,不过网上大多都是英文文档,第一次使用会比较麻烦 ...
- 【转载】BootStrap表格组件bootstrap table详解
(转载,来源“脚本之家”,作者不详) 一.Bootstrap Table的引入 关于Bootstrap Table的引入,一般来说还是两种方法: 1.直接下载源码,添加到项目里面来.由于Bootstr ...
- 轻量级表格插件Bootstrap Table。拥有强大的支持固定表头、单/复选、排序、分页、搜索及自定义表头等功能。
Bootstrap Table是轻量级的和功能丰富的以表格的形式显示的数据,支持单选,复选框,排序,分页,显示/隐藏列,固定标题滚动表,响应式设计,Ajax加载JSON数据,点击排序的列,卡片视图等. ...
- Bootstrap表格组件 Bootstrap Table
Bootstrap Table是Bootstrap的一个组件 Bootstrap Table Demo:http://issues.wenzhixin.net.cn/bootstrap-table/i ...
- Bootstrap分页插件--Bootstrap Paginator
开源中国有一篇介绍的很详细,链接:https://my.oschina.net/shunshun/blog/204587 使用这个插件和使用其他Bootstrap内置的插件一样,需要引入如下文件: & ...
- bootstrap分页插件--Bootstrap Paginator的使用&AJAX版备份(可单独使用)
html部分: <ul class="pagination"></ul> <!--bootstrap3版本用ul包裹--> <div cl ...
- bootstrap评分插件 Bootstrap Star Rating Examples
http://www.jq22.com/demo/bootstrap-star-rating-master201708041812/
- 【原创】bootstrap框架的学习 第七课 -[bootstrap表格]
Bootstrap 表格 标签 描述 <table> 为表格添加基础样式. <thead> 表格标题行的容器元素(<tr>),用来标识表格列. <tbody& ...
随机推荐
- Linux中脚本的使用方法
Linux中脚本的使用方法 一.前言 关于Linux中的脚本的用法,一直没有时间去好好地总结,正好今天下雨,就好好的整理一下思路吧,其实精通了一门语言,比如C语言,学习其他语言需要的成本是非常少的,同 ...
- pyenv - python版本管理
1. 安装pyenv brew install pyenv 2. 安装python其它版本(如:python 3.6.7) pyenv install --list #查看可以安装的python版本 ...
- 鸡肋提权之变态root利用
你急有毛用,我电脑没带,怎么搞? 联系了基友adminlm牛看看吧,他说有防护软件啥的,有root,无法UDF,于是我让他去Mof,经历一番周折,知道了,对mof目录也锁定了权限,无法用root导出m ...
- 021.12 IO流 ObjectStream对象序列化
内容:通过文件存储对象我们遇到的问题,需要保存对象到硬盘中,如何解决这个就是用来解决的 用法:1.创建流对象FileOutputstream2.创建ObjectOutputStream对象与FileO ...
- Gym 100633G Nano alarm-clocks
题目,给定n个时钟,要求把他们调成一样的时间.求最小的步数 思路:肯定是有一个时钟作为标准的啦,要找到这个时钟,怎么找呢?没其他方便的方法,暴力枚举.那么枚举后,怎么能快速地算到其他时钟转到这个时钟的 ...
- jquery mobile header title左对齐 button右对齐
<div data-theme="b" data-role="header" data-position="fixed"> &l ...
- ThinkPHP里面用原生SQL
public function rewardlog(){ $adminNav = C('ADMIN_NAV'); $adminNav[1]['class'] = 'cur'; $this->as ...
- 用 S5PV210 学习 Linux (一) 刷机(一)
简介: 习惯了 用 keil 或者 IAR 一键下载 (烧写) 代码,S5PV210 貌似就不能这么简单用 仿真器的 方式 下载代码了,因此 学习 S5PV210 的第一步就是 学习怎么下载代码,下 ...
- Hive 的collect_set使用详解
Hive 的collect_set使用详解 https://blog.csdn.net/liyantianmin/article/details/48262109 对于非group by字段,可以 ...
- vue-cli3 使用mint-ui
关于vue-cli3.x按需引入mint-ui问题记录: 按需引入 借助 babel-plugin-component,我们可以只引入需要的组件,以达到减小项目体积的目的. 首先,安装 babel-p ...