jquery还原含有rowspan、colspan的table


/**
* remove colspan/rowspan,make tabke be normalize
* @param {*} $
* @param {*}
*/
const normalizeTable = function($, $paramTable) {
let $trs = $paramTable.children('*').children('tr');

//expand colspan
$trs.each(function(i, tr) {
let $curTr = $(tr);
let $tds = $curTr.children('[colspan]');
let tds = [];
$tds.each(function(j, td) {
tds.push(td);
});
tds.forEach(td => {
let $td = $(td);
let colspan = $td.attr('colspan');
$td.removeAttr('colspan');

colspan = parseInt(colspan);

for (let k = 1; k < colspan; k++) {
let $clone = $td.clone();
$td.after($clone);
}
});
});

//expand rowspan
$trs = $paramTable.children('*').children('tr');
for (let i = $trs.length - 1; i >= 0; i--) {
let curRowIndex = i;

let $curTr = $($trs[i]);
let $tds = $curTr.children('[rowspan]');
$tds.each(function(j, td) {
let $td = $(td);

let index = td.cellIndex;

let rowspan = parseInt($td.attr('rowspan'));
let lastRowIndex = curRowIndex + rowspan;
$td.removeAttr('rowspan');

for (let k = curRowIndex + 1; k < lastRowIndex; k++) {
let $currentTr = $($trs[k]);
let $flagTd = $currentTr.children(`:eq(${index})`);
if ($flagTd.length > 0) {
$flagTd.before($td.clone());
} else {
$currentTr.children(':last').after($td.clone());
}
}
});
}

return $paramTable;
};

posted on 2017-03-23 09:39  sdfczyx  阅读(203)  评论(0)    收藏  举报