Different width for each columns in jspdf autotabl

2019-04-21 19:52发布

My table has 13 columns. How can I get different width for each column? Can I give each column width like this?

styles: {overflow: 'linebreak' ,columnWidth: [100,80,80,70,80,80,70,70,70,70,60,80,100]},

My table Syntax:

> var res = doc.autoTableHtmlToJson(document.getElementById(tableID)); 
> doc.autoTable(res.columns, res.data, {  styles: {overflow: 'linebreak'
> ,columnWidth: [100,80,80,70,80,80,70,70,70,70,60,80,100]},  startY:
> 60,  bodyStyles: {valign: 'top'},  });

4条回答
Fickle 薄情
2楼-- · 2019-04-21 20:17

By default the 'columnsStyles' dont have in 'Options', that's necessary create it and next step you define the 'columnWidth'.

Options['columnStyles'] = {
  0: {columnWidth: 50},
  1: {columnWidth: 'auto'},
  2: {columnWidth: 'wrap'},
  3: {columnWidth: 150}
}
JSPDF.autoTable(columns, values, Options)
查看更多
混吃等死
3楼-- · 2019-04-21 20:26

You would have to transform your columnWidth array to look like this:

doc.autoTable({
  html: document.getElementById(tableID)
  columnStyles: {
    0: {columnWidth: 100},
    1: {columnWidth: 80},
    2: {columnWidth: 80},
    // etc
  }
});

Note the use of columnStyles instead of styles.

查看更多
家丑人穷心不美
4楼-- · 2019-04-21 20:27

In previous release (1.3.4) it could have done like below:

var columns = [
          {title: "Signum", dataKey: "signum"},
          {title: "Name", dataKey: "name"},
          {title: "Role", dataKey: "role"},
          {title: "Location", dataKey: "location"} 
          ]

But the latest one i.e. 2.3.2 requires the below format

doc.autoTable(colums,data,{
    addPageContent:pageContent,
    margin: {horizontal:5,top: 20},
    startY: 0.47*doc.internal.pageSize.height,
    styles: {overflow: 'linebreak'},
    columnStyles: {
     id: {columnWidth: 25}, 
     name:{columnWidth:40}, 
     role: {columnWidth: 15}, 
     location: {columnWidth: 30}
    }
  });

This will fix only id, name, role, and location to the specified limits. Rest other headers will be adjusted accordingly by autotable.js

查看更多
我想做一个坏孩纸
5楼-- · 2019-04-21 20:42

Let's say,[ steps,Methods,process,Delivers,Results ] are all my Table Headers.If i want to increase or decrease table width mean's ,

columnStyles: {
    steps: {columnWidth:215},
    Methods: {columnWidth: 60},
    process: {columnWidth: 100},
    Delivers: {columnWidth: 90},
    Result: {columnWidth: 90}
}

Here you can specify the each column width.

查看更多
登录 后发表回答