【问题标题】:How to color each two rows in pdfmake table如何为pdfmake表中的每两行着色
【发布时间】:2019-11-20 11:16:54
【问题描述】:

我试图正常地实现一件简单的事情,但我正在使用 PDFMAKE 库从数据中制作 pdf。 在表格的文档中,有一个这样的条纹表格示例:

{
        style: 'tableExample',
        table: {
            body: [
                ['Sample value 1', 'Sample value 2', 'Sample value 3'],
                ['Sample value 1', 'Sample value 2', 'Sample value 3'],
                ['Sample value 1', 'Sample value 2', 'Sample value 3'],
                ['Sample value 1', 'Sample value 2', 'Sample value 3'],
                ['Sample value 1', 'Sample value 2', 'Sample value 3'],
            ]
        },
        layout: {
            fillColor: function (rowIndex, node, columnIndex) {
                return (rowIndex % 2 === 0) ? '#CCCCCC' : null;
            }
        }
    },

但我想用#CCCCCC 每两行而不是一行着色,然后切换颜色。 我尝试了很多东西,但没有一个奏效。 似乎函数循环遍历每一行的每一列。

请有人知道如何使用 pdfmake 库来做到这一点?

【问题讨论】:

  • 两行`#CCCCCC`然后null和再次`#CCCCCC`等等?
  • 嘿,请检查我的答案,它将如何工作:)

标签: javascript formatting modulo conditional-operator pdfmake


【解决方案1】:

下面会产生两个后两个

for(var i = 0; i < 10; i++){
  if(Math.floor(i/2)%2==0){
    console.log('#CCCCCC');
  }
  else{
    console.log(null);
  }
}

所以你的代码应该是

fillColor: function (rowIndex, node, columnIndex) {
    return (Math.floor(rowIndex/2)%2==0) ? '#CCCCCC' : null;
}

【讨论】:

    【解决方案2】:

    您必须添加return (rowIndex % 3 != 0) ? '#CCCCCC' : null;

    试试这个:

    for (let i = 0; i < 9; i++) {
      console.log(((i+1) % 3 != 0) ? '#CCCCCC' : null);
    }

    【讨论】:

    • 嗨,你离我想要的很近。我有一个两行标题,必须先用灰色着色,然后我们开始对每 2 行的颜色进行 swotch。对于 i % 3 ,灰色从第二行开始。但是谢谢你:D
    • 让我修改一下。
    猜你喜欢
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多