我需要在导出到 PDF 时检查列的值是否为负数,如果是,则使整个单元格变为红色或文本装饰:换行。
但是我不知道如何访问元素来进行此类修改。
当背景显示在浏览器中时,我可以修改背景,但导出时它的工作方式不同。
{
extend: 'pdfHtml5',
footer: true,
text: 'PDF',
header: true,
title: t,
orientation: 'landscape',
exportOptions: {
rows: function ( idx, data, node ) {
//node.attributes.style = {background-color: "#000";};
//console.log(node.attributes.style);
//return true;
// return data[2] === 'London' ?
// true : false;
}
},
customize: function(doc) {
doc.content.splice( 1, 0, {
margin: [ 0, 0, 0, 0 ],
alignment: 'center',
image: base64
} );
doc.defaultStyle.fontSize = 10;
doc.pageMargins = [20,10,10,10];
doc.styles.tableHeader.fontSize = 14;
doc.styles.title.fontSize = 12;
// Remove spaces around page title
doc.content[0].text = doc.content[0].text.trim();
}
},
{
extend: 'print',
footer: true,
}
],
fnRowCallback: function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
var valor = aData[3].split('$');
//console.log(teste);
if (parseInt(valor[1])<0) {
$(nRow).css('background-color', '#e96464')
}else{
$(nRow).css('background-color', '#cdedc1')
}
}
});
最佳答案
我刚刚在数据表中发布了答案 forum对于类似的问题。解决办法是在自定义回调中修改PDF文档中的表格。
buttons: [
{
extend: "pdfHtml5",
customize: function(doc) {
age = table.column(3).data().toArray();
for (var i = 0; i < age.length; i++) {
if (age[i] < 40) {
doc.content[1].table.body[i+1][3].fillColor = 'blue';
}
}
}
}
]
直播example在这里。
关于jquery - 更改行背景颜色pdf导出数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47983349/