简体   繁体   English

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

[英]jsPDF: new document generated for each entry

I have two dummy time entries that I've brought back from my database to my page in a console.log : 我有两个虚拟时间条目,它们已从数据库带回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"
}]

I am using and AutoTable to put these results in table form inside a . 我正在使用 AutoTable将这些结果以表格形式放在 Every time, I generate a , a separate document is created for each of the time entries. 每次生成 ,都会为每个时间条目创建一个单独的文档。 What I need is to get these two entries to print on one document and be rows in one table. 我需要的是让这两个条目打印在一个文档上并成为一张表中的行。

I'm also trying to get better at writing functions too. 我也在尝试更好地编写函数。 Here it is: 这里是:

$.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

My question is how do I stop from generating a new document for each time entry? 我的问题是如何阻止为每次输入生成新文档?

Your object.map function will loop over each object and therefore call timesheet(startd, endd, totals); 您的object.map函数将遍历每个对象,因此调用timesheet(startd, endd, totals); for each object. 对于每个对象。 Simply moving it outside of the loop should be enough. 只需将其移出循环就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM