簡體   English   中英

jsPDF:為每個條目生成新文檔

[英]jsPDF: new document generated for each entry

我有兩個虛擬時間條目,它們已從數據庫帶回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

我的問題是如何阻止為每次輸入生成新文檔?

您的object.map函數將遍歷每個對象,因此調用timesheet(startd, endd, totals); 對於每個對象。 只需將其移出循環就足夠了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM