微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

表格中的行,每列都带有 pdfmake

如何解决表格中的行,每列都带有 pdfmake

在我的 pdf 中,我希望每行中的每个关联元素都被拆分。而如下图所示,每列都有下面附加的值

packStruct = []
packEnergy = []
packDescriptionEnergy = []
packPrice = []

{text: 'Integration',style: 'header',alignment:'center',bold:'true',fontSize: 13},{
          style: 'tableExample',unbreakable: true,table: {
            headerRows: 1,widths: ['auto','auto',100],body: [
              ['Interventi Strutturali','Interventi Energetici','Descrizione Interventi Energetici','Prezzo Interventi Intergrati'],[
              this.packStruct.map(function(item) {
                return item
              }),this.packEnergy.map(function(item) {
                return item
              }),this.packDescriptionEnergy.map(function(item) {
                return item
              }),this.packPrice.map(function(item) {
                return item.toFixed(2) + '€'
              })
            ]
            ]
          }
        },

screen

预期输出

screen2

解决方法

getDocumentation() {
const config = {
      table: {
        headerRows: 1,widths: ['*','*','*'],body: [
            ['Interventi Strutturali','Interventi Energetici','Descrizione Interventi Energetici','Prezzo Interventi Intergrati'],]
      }
};

for(let i=0;i<this.packStruct.length;i++) {
    const newRow = [];
    this.newRow.push(!!this.packStruct[i] ? this.packStruct[i] : "");
    this.newRow.push(!!this.packEnergy[i] ? this.packEnergy[i] : "");
    this.newRow.push(!!this.packDescriptionEnergy[i] ? this.packDescriptionEnergy[i] : "");
    this.newRow.push(!!this.packPrice[i] ? this.packPrice[i].toFixed(2) + '€' : "");
    config.table.body.push(newRow);
}

return {content: [config]};
}

您可以根据需要添加样式,例如表格边框等。这是创建单独行的基本配置。

,

我添加这样的代码:


    getDocumentDefinition() {
    
        const config = {
          table: {
            headerRows: 1,100],body: [
              ['Interventi Strutturali','Prezzo Interventi Integrati'],]
          }
        }
    
        for(let i = 0; i < this.packStruct.length; i++) {
          const newRow = []
          newRow.push(!!this.packStruct[i] ? this.packStruct[i] : "");
    newRow.push(!!this.packEnergy[i] ? this.packEnergy[i] : "");
    newRow.push(!!this.packDescriptionEnergy[i] ? this.packDescriptionEnergy[i] : "");
    newRow.push(!!this.packPrice[i] ? this.packPrice[i].toFixed(2) + '€' : "");
    config.table.body.push(newRow);
        }
    
        return {
    
          content: [
            {
              table: {
                body: config
              }
            },]
       }
    ] 
    }
    }

对吗?我不知道他为什么给我这个错误:

TypeError: Cannot read property 'length' of undefined

但我不知道为什么,因为它以另一种方式打印,所以packStruct中有数据

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。