改写merge()方法,直接通过列field查找td
function merge(res) {
var data = res.data;
var mergeIndex = 0;
var mark = 1;
var columsName = ['fun','requirement','qzsxms','qzsxyx'];
for (var k = 0; k < columsName.length; k++) {
var trArr = $(".layui-table-body>.layui-table").find("tr");
for (var i = 1; i < res.data.length; i++) {
var tdCurArr = trArr.eq(i).find('td[data-field="'+ columsName[k] +'"]');
var tdPreArr = trArr.eq(mergeIndex).find('td[data-field="'+ columsName[k] +'"]');
if (data[i][columsName[k]] === data[i-1][columsName[k]] && verifycolumsValue(data,columsName,k,i)) {
mark += 1;
tdPreArr.each(function () {
$(this).attr("rowspan", mark);
});
tdCurArr.each(function () {
$(this).css("display", "none");
});
}else {
mergeIndex = i;
mark = 1;
}
}
mergeIndex = 0;
mark = 1;
}
}
function verifycolumsValue(data,columsName,k,td) {
for (let i = 0; i < k; i++) {
if (data[td][columsName[i]] !== data[td-1][columsName[i]]){
return false;
}
}
return true;
}
避免的混淆情况如图:
merge()方法在渲染方法内使用
var tableIns = table.render({
elem: '#'+ Dfmea.tableId,
url: Qams.ctxPath + '/dfmea/dfmealist?prjid=' + Qams.getUrlParam("prjid"),
page: false,
limit:1000,
cellMinWidth: 60,
loading: true,
even: false,
cols:Dfmea.initColumn(),
done: function (res, curr, count) {
Dfmea.tbData = res.data;
merge(res);
form.render();
}
});