简体   繁体   English

最后几行带有 colspan 的 jspdf Autotable

[英]jspdf Autotable with colspan for last some rows

This is actually 3 column table, I need last three rows should be two columns, that means colspan 2 , How can we use the colspan for some rows ?这实际上是 3 列表,我需要最后三行应该是两列,这意味着 colspan 2 ,我们如何将 colspan 用于某些行? This is the table structure and the code这是表结构和代码

--------------------------------
| Si     |  item     |  Amount |
--------------------------------
|  1     | Keyboard  |  10 $   |
|  2     | Mouse     |  5  $   |
--------------------------------


this.cols.push({si:"1",itm:"keyboard",amt:"10"})
this.cols.push({si:"2",itm:"Mouse",amt:"5"})
var columns = [
                {title: "Si.", dataKey: "si"},
                {title: "Item", dataKey: "itm"},
                {title: "Amount", dataKey: "amt"},

            ]
var a = doc.autoTable(columns, this.cols, {
                theme: 'striped',
                styles: {
                    lineWidth: 0.2,
                    lineColor: 0,
                    fontSize:8
                },
                headerStyles: {
                    fillColor: 230,
                    textColor:0,
                    fontStyle:'normal', 
                     halign: 'center'           

                },
                margin: {top: 92},
                columnStyles:{
                    value_usd: {halign: 'right'},
                    value_aed: {halign: 'right'}
                }
            });

The result expected:结果预期:

--------------------------------
| Si     |  item     |  Amount |
--------------------------------
|  1     | Keyboard  |  10 $   |
|  2     | Mouse     |  5  $   |
--------------------------------
| Total              |  15 $   |
--------------------------------
| VAT                |  5%     |
--------------------------------
| Grand Sum          |  15.75  |

Try something like this.尝试这样的事情。

let body = bodyRows(5);
for (var i = 0; i < body.length; i++) {
    var row = body[i];
    if (i >= body.length - 3) {
        row[i]['si'] = {
            colspan: 2,
            content: body[i]['item'],
            styles: {
                valign: 'middle',
                halign: 'center'
            }
        };
    }
}

let head = headRows();

doc.autoTable({
    startY: 60,
    head: head, //head is the header that you have created
    body: body,
    theme: 'striped'
});
return doc;

var data = [{
    'item': 'keyboard',
    'amt': '10'
}, {
    'item': 'keyboard',
    'amt': '5'
}, {
    'item': 'Total',
    'amt': '15'
}, {
    'item': 'VAT',
    'amt': '5'
}, {
    'item': 'Grand Sum',
    'amt': '15.75'
}];

function headRows() {return [{si: 'SI', item: 'item', amt: 'Amount'}];}

function bodyRows(rowCount) {
    rowCount = rowCount || 10;
    let body = [];
    for (var j = 1; j <= rowCount; j++) {
        if (j <= rowCount - 3) {,
            body.push({
                si: j,
                item: data[j]['item'],
                amt: data[j]['amt']
            });
        } else {
            body.push({
                si: data[j]['item'],
                item: data[j]['item'],
                amt: data[j]['amt']
            });
        }
    }
    return body;
}

you can use didParseCell function to solve your problem您可以使用 didParseCell 函数来解决您的问题

for example if you have array of object例如,如果您有对象数组

var objarr=[] //array of object to construct the table
didParseCell:(data)=>{
          if(data.row.index==this.objarr.length-1&&data.column.index==0){
            data.row.cells.description.colSpan=3;
            //description above refer to the column of the table on the lastrow
          }
        }

I've found it works better if in this example you made the table a 6 column table and then your colspans would reflect the widths needed.我发现如果在这个例子中你把表格做成一个 6 列的表格,然后你的 colspans 将反映所需的宽度,它会更好。 So in this example the first two rows would be 3-2colSpan cells, and the following ones would be a 4-colspan cell, and a 2colspan cell row.因此,在此示例中,前两行将是 3-2colSpan 单元格,接下来的两行将是 4-colspan 单元格和 2colspan 单元格行。

--------------------------------
| Si     |  item     |  Amount |
--------------------------------
|  1     | Keyboard  |  10 $   |
|  2     | Mouse     |  5  $   |
--------------------------------
| Total              |  15 $   |
--------------------------------
| VAT                |  5%     |
--------------------------------
| Grand Sum          |  15.75  |

I'll say there is one caveat to this, that you'll have to set a standard column width with your columnStyles, and a blank row of the maximum number of columns.我会说有一个警告,您必须使用 columnStyles 设置标准列宽,以及最大列数的空白行。 A blank non-shown header row has worked for me.一个空白的未显示的标题行对我有用。

 let pdfObject = {
    showHead:'never',
    head:[_.range(6).map(el=>'')],
    body: [Arrays(n)],
    rowPageBreak: "avoid",
    styles: styles,
    headStyles: headerStyles,
    bodyStyles: {
      overflow: "linebreak",
      textColor: [0, 0, 0],
    },
    columnStyle:_.range(6).reduce((cache,el)=>{
      cache[el] = {
        minCellWidth : pdfDocument.internal.pageSize.width/6, 
      };
      return cache;
    } ,{}),
    theme: "grid", 
}

just after looping for the data in version 3 > you can do something like this :在对版本 3 中的数据进行循环之后,您可以执行以下操作:

//your data
let data = {
  details: []
}
//parse the data for the table        
let dataTable = []
let billTotal = 0
for (let index = 0; index < data.details.length; index++) {
    const element = data.details[index];
    billTotal += element.bill_final
    dataTable.push([
        index + 1,
        element.site_id,
        element.wbs,
        element.prov,
        element.penalty_price,
        element.bill_final,
    ]);
}
//push the last one after looping your fixed data.
//you can also style as you like.
dataTable.push(
    [{
            content: 'TOTAL : ',
            colSpan: 5,
            styles: {
                halign: 'right'
            }
        },
        {
            content: billTotal,
            styles: {
                halign: 'center'
            }
        }
    ]
); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM