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标签中,用来指定单元格横向跨越的列数: 在浏览器中将显 ...
随机推荐
- pandas的使用(7)--分组
pandas的使用(7)--分组
- Rider中Winform开发支持预览(5)
1.Rider .netCore3.0 winform设计器支持预览,这点vs目前还不支持. 2.不过winform下控件选择工具栏都是没有图标的
- dp - 最大子矩阵和 - HDU 1081 To The Max
To The Max Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1081 Mean: 求N*N数字矩阵的最大子矩阵和. ana ...
- windows10下录屏
windows10自带了录屏功能.运行win+G即可打开.如果出现错误,可以运行如下PS脚本. https://files.cnblogs.com/files/mqingqing123/reinsta ...
- 异步编程的类型系统:promise & future & closure & observable----异步编程类型的结构和操作
异步编程类型的结构和操作. 上下文维护. A promise represents the eventual result of an asynchronous operation. The prim ...
- 练手WPF(一)——模拟时钟与数字时钟的制作(上)
一.Visual Studio创建一个WPF项目. 简单调整一下MainWindow.xaml文件.主要使用了两个Canvas控件,分别用于显示模拟和数字时钟,命名为AnalogCanvas.digi ...
- IDEA 部署spring Cloud
Spring cloud Eureka Eureka Server,注册中心 Eureka Client,所有要进行注册的微服务通过Eureka Client 连接到 Eureka Server ,完 ...
- 详解Go变量类型的内存布局
定义 每当我们编写任何程序时,我们都需要在内存中存储一些数据/信息.数据存储在特定地址的存储器中.内存地址看起来像0xAFFFF(这是内存地址的十六进制表示). 现在,要访问数据,我们需要知道存储它的 ...
- C# 构造基础返回值类型-BaseResponse
学无止境,精益求精 十年河东,十年河西,莫欺少年穷 用于基础返回值类型,如下: using System; using System.Collections.Generic; using System ...
- idea中Entity实体中报错:cannot resolve column/table/...解决办法。
idea中Entity实体中报错:cannot resolve column/table/...解决办法. 若idea中Entity实体中报错: cannot resolve column.... c ...