下面是可以分割嵌套元素的版本,
复制内容到剪贴板
代码:
function doPrint(action) {
// 打印文档对象
var myDoc = {
documents : document,
copyrights : '杰创软件拥有版权 www.jatools.com' // 版权声明必须
};
normalizeRowHeight($('#break-table'), 380);
if (action == '打印预览')
getJatoolsPrinter().printPreview(myDoc);
else if (action == '打印')
getJatoolsPrinter().print(myDoc, true);
else
getJatoolsPrinter().print(myDoc, false);
}
function normalizeRowHeight($table, maxHeight) {
if (!$table.is(".handled")) {
var cols = 0;
$('tr:first', $table).children().each(function() {
if ($(this).attr('colspan')) {
cols += +$(this).attr('colspan');
} else {
cols++;
}
});
_normalize($table, maxHeight, cols);
$table.addClass("handled");
}
}
function _normalize($table, maxHeight, cols) {
var rowspans = [];
$('tr', $table).each(function(row) {
var $tr = $(this);
var c = 0;
$tr.children().each(function(i) {
for (; c < cols;) {
if (!rowspans[c] || (--rowspans[c].rows) == 0) {
break;
}
c += rowspans[c].colspan;
}
rowspans[c] = {
index : i,
row : row,
rows : parseInt($(this).attr("rowspan") || 1),
rowspan : parseInt($(this).attr("rowspan") || 1),
colspan : parseInt($(this).attr("colspan") || 1),
$target : $(this)
}
c += rowspans[c].colspan;
});
if (!$tr.is('.handled') && $tr.height() > maxHeight) {
// 高度超过最大值,需要分割单元格
var maxCellIndex = null;
var maxChars = 0;
var $tds = $('td', $tr);
$tds.each(function(i) {
if ($(this).text().length > maxChars) {
maxCellIndex = i;
maxChars = $(this).text().length;
}
});
var $clonedTr = $tr.clone(true);
var $maxCell = $($tds[maxCellIndex]);
var $clonedMaxCell = $($clonedTr.find('td')[maxCellIndex]);
var selectText = function($node) {
var text = $node.text();
var from = 0;
var to = text.length;
var selected = null;
while (from < to - 1) {
selected = text.substring(0, (from + to) / 2);
$node.text(selected);
if ($tr.height() > maxHeight) {
to = selected.length;
} else
from = selected.length;
}
return selected;
}
var makeFit = function($target, $clonedTarget) {
var children = $target.children();
if (children.length) {
$clonedTarget.html("");
for (var k = children.length - 1; k >= 0; k--) {
var $k = $(children[k]);
$k.remove();
if ($tr.height() > maxHeight) {
// 去掉,还是超高,将这个,放到$clonedMaxCell上去
$k.prependTo($clonedTarget);
} else {
// 去掉$k可以,加上就超高,一分两半,前半部分在这行,后半部分,到后面行
var $clonedK = $k.clone(true);
$k.appendTo($target);
$clonedK.prependTo($clonedTarget);
makeFit($k, $clonedK);
/*
* var selected = selectText($k); $k.text(selected);
* $clonedK.text($clonedK.text()
* .substring(selected.length));
* $clonedK.prependTo($clonedTarget);
*/
break;
}
}
} else {
var selected = selectText($target);
$target.text(selected);
$clonedTarget.text($clonedTarget.text()
.substring(selected.length));
}
}
makeFit($maxCell, $clonedMaxCell);
$tr.addClass('handled');
$clonedTr.insertAfter($tr);
var $clonedCells = $clonedTr.children();
for (var x = 0; x < cols; x++) {
var cell = rowspans[x];
if (cell) {
if (!(cell.row == row && cell.rowspan == 1)) {
// 如果不是当前行,或者,
var span = parseInt(cell.$target.attr("rowspan") || 1);
cell.$target.attr("rowspan", span + 1);
if (cell.row == row && cell.rowspan > 1) {
$($clonedCells[cell.index]).remove();
}
}
}
}
_normalize($table, maxHeight, cols);
return false;
}
})
}