Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a multiple table header

I am trying to make a table with 2 headers merged. At the moment i made 2 seperate tables with 2 seperate headers and it looks okay, but when the table width expands the first table header does not expand. How can i merge the 2 headers or can i make 1 table with 2 tableheaders. Please see picture (how the table is at the moment with 2 tableheaders) auto table at the moment

Here is my code :

function createPDF(){
             /** START PDF INSTANCE */
            //var doc = new jsPDF('p', 'pt');
            var doc = new jsPDF('l', 'pt');
            var row = 80;
            addPdfHeader(doc, row, vm.translate("REPORT.LEGALFORMS ")+" "+vm.activeCompanyYear);

            doc.setFillColor(33, 150, 243); 
            var columns = [ "               ",vm.activeCompanyYear,vm.activeCompanyYear-1,vm.activeCompanyYear-2];

            var rows = [];

            var description = "";
            for(var j=0; j<vm.reportData.length; j++){
                var obj = vm.reportData[j];


                description = obj.descriptionEng;

                if(description == "total"){
                    description = vm.translate("REPORT.REGISTRY.TOTAL");
                } 

                var singleRow = [description,
                                 obj.year3Total,
                                 obj.year3Local,
                                 obj.year3International,
                                 obj.year2Total,
                                 obj.year2Local,
                                 obj.year2International,  
                                 obj.year1Total,
                                 obj.year1Local,
                                 obj.year1International
                               ]
               rows.push(singleRow);
            }                       

            doc.autoTable(columns, [], {
                theme : 'grid',
                styles: {
                   halign: 'right'
                },
                headerStyles: {
                   fillColor: [33, 150, 243],
                   halign:'center',
                   lineWidth: 1,
                   lineColor: [221, 221, 221]

                },
                columnStyles:{
                     0: {columnWidth: 266}
                },
                margin : {
                  top : 100
                }
            });

            var columns2 = [ vm.translate("MENU.SETTINGS.LEGALFORM"), 
                             vm.translate("REPORT.REGISTRY.TOTAL"),
                             vm.translate("REPORT.REGISTRY.LOCAL"),
                             vm.translate("REPORT.REGISTRY.INTERNATIONAL"),
                             vm.translate("REPORT.REGISTRY.TOTAL"),
                             vm.translate("REPORT.REGISTRY.LOCAL"),
                             vm.translate("REPORT.REGISTRY.INTERNATIONAL"),
                             vm.translate("REPORT.REGISTRY.TOTAL"),
                             vm.translate("REPORT.REGISTRY.LOCAL"),
                             vm.translate("REPORT.REGISTRY.INTERNATIONAL")
                            ];

            doc.autoTable(columns2, rows, {
                theme : 'grid',
                styles: {
                   halign: 'right'
                },
                headerStyles: {
                   halign:'center',
                   lineWidth: 1,
                   lineColor: [221, 221, 221]
                },
                margin : {
                  top : 120
                },
                columnStyles:{
                     0: {halign:'left'}
                },
                createdCell: function(cell, data) {
                   if(data.row.raw[0] === vm.translate("REPORT.REGISTRY.TOTAL")) {
                      cell.styles.fontStyle = 'bold';
                      cell.styles.fillColor = [255,251,204];
                   }
                }

            });

            doc.save();
        };
like image 826
akshay bhoendie Avatar asked Jan 24 '17 16:01

akshay bhoendie


People also ask

How do I create a multi header table?

To apply the header row formatting to multiple rows in the table: after the table style is applied, select the rows you want included in the "header row" and on the Table tools > Layout tab, click Repeat Header Rows. If header row is ticked in Design tab > Table style options, you will see your header row formatting.

Can an HTML table have 2 headers?

You can have different headers for different rows.


1 Answers

Something like this (v3 and up):

let head = [
    [
        {content: 'People', colSpan: 3, styles: {halign: 'center', fillColor: [22, 160, 133]}}, 
        {content: 'Data', colSpan: 2, styles: {halign: 'center', fillColor: [22, 160, 133]}}
    ],
    ['ID', 'Name', 'Email', 'City', 'Sum'],
];

doc.autoTable({
    startY: 60,
    head: head,
    body: body,
    theme: 'grid'
});

Multiple headers with v3 of autotable

like image 81
Simon Bengtsson Avatar answered Sep 20 '22 18:09

Simon Bengtsson