废话不多说直接上码
onLoadSuccess: function () {//当所有数据被加载时触发处理函数
var data = $('#bootstrap-table').bootstrapTable('getData', true);//获取当前页数据
mergeCells(data,'你想要合并的字段',1,$('#bootstrap-table'));
},
onPageChange: function (){//当页面更改页码或页面大小时触发
var data = $('#bootstrap-table').bootstrapTable('getData', true);
mergeCells(data,'你想要合并的字段',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;
//注意:合并前Java后台Sql要对field字段排序(order by)
//index:合并起始位置,field:合并列名,colspan:合并成一列,rowspan:合并行数
$(target).bootstrapTable('mergeCells',{index:index, field:fieldName, colspan: colspan, rowspan: count});
index += count;
}
}
最后附上隐藏表格内容的小标签
showToggle:false,
showRefresh:false,