【发布时间】:2020-09-08 14:05:10
【问题描述】:
我想在打印表格时更改表格单元格的颜色。打印自定义函数被调用并正常工作,但颜色没有改变..
当我渲染初始表格时,我调用 rollCallback 来设置单元格颜色
rowCallback: function(row, data, index){
if(data.scores <= 1){
$(row).find('td:eq(2)').css('background-color', 'red');
}
if(data.scores > 1 && data.scores <=2 ){
$(row).find('td:eq(2)').css('background-color', 'yellow');
}
if(data.scores > 2){
$(row).find('td:eq(2)').css('background-color', 'green');
}
}
在打印按钮中。我使用相同的代码再次渲染背景颜色
{
extend : 'print',
text : '<i class="fa fa-print fa-lg"></i>',
titleAttr : 'Print',
exportOptions : {
columns : [0,1,2,4]
},
customize: function(win,conf,table) {
table.rows().every(function(index,element) {
if(this.data().scores <= 1)
{
$(this).find('td:eq(2)').css('background-color', 'red');
}
if(this.data().scores > 1 && this.data().scores <=2 )
{
$(this).find('td:eq(2)').css('background-color', 'yellow');
}
if(this.data().scores > 2)
{
$(this).find('td:eq(2)').css('background-color', 'green');
}
});
}
}
颜色不变.. 我正在使用 jquery-3.5.1 数据表-1.10.21 按钮-1.6.3
使用@andrewjames 回答我得到了更好的结果.. 预览现在有颜色.. 但是当从浏览器使用 ctrl-print 时,我再次失去了样式。
【问题讨论】:
-
打印预处理器会从打印视图中去除所有手动添加的类和样式 - 如您所见。 this question 中描述了一种解决方法。
-
@andrewjames 我在我的问题中包含了您提供的链接中提到的自定义功能。那对我不起作用。我要做的就是改变单元格的背景颜色。在您证明的示例中,他正在执行类似的方法调用来更改字体大小
-
我使用其他答案中的方法添加了一个示例 - 希望它有助于澄清。如果我误解了您需要做什么,当然可以告诉我。
标签: datatables