jsPDF:为每个条目生成的新文档

时间:2017-11-17 16:58:43

标签: javascript php jspdf jspdf-autotable

我有两个虚拟时间条目,我已从我的数据库带回console.log中的页面:

[{
  "FIRST_NAME": "Kiera",
  "LAST_NAME": "Long",
  "USERNAME": "klong.thehelpers@gmail.com",
  "CLOCK_IN": "2017-08-25 09:19:04",
  "START_LUNCH": "0000-00-00 00:00:00",
  "END_LUNCH": "0000-00-00 00:00:00",
  "CLOCK_OUT": "2017-08-25 09:33:28",
  "TOTAL_HRS": "00:00hr:14min:24s"
}, {
  "FIRST_NAME": "Kiera",
  "LAST_NAME": "Long",
  "USERNAME": "klong.thehelpers@gmail.com",
  "CLOCK_IN": "2017-11-12 10:59:59",
  "START_LUNCH": "0000-00-00 00:00:00",
  "END_LUNCH": "0000-00-00 00:00:00",
  "CLOCK_OUT": "2017-11-12 11:39:13",
  "TOTAL_HRS": "00:00hr:39min:14s"
}]

我正在使用 AutoTable将这些结果以表格形式放在中。每次我生成时,都会为每个时间条目创建单独的文档。我需要的是让这两个条目在一个文档上打印,并在一个表中是行。

我也在努力改善写作功能。这是:

$.post('api/hoursprintoff.php', {
  useremail: useremail,
  startdate: startdate,
  enddate: enddate
}, function(data) {
  console.log(data);
  var obj = JSON.parse(data);
  var htmlToInsert = obj.map(function(item) {
    var fname = item.FIRST_NAME;
    var lname = item.LAST_NAME;
    var username = item.USERNAME;
    var startd = item.CLOCK_IN;
    var totals = item.TOTAL_HRS;
    var endd = item.CLOCK_OUT;
    var username = item.USERNAME;


    /*if (startd.length >1 && endd.length > 1) {
        console.log(startd + " " + endd);
        /*$.each(obj, function( key, value ) {
            console.dir( key + ": " + JSON.stringify(value) );
        });
    }*/
    //console.log(username);
    //console.log(startd);
    //console.log(endd);
    //var columns = [{title: "Clock In", dataKey: "CLOCK_IN"}, {title: "Clock Out", dataKey: "CLOCK_OUT"}, {title: "Total Hours", dataKey: "TOTAL_HRS"}];
    //var rows = {data} ;
    //console.log(obj.length);
    //console.log(item);
    /*
    function timesheet(timein, timeout, total) {
            var columns = [{title: "Clock In", dataKey: "CLOCK_IN"}, {title: "Clock Out", dataKey: "CLOCK_OUT"}, {title: "Total Hours", dataKey: "TOTAL_HRS"}];
            $.each(item, function( key, value ) {
                console.log(key, value);
            //var rows = [{'CLOCK_IN': timein, 'CLOCK_OUT': timeout, 'TOTAL_HRS': total}];
            });
            var doc = new jsPDF('p', 'pt');
            //doc.createdCell: function (cell, data) { }
            //doc.autoTable(columns, rows);
            doc.text(20, 20, fname + " " + lname + "'s " + 'Timesheet!');
            doc.text(20, 30, 'Organization Email: ' + username);
            doc.save("Test.pdf");
        }//end of timesheet function
    */

    function timesheet(startd, endd, totals) {
      var doc = new jsPDF('p', 'pt');
      var columns = [{
        title: "Clock In",
        dataKey: "CLOCK_IN"
      }, {
        title: "Clock Out",
        dataKey: "CLOCK_OUT"
      }, {
        title: "Total Hours",
        dataKey: "TOTAL_HRS"
      }];
      var rows = [{
        'CLOCK_IN': startd,
        'CLOCK_OUT': endd,
        'TOTAL_HRS': totals
      }];
      if (item > 1) {
        doc.after(rows);
      }


      //console.log(rows);


      //doc.createdCell: function (cell, data) { }
      doc.autoTable(columns, rows);
      doc.text(20, 20, fname + " " + lname + "'s " + 'Timesheet!');
      //doc.text(20, 30, 'Organization Email: ' + username);
      doc.save("Test.pdf");
    } //end of timesheet function
    timesheet(startd, endd, totals);
  }); //end of object map

}); //end of post

我的问题是如何阻止为每个条目生成新文档?

1 个答案:

答案 0 :(得分:0)

您的object.map函数将遍历每个对象,因此为每个对象调用timesheet(startd, endd, totals);。只需将其移出循环就足够了。