本文介绍了使用PDFMake动态构建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用使用javascript生成pdf。我正在尝试动态构建一个表但不起作用,这是我的尝试

I'm working with pdfmake to generate pdf with javascript. I'm trying to build a table dynamically but not works ,this my attempt

$.ajax({
    type: "POST",
    url: myURL,
    success:function(data){
        /* data has a format like :
         *[{"peaje":"Peaje 1","ruta":"Ruta 1","fechaCruce":"2014-10-18","hora":"15:42","valor":"5000"},{"peaje":"Peaje 1","ruta":"Ruta 1","fechaCruce":"2014-10-18","hora":"14:21","valor":"7000"},{"peaje":"Peaje 1","ruta":"Ruta 1","fechaCruce":"2014-09-19","hora":"11:58","valor":"17000"}]
         */
        var peajes = JSON.parse( data );
        var body = [];
        var titulos = new Array( 'PEAJE', 'RUTA', 'FECHA CRUCE', 'HORA', 'VALOR' );
        body.push( titulos );
        for (key in peajes)
        {
            if (peajes.hasOwnProperty(key))
            {
                var peaje = peajes[key];
                var fila = new Array();
                fila.push( peaje.peaje.toString() );
                fila.push( peaje.ruta.toString()  );
                fila.push( peaje.fechaCruce.toString() );
                fila.push( peaje.hora.toString()  );
                fila.push( peaje.valor.toString() );
                body.push(fila);
            }
        }
        console.log( body );
        var docDefinition = {
            content: [
            {
                table: {
                   headerRows: 1,
                   widths: [ '*', 'auto', 100, '*' ],
                   body: body
                }
            }]
        };//end docDefinition
        pdfMake.createPdf(docDefinition).open();
    }//end success
  });

这是库的例子

我不知道我做错了什么?

I don't know what am I doing wrong?

推荐答案

你应该制作列名和数组。值:

You should make array of column names & values:

var column = [];
column.push({ text: 'A', style: 'tableHeader'});
column.push({ text: 'B', style: 'tableHeader'});

var value = [];
value.push({ text: 'Asda', style: 'tableHeader'});
value.push({ text: 'Bsa', style: 'tableHeader'});

当你制作一张桌子时,你应该这样做。

When you make a table, you should do like this.

table: {
  headerRows: 1,
    body: [
      column, value
    ]
}

这篇关于使用PDFMake动态构建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 21:02