為了更直觀展示表格的一大堆亂七八糟的數據,合並單元格就派上用場。。。
效果圖:

貼上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>
