【问题标题】:PDFmake table dynamic dataPDFmake 表格动态数据
【发布时间】:2019-08-28 21:13:31
【问题描述】:

我正在使用PDFmake创建pdf文件,我想在表格中添加动态数据但正文显示为空。

当我打印 [0] 时这个可以工作,但我有很多动态数据

 getData(installation, request) {
          const phases = {
            layout: {
              hLineWidth: function(i, node) {
                return 0.45;
              },
              vLineWidth: function(i, node) {
                return 0.45;
              }
            },
            table: {
              dontBreakRows: true,
              widths: ["30%", "10%", "60%"],
              body: [
                [

                  {
                    text: installation.phases[0].text,
                    style: "cell"
                  },
                  { text: installation.phases[0].status},
                  {
                    text: this.getFeedback(installation.phases[0]),
                    style: "commentCell"
                  }
                ],
              ]
            }
          };
          console.log(phases);
          return phases;
      }

但如果我使用这个,它会创建空表体。

  getData(installation, request) {
installation.phase.forEach(value =>{
          const phases = {
            layout: {
              hLineWidth: function(i, node) {
                return 0.45;
              },
              vLineWidth: function(i, node) {
                return 0.45;
              }
            },
            table: {
              dontBreakRows: true,
              widths: ["30%", "10%", "60%"],
              body: [
                [

                  {
                    text: data.text,
                    style: "cell"
                  },
                  { text: data.status},
                  {
                    text: this.getFeedback(data),
                    style: "commentCell"
                  }
                ],
              ]
            }
          };
          console.log(phases);
          return phases;
});
}

这里有什么问题?我试过了,但找不到解决办法

【问题讨论】:

  • 您将表格正文的文本设置为 data.text、data.status 和 this.getFeedback(data),但是这个“数据”来自哪里?

标签: javascript typescript pdfmake


【解决方案1】:

我不确定这是否是您的目标。

我已经重写了您的代码并为 PDF 创建了 JSON 结构(常量阶段),然后使用由行组成的 JSON 数组动态填充 table.body。我没有尝试运行它,所以可能会有一些编译错误。希望这会有所帮助!

getData(installation, request) {
  const phases = {
    layout: {
      hLineWidth: function(i, node) {
        return 0.45;
      },
      vLineWidth: function(i, node) {
       return 0.45;
      }
    },
    table: {
     dontBreakRows: true,
     widths: ["30%", "10%", "60%"],
     body: []
    }
   }

   installation.phases.forEach(value => {
    phases.table.body.push([
     { text: value.text, style: 'cell' },
     { text: value.status, },
     { text: this.getFeedback(value), style: 'commentCell' }
    ])
   })

  console.log(phases)
  return phases
}

【讨论】:

    猜你喜欢
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    相关资源
    最近更新 更多