打字稿:如何使用 drawHeaderRow 创建水平 headers 的 table?

Typescript: How to use drawHeaderRow to create table with horizontal headers?

jsPDF 允许创建 table 表单 JSON 数据并将该 table 保存到 PDF 文档中。我创建了一个 Angualr2/Typescript application to do the same. This create table form my JSON data. I'm trying to use jsPDF is create a table with horizontal headers. Example for this given here。创建代码如下。

// Horizontal - shows how tables can be drawn with horizontal headers
examples.horizontal = function () {
  var doc = new jsPDF('p', 'pt');
  doc.autoTable(getColumns().splice(1,4), getData(), {
    drawHeaderRow: function() {
        // Don't draw header row
        return false;
    },
    columnStyles: {
        first_name: {fillColor: [41, 128, 185], textColor: 255, fontStyle: 'bold'}
    }
  });
  return doc;
};

完整代码可用 here。这段代码写在JavaScript。我正在寻找一种将其转换为 Typescript 的方法。有人知道怎么做吗?

您的组件可能如下所示:

@Component({
  selector: 'my-app',
  template: 
    `<h1>JSON to PDF app</h1>
    <div class="container" id="div1">
        <button id="create" (click)="convert('base')">Create file</button> 
        <button id="create" (click)="convert('horizontal')">
           Create file with horizontal table
        </button> 
    </div>
    `
})
export class AppComponent {
  cols: Array<any> = [{
      title: "Details",
      dataKey: 'details'
    }, {
      title: "Values",
      dataKey: 'values'
   }];

  optionsContainer = {
    base: {},
    horizontal: {
      drawHeaderRow: () => false,
      columnStyles: {
          details: {fillColor: [41, 128, 185], textColor: 255, fontStyle: 'bold'}
      }
    }
  };

  rows: Array<any> = [];

  constructor() {
    const item = {
      "Name" : "XYZ",
      "Age" : "22",
      "Gender" : "Male"
    }; 

    this.rows = Object.keys(item).map((key) => {  
      return { 'details': key, 'values': item[key] };
    });
  }

  convert(action){
    const doc = new jsPDF()
       .autoTable(this.cols, this.rows, this.optionsContainer[action]);
    doc.save('Test.pdf');
  }
}

Demo Plunker