BootStrap Table 合并单元格
为了更直观展示表格的一大堆乱七八糟的数据,合并单元格就派上用场。。。
效果图:

贴上JSON数据(后台查询数据一定要对合并字段排序):
[
{
"city": "广州市",
"area": "天河区",
"gdp":"100"
},
{
"city": "广州市",
"area": "海珠区",
"gdp":"101"
},
{
"city": "广州市",
"area": "番禺区",
"gdp":"102"
},
{
"city": "广州市",
"area": "增城区",
"gdp":"103"
},
{
"city": "深圳市",
"area": "罗湖区",
"gdp":"200"
},
{
"city": "深圳市",
"area": "福田区",
"gdp":"201"
},
{
"city": "深圳市",
"area": "南山区",
"gdp":"202"
},
{
"city": "深圳市",
"area": "宝安区",
"gdp":"203"
},
{
"city": "深圳市",
"area": "龙岗区",
"gdp":"204"
},
{
"city": "深圳市",
"area": "盐田区",
"gdp":"205"
},
{
"city": "广州市",
"area": "白云区",
"gdp":"206"
},
{
"city": "上海市",
"area": "黄浦区",
"gdp":"301"
},
{
"city": "上海市",
"area": "徐汇区",
"gdp":"302"
}
]
HTML代码:
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>BootstrapTable合并单元格</title> <!-- Bootstrap -->
<link th:href="@{/static/css/bootstrap/bootstrap.min.css}" rel="stylesheet" type="text/css">
<link th:href="@{/static/css/bootstrap/bootstrap-table.min.css}" rel="stylesheet" type="text/css"> <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
<!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
<!--[if lt IE 9]>
<script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->
</head>
<body>
<table id="bootstrap-table" class="table table-hover"></table>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<script th:src="@{/static/js/ajax/jquery.min.js}"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script th:src="@{/static/js/bootstrap/bootstrap.min.js}"></script>
<!-- 加载 Bootstrap-Table 插件 -->
<script th:src="@{/static/js/bootstrap/bootstrap-table.min.js}"></script>
<!-- 汉化插件 -->
<script th:src="@{/static/js/bootstrap/bootstrap-table-zh-CN.min.js}"></script>
<script type="application/javascript">
$('#bootstrap-table').bootstrapTable({
url: './../static/json/data.json',
/*sidePagination: "true",*/
pageSize: "10", //每页显示10条
search:true, //显示搜索
searchOnEnterKey:true, //Enter触发搜索
pagination: true, // 是否分页
/*singleSelect:true, //单选
checkboxHeader:false, //隐藏checkbox全选*/
columns: [
{
field: 'checkStatus',
checkbox:true
},
{
field: 'city',
title: '市'
},
{
field: 'area',
title: '区'
},
{
field: 'gdp',
title: 'GDP'
}
],
onLoadSuccess: function () {//当所有数据被加载时触发处理函数
var data = $('#bootstrap-table').bootstrapTable('getData', true);//获取当前页数据
mergeCells(data,'city',1,$('#bootstrap-table'));
},
onPageChange: function (){//当页面更改页码或页面大小时触发
var data = $('#bootstrap-table').bootstrapTable('getData', true);
mergeCells(data,'city',1,$('#bootstrap-table'));
},
}); function mergeCells(data,fieldName,colspan,target){
//声明一个map计算相同属性值在data对象出现的次数和
var sortMap = {};
for(var i = 0 ; i < data.length ; i++){
for(var prop in data[i]){
if(prop == fieldName){
var key = data[i][prop] //fieldName的value
if(sortMap.hasOwnProperty(key)){
sortMap[key] = sortMap[key] * 1 + 1;
} else {
sortMap[key] = 1;
}
break;
}
}
}
/*for(var prop in sortMap){
console.log(prop,sortMap[prop])
}*/
//合并单元格
var index = 0;
for(var prop in sortMap){
var count = sortMap[prop] * 1;
$(target).bootstrapTable('mergeCells',{index:index, field:fieldName, colspan: colspan, rowspan: count});
index += count;
}
}
</script>
</body>
</html>
BootStrap Table 合并单元格的更多相关文章
- css table 合并单元格
1. css table 合并单元格 colspan:合并列, rowspan:合并行, 合并行的时候,比如rowspan="2",它的下一行tr会少一列: 合并列的时候,比如co ...
- table中tr间距的设定table合并单元格 colspan(跨列)和rowspan(跨行)
table中的tr的默认display:table-row,虽然可以修改为display:block但是就失去了tr特有的显示效果,如(td自动对齐): 并且在tr中对起设定padding是有用的,可 ...
- table合并单元格
table合并单元格 新建一张表格.要求表格有四行四列,当中第一行的四列合并,第二行.第三行和第四行的第一列合并 <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- ant design Table合并单元格合并单元格怎么用?
1.ant design table合并单元格怎么用?
- 一种HTML table合并单元格的思路
/** * 合并单元格 * @param table1 表格的ID * @param startRow 起始行 * @param col 合并的列号,对第几列进行合并(从0开始).如果传下来为0就是从 ...
- table合并单元格colspan和rowspan .
colspan和rowspan这两个属性用于创建特殊的表格. colspan是“column span(跨列)”的缩写.colspan属性用在td标签中,用来指定单元格横向跨越的列数: 在浏览器中将显 ...
- JQuery Table 合并单元格-解决Bug版本
网络中提供的方法是: <script type="text/javascript"> function _w_table_rowspan(_w_table_id, _w ...
- bootstrap table 根据单元格中的数据改变单元格的样式
在bootstrap-table.js里面列属性 formatter就是用来格式化单元格的,其默认值是undefined 类型是function,function(value, row, index ...
- table合并单元格 colspan(跨列)和rowspan(跨行)
colspan和rowspan这两个属性用于创建特殊的表格. colspan是“column span(跨列)”的缩写.colspan属性用在td标签中,用来指定单元格横向跨越的列数: 在浏览器中将显 ...
随机推荐
- Window安装AutoCAD
1.运行crack文件夹下的“nlm11.14.1.3_ipv4_ipv6_win64.msi”,根据向导提示完成安装2.同样在“MAGNiTUDE”文件夹中找到“adesk.dat”文件,用记事本打 ...
- [转帖]centos 7 avahi-daemon服务的作用及如何关闭
centos 7 avahi-daemon服务的作用及如何关闭 https://blog.csdn.net/tjjingpan/article/details/81237308 关闭 systemct ...
- 【HTML】前台input上传限制文件类型
仅限制xls文件上传 <input id="uploadSkufile" type="file" value="批量导入" style ...
- 【mysql】windows7 安装mysql5.7 解压缩版 + windows7 安装mysql5.7报错 计算机丢失了MSVCR120.dll解决方法
1.下载mysql 5.7的zip版解压缩的安装包 在mysql官网:http://dev.mysql.com/downloads/mysql/ 2.解压到本地任意目录,并创建一个mysql_data ...
- Java基础之UDP协议和TCP协议简介及简单案例的实现
写在前面的废话:马上要找工作了,做了一年的.net ,到要找工作了发现没几个大公司招聘.net工程师,真是坑爹呀.哎,java就java吧,咱从头开始学呗,啥也不说了,玩命撸吧,我真可怜啊. 摘要: ...
- 关于SQL server事务
- 在 Windows 上的 Visual Studio 中使用 Python
地址:https://docs.microsoft.com/zh-cn/visualstudio/python/tutorial-working-with-python-in-visual-studi ...
- git tag 常用笔记
git tag 常用笔记 查看 tag 列出现有 tag git tag 列出 v1.4.2 相关的 tag git tag -l "v1.4.2" 查看指定 tag 的信息 gi ...
- centos7设置时间
1.查看时间时区 date 2.修改时区 timedatectl set-timezone Asia/Shanghai # 设置系统时区为上海 3.安装ntp 联网校准时间 yum install n ...
- React学习笔记③
生命周期的理解 class App extends Component{ constructor(){ console.log("constructor") //初始化属于组件的属 ...