bootstrap表格行合并问题
js相关代码:
function mergeCells(data, fieldName, colspan, target) {
if (!data) {
return;
}
var numArr = [];
var value = data[0][fieldName];
var num = 0;
for (var i = 0; i < data.length; i++) {
if (value != data[i][fieldName]) {
numArr.push(num);
value = data[i][fieldName];
num = 1;
continue;
}
num++;
}
if (typeof (value) != "undefined" && value != "") {
numArr.push(num);
}
var merIndex = 0;
for (var i = 0; i < numArr.length; i++) {
$(target).bootstrapTable('mergeCells', {
index: merIndex,
field: fieldName,
colspan: colspan,
rowspan: numArr[i]
});
merIndex += numArr[i];
}
}
Faxingfenlei.initTable=function () {
$('#id').bootstrapTable({
classes:"table table-hover " + "test",
dataType:"json",
url:url,
pagination:true,
pageList: [50, 100, 200],
striped: true,
queryParams:function () {
var param = {
riqiStart: $("#riqiStart").val(),
riqiEnd:$("#riqiEnd").val(),
pageNumber: Faxingfenlei.pageNumber,
pageSize: Faxingfenlei.pageSize
};
return param;
},
queryParamsType:'',
resizable:true,//列表可拖拽
columns: defaultColunms,
height:$("body").height() - $(".layui-form").outerHeight(true) - 26,
sidePagination:"server",
onPageChange:function(number, size){
Faxingfenlei.pageNumber = number ;
Faxingfenlei.pageSize = size
},
onLoadSuccess: function (data) { //加载成功时执行
if (data.total != 0) {
mergeCells(data.rows, "xiangmu1", 1, $('#id'));//行合并
mergeCells(data.rows, "xiangmu2", 1, $('#id'));//行合并
}
}
});
};
表格:
Faxingfenlei.initColumn = function () {
return [
[
{title : '项目',colspan: 3,align:'center'},
{title: '今年同期', field: 'jinniantongqi', align: 'center', halign: 'center',
formatter: function (value) {return toDecimal2(value)}}
], [
{title: '', field: 'xiangmu1', align: 'center', halign: 'center',
formatter:function (value, row, index) {return getFormatter(value, row, index)}},
{title: '', field: 'xiangmu2', align: 'center', halign: 'center',
formatter:function (value, row, index) {return getFormatter(value, row, index)}},
{title: '', field: 'xiangmu3', align: 'center', halign: 'center',
formatter:function (value, row, index) {return getFormatter(value, row, index)}},
]];
};